struts2 数据校验

时间:2023-03-08 17:41:09
struts2 数据校验

  通过struts2中延续自xwork框架的validation.xml配置方式进行数据校验,因struts2 下存在三种请求参数的注入方式,固按照不同注入方式对validation.xml的配置进行总结。

一、Action类下直接参数的校验配置:

  • web.xml文件配置struts过滤器:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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>
  • bean文件:因直接在action类下进行注入及验证,不涉及bean文件,固略;
  • RegistServiceDao文件存储注册业务逻辑,略;
  • Action文件:
package com.action

public class RegAction extends ActionSupport{
private String name;
private String password;
private String gender;
private String email;
private String address; private User userInfo;
//setter和getter方法略; public User setInfo(){
User info = new User();
info.setname(this.name);
info.setpassword(this.password);
info.setgender(this.gender);
info.setemail(this.email);
info.setaddress(this.address); return info;
} public String regist(){
RegistServiceDao rsd = new RegistServiceDao();
userInfo = setInfo();
if(rsd.regist(usrInfo)){
return "success";
}else{
return "input";
}
}
}
  • struts配置文件配置action:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="default" extends="struts-defualt">
<action name="regist" class="com.action.RegistAction">
<result name="success">success.jsp</result>
<result name="INPUT">regist.jsp</result>
</action>
</package>
</struts>

****

  • validation.xml文件配置校验器,命名规则为<ActionClassName>-<aliasName>-validation.xml,文件必须放在action文件的同目录下;  

  RegActoin-regiest-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators>
<feild name="name">
<feild-validator type="requiredString">
<message>用户名不能为空</message>
</feild-validator>
</feild>
<feild name="password">
<feild-validator type="requiredString">
<message>密码不能为空</message>
</feild-validator>
</feild>
<feild name="gender">
<feild-validator type="requiredString">
<message>性别不能为空</message>
</feild-validator>
</feild>
<feild name="email">
<feild-validator type="email">
<message>油箱格式不正确</message>
</feild-validator>
</feild>
</validators>

二、DomainModel的注入方式下的数据校验:

  • web.xml配置文件无变化,同第一种;
  • bean文件,包含setter和getter的完整bean文件:
package com.entity

public class User{
private String name;
private String password;
private String gender;
private String email;
private String address; public User(){} public User(String name,String password,String gender,String email,String address){
this.name = name;
this.password = password;
this.gender = gender;
this.email = email;
this.address = address;
} //setter和getter方法略
}
  • RegistServiceDao文件存储注册业务逻辑,略;
  • Action文件,利用User对象注入数据,添加user的getter和setter方法,同时调用字段值时亦通过对象(user.getName()):
package com.action

public class RegAction extends ActionSupport{

    //无需实例化User
private User user; public User getUser(){
return user;
} public void setUser(User user){
this.user = user;
} public String regist(){
RegistServiceDao rsd = new RegistServiceDao();
if(rsd.regist(user)){
return "success";
}else{
return "input";
}
}
}
  • struts配置文件配置Action,同第一种;

********

  • validation.xml文件配置配置校验器,校验user对象的字段需通过visitor校验器,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators>
<field name="user">
<field-validator type="visitor">
<param name="context">abc</param>
<param name="appendPrefix">true</param>
</field-validator>
</field>
</validators>
  • User-abc-validation.xml文件配置visitor校验,命名规则为类似Action的校验配置文件,文件位置亦必须与实体类同一目录下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators>
<feild name="name">
<feild-validator type="requiredString">
<message>用户名不能为空</message>
</feild-validator>
</feild>
<feild name="password">
<feild-validator type="requiredString">
<message>密码不能为空</message>
</feild-validator>
</feild>
<feild name="gender">
<feild-validator type="requiredString">
<message>性别不能为空</message>
</feild-validator>
</feild>
<feild name="email">
<feild-validator type="email">
<message>油箱格式不正确</message>
</feild-validator>
</feild>
</validators>

!!

  • jsp文件改动,因DomainModel下的注入方式,对参数设定的字段名称需要加对象实例的前缀(user.name):
<s:form action = "regist" method = "post">
<s:textfield name="user.name" label="用户名"/>
<s:password name="user.password" label="密码"/>
<s:texfield name="user.gender" label="性别"/>
<s:texfield name="user.email" label="油箱"/>
<s:texfield name="user.address" label="地址"/>
<s:submit/>
</s:form>

三、ModelDriven注入下的数据校验,与域模型相比,主要是Action及jsp注入参数发生变化,同时需要对visitor校验器参数进行调整:

  • web.xml配置文件无变化;
  • bean文件无变化;
  • RegServiceDao文件无变化;
  • Action文件,需做以下修改:
    • 声明ModelDriven接口(泛型接口);
    • 需要声明getModel方法,同时对User类进行实例化;
    • 无需添加bean对象的getter和setter方法;
package com.action

public class RegAction extends ActionSupport implements ModelDriven<User>{

    //无需实例化User
private User user; public User getModel(){
if(user!=null){
return user;
}else{
user = new User();
return user;
}
} public String regist(){
RegistServiceDao rsd = new RegistServiceDao();
if(rsd.regist(user)){
return "success";
}else{
return "input";
}
}
}
  • struts文件配置Action无变化;

********

  • validation.xml文件配置校验器,同第二种,使用visitor校验器,需要修改如下两个参数:
    • field标签的name属性为:model;
    • field下param标签的name为"appendPrefix" 属性的值修改为false(字段不再需要添加bean对象前缀);
  • User-abc-validation.xml配置校验器无变化;

!!

  • jsp页面需做简单修改,接收参数不需添加bean对象前缀
<s:form action = "regist" method = "post">
<s:textfield name="name" label="用户名"/>
<s:password name="password" label="密码"/>
<s:texfield name="gender" label="性别"/>
<s:texfield name="email" label="油箱"/>
<s:texfield name="address" label="地址"/>
<s:submit/>
</s:form>