Strust2-数据校验

来源:互联网 发布:java定义一维数组 编辑:程序博客网 时间:2024/06/07 10:08

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

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

三. 注册实例

1. register.jsp 

<%@ page language="java" contentType="text/html; charset=utf-8"%>  <%@ taglib prefix="s" uri="/struts-tags"%>  <html>  <head>    <title>Insert title here</title>  </head> <body>    <s:form action="register" method="post">   <s:textfield name="name" label="姓名" required="true" />   <s:textfield name="age" label="年龄" required="true" />   <s:submit value="提交"></s:submit>   </s:form>   </body> </html> 

2.当姓名和年龄提交时候,用RegistAction类来获取数值:

RegistAction.java 

 1:  public class RegistAction extends ActionSupport {    2:  private String name;    3:      4:  private int age;    5:      6:  ...//省略name,age的setter,getter方法   7:      8:  public String execute() {    9:  return SUCCESS;   10:  }   11:     12:  public void validate() {   13:  if ("".equals(name)) {   14:  this.addFieldError("name", "用户名不能为空");   15:  }   16:  if (name.length() > 6) {   17:  this.addFieldError("name", "用户名长度不能超过5位");   18:  }   19:     20:  if (age < 1 || age > 150) {   21:  this.addFieldError("age", "年龄范围必须在1~150之间");   22:  }   23:  }   24:  } 

.struts.xml    1:  <package name="test1" extends="struts-default" namespace="test1">    2:  <action name="register1" class="test1.RegistAction">    3:  <result name="input">register.jsp</result>    4:  <result name="success">../success.jsp</result>    5:  </action>    6:  </package> 
4.success.jsp    1:  <%@ page contentType="text/html;charset=GBK"%>    2:  <%@ taglib prefix="s" uri="/struts-tags" %>    3:  <s:property value="name"/>,欢迎您登录!  

如果name和age都不输入,则会执行Action中的校验方法,会提示

用户名不能为空

如果不输入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的功能 

1:  package com;   2:      3:  import com.opensymphony.xwork2.ActionSupport;   4:      5:  public class Regist2Action extends ActionSupport {   6:      7:          private String userName;   8:      9:                   10:     11:          public String getUserName() {  12:     13:                 return userName;  14:     15:          }  16:     17:          public void setUserName(String userName) {  18:     19:                 this.userName = userName;  20:     21:          }  22:     23:          public String regist() throws Exception {  24:     25:                 System.out.println("注册成功-regist");  26:     27:                 return SUCCESS;  28:     29:          }  30:     31:            32:     33:          public void validateRegist() {  34:     35:                 if(userName.equals("")){  36:     37:                         addFieldError("userName", "请输入用户名-registValidate");  38:     39:                 }  40:     41:          }  42:     43:  }  44:     45:  <action name="regist2" class="com.Regist2Action" method="regist">  46:  <result name="success">/welcome.jsp</result>  47:  <result name="input">/regist2.jsp</result>  48:  </action>

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



原创粉丝点击