如何进行声明式验证之字段验证?

来源:互联网 发布:js jq时间段选择控件 编辑:程序博客网 时间:2024/06/06 01:44

使用一个声明式验证程序需要 3 个步骤:

1. 确定哪些 Action 字段需要验证
2. 编写一个验证程序配置文件. 它的文件名必须是以下两种格式之一: 
若一个 Action 类的多个 action 使用同样的验证规则: ActionClassName-validation.xml
若一个 Action 类的多个 action 使用不同的验证规则: ActionClass-alias-validation.xml, 例如 UserAction-User_create-validation.xml (想了解这个条如何使用,请点击大笑
3. 确定验证失败时的响应页面: 在 struts.xml 文件中定义一个 <result name=“input”> 的元素.
 


如何进行声明式验证之字段验证


1 搭建环境:

eclipase:Luna Release (4.4.0)

 sturts2:2.3.28  下载地址:http://download.csdn.net/detail/chuck_kui/9513090


项目结构:



~~~~~~~~~~~~~~~~~~~华华 ......,严肃点 。嗯 ,好的,这是分割线~~~~~~~~~~


Person.java

<span style="font-size:14px;">package com.baidu.domain;public class Person {private Integer age;public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Person [age=" + age + "]";}}</span><span style="font-weight: bold; font-size: 18px;"></span>
Action  :TestAgeValidation.java

<span style="font-size:14px;">package com.baidu.ActionValidation;import com.baidu.domain.Person;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.Preparable;public class TestAgeValidation extends ActionSupport implementsModelDriven<Person>, Preparable {private static final long serialVersionUID = 1L;@Overridepublic String execute() throws Exception {return super.execute();}private Person person;@Overridepublic Person getModel() {person = new Person();return person;}@Overridepublic void prepare() throws Exception {System.out.println("进来了吗?");}}</span>
验证器:TestAgeValidation-validation.xml

<span style="font-size:14px;"><!DOCTYPE validators PUBLIC        "-//Apache Struts//XWork Validator 1.0.2//EN"        "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"><validators>     <field name="age">         <field-validator type="int">             <param name="min">20</param>             <param name="max">50</param>             <message>Age needs to be  22222 between ${min} and ${max}</message>         </field-validator>     </field>     </validators></span>


配置:

struts.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 配置全局的国际化资源文件 --><constant name="struts.custom.i18n.resources" value="nihao"></constant><package name="default" namespace="/" extends="struts-default"><!-- 这个就是起烂笔头的作用  --><interceptors><interceptor-stack name="baiduStack"><interceptor-ref name="paramsPrepareParamsStack"><param name="prepare.alwaysInvokePrepare">false</param></interceptor-ref></interceptor-stack></interceptors><default-interceptor-ref name="baiduStack"/><action name="testAge" class="com.baidu.ActionValidation.TestAgeValidation"><result>/success.jsp</result><result name="input">/validation.jsp</result></action></package></struts></span>

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">       <filter>    <filter-name>struts2</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>    </web-app>


页面:validation.jsp

<span style="font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><center><br><br><s:form action="testAge" ><s:textfield name="age" label="Age"></s:textfield><s:submit></s:submit></s:form></center></body></html></span>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>Age:<s:property value="age" /><h3>恭喜你有为青年!</h3></body></html>






55 大于50  报提示与页面  

等于20 验证通过,

验证器好用!

0 0
原创粉丝点击