struts2 数据校验



struts2 数据校验。

一. ActionSupport是个工具类,他实现了Action, Validatable等接口, Validatable提供validate()方法进行数据验证.Action只要继承ActionSupport类,重写validate()方法就可以进行数据验证

二. 校验的流程      首先,Struts框价对输入数据进行类型转换,然后再进行数据校验,如果类型转换与数据校验都没有错误发生, 就进入execute(),否则请求将被转发到input视图

三. 注册实例     首先新建RegistAcion.java

Java代码 复制代码 收藏代码
  1. package com;
  2. import java.util.Date;
  3. import com.opensymphony.xwork2.ActionSupport;
  4. public class RegistAction extends ActionSupport {
  5.     private String userName;
  6.     private Integer age;
  7.     private Date birthday;
  8.     public String getUserName() {
  9.         return userName;
  10.     }
  11.     public void setUserName(String userName) {
  12.         this.userName = userName;
  13.     }
  14.     public Integer getAge() {
  15.         return age;
  16.     }
  17.     public void setAge(Integer age) {
  18.         this.age = age;
  19.     }
  20.     public Date getBirthday() {
  21.         return birthday;
  22.     }
  23.     public void setBirthday(Date birthday) {
  24.         this.birthday = birthday;
  25.     }
  26.     @Override
  27.     public String execute() throws Exception {
  28.         System.out.println(“注册成功”);
  29.         return SUCCESS;
  30.     }
  31.     @Override
  32.     public void validate() {
  33.         if(“”.equals(userName)){
  34.             addFieldError(“userName”, “username is empty”);
  35.         }
  36.         if(null != age){
  37.             if(1 > age || 150 < age){
  38.                 addFieldError(“age”, “age invalid”);
  39.             }
  40.         }
  41.     }
  42. }
package com;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class RegistAction extends ActionSupport {

	private String userName;

	private Integer age;

	private Date birthday;

	public String getUserName() {

		return userName;

	}

	public void setUserName(String userName) {

		this.userName = userName;

	}

	public Integer getAge() {

		return age;

	}

	public void setAge(Integer age) {

		this.age = age;

	}

	public Date getBirthday() {

		return birthday;

	}

	public void setBirthday(Date birthday) {

		this.birthday = birthday;

	}

	@Override

	public String execute() throws Exception {

		System.out.println("注册成功");

		return SUCCESS;

	}

	@Override

	public void validate() {

		if("".equals(userName)){

			addFieldError("userName", "username is empty");

		}

		if(null != age){

			if(1 > age || 150 < age){

				addFieldError("age", "age invalid");

			}

		}

	}

}

配置Action

Xml代码 复制代码 收藏代码
  1. <action name=”regist” class=”com.RegistAction” method=”regist”>
  2.       <result name=”success”>/welcome.jsp</result>
  3.       <result name=”input”>/regist.jsp</result>
  4. </action>
<action name="regist" method="regist">

      <result name="success">/welcome.jsp</result>

      <result name="input">/regist.jsp</result>

 </action>

接着是注册页面和注册成功页面

regist.jsp

Html代码 复制代码 收藏代码
  1. <body>
  2.     <form action=”regist.action” method=”post”>
  3.         <s:fielderror></s:fielderror>
  4.         <table>
  5.             <tr>
  6.                 <td>userName:</td>
  7.                 <td>
  8.                     <input type=”text” name=”userName”>
  9.                 </td>
  10.             </tr>
  11.             <tr>
  12.                 <td>age:</td>
  13.                 <td>
  14.                     <input type=”text” name=”age”>
  15.                 </td>
  16.             </tr>
  17.             <tr>
  18.                 <td>birthday:</td>
  19.                 <td>
  20.                     <input type=”text” name=”birthday”>
  21.                 </td>
  22.             </tr>
  23.             <tr>
  24.                 <td colspan=”2″>
  25.                     <s:submit value=”注册”></s:submit>
  26.                 </td>
  27.             </tr>
  28.     </form>
  29.   </body>
<body>

  	<form action="regist.action" method="post">

  		<s:fielderror></s:fielderror>

  		<table>

  			<tr>

  				<td>userName:</td>

  				<td>

  					<input type="text" name="userName">

  				</td>

  			</tr>

  			<tr>

  				<td>age:</td>

  				<td>

  					<input type="text" name="age">

  				</td>

  			</tr>

  			<tr>

  				<td>birthday:</td>

  				<td>

  					<input type="text" name="birthday">

  				</td>

  			</tr>

  			<tr>

  				<td colspan="2">

  					<s:submit value="注册"></s:submit>

  				</td>

  			</tr>

   	</form>

  </body>

如果不输入userName, age输入为abc,会提示 • Invalid field value for field “age”. • username is empty

1. 其中Invalid field value for field “age” 信息是struts2通过内置的类型转换器进行类型转换时,如果不能成功转换, struts2框架自动生成一条错误信息,并将该错误信息放到addFieldError里面,这种默认的输出信息格式是在  xwork-2.0.4.jar中定义的.  com/opensymphony/xwork2/xwork-messages.properties文件中有一条xwork.default.invalid.fieldvalue=Invalid field value for field “{0}”.


2. 这是一种全局的错误提示方式,整个系统中只要是字段类型转换错误都会这样提示,我们也可以改变这种输出格式,只要在全局的国际资源文件中重写xwork.default.invalid.fieldvalue就可以了.

实现方式: 在struts.xml中加入<constant name=”struts.custom.i18n.resources” value=”messageResource”></constant> 或者也可以在struts.properties中加入struts.custom.i18n.resources=messageResource 指定国际化资源文件名为messageResource. Properties

新建messageResource. Properties资源文件并添加数据xwork.default.invalid.fieldvalue={0} failure 修改之后字段类型转换错误提示为 : {0} failure

3 所有的类型转换失败后,struts2会将基本类型设置为0,对象类型设置为null,这里的age的类型为Integer,当类型转换失败age值为null,如果age的类型为int,那么转换失败后值为0

4.这种提示信息不够友好,也可以定义局布的提示信息,为每一个Action新建一个properties文件,文件名为XXX.properties(Action名.properties)

实现方式:新建RegistAction.properties并添加 invalid.fieldvalue.age=age error invalid.fieldvalue.birthday=birthday error 其中age和birthday分别为字段的名称
四. Struts2也提供类似BaseDispatchAction的功能

Java代码 复制代码 收藏代码
  1. package com;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class Regist2Action extends ActionSupport {
  4.     private String userName;
  5.     public String getUserName() {
  6.         return userName;
  7.     }
  8.     public void setUserName(String userName) {
  9.         this.userName = userName;
  10.     }
  11.     public String regist() throws Exception {
  12.         System.out.println(“注册成功-regist”);
  13.         return SUCCESS;
  14.     }
  15.     public void validateRegist() {
  16.         if(userName.equals(“”)){
  17.             addFieldError(“userName”, “请输入用户名-registValidate”);
  18.         }
  19.     }
  20. }
package com;

import com.opensymphony.xwork2.ActionSupport;

public class Regist2Action extends ActionSupport {

	private String userName;

	public String getUserName() {

		return userName;

	}

	public void setUserName(String userName) {

		this.userName = userName;

	}

	public String regist() throws Exception {

		System.out.println("注册成功-regist");

		return SUCCESS;

	}

	public void validateRegist() {

		if(userName.equals("")){

			addFieldError("userName", "请输入用户名-registValidate");

		}

	}

}
Xml代码 复制代码 收藏代码
  1. <action name=”regist2″ class=”com.Regist2Action” method=”regist”>
  2.             <result name=”success”>/welcome.jsp</result>
  3.             <result name=”input”>/regist2.jsp</result>
  4. </action>
<action name="regist2" method="regist">

    		<result name="success">/welcome.jsp</result>

    		<result name="input">/regist2.jsp</result>

 </action>

 

指定了method为regist,当请求时会执行regist(),不会再去执行默认的execute()方法了, validateRegist()方法是专门针对regist校验的.(格式为validate+方法名)

http://callan.iteye.com/blog/185418