(1)a:手工编写代码实现对Action中所有方法执行之前的验证
很简单就是讲对应的Action类继承ActionSupport父类后并对父类中的validate方法进行重写即可;
例如:
1 package com.bjyinfu.struts.actions; 2 3 import java.util.regex.Pattern; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 7 public class LoginAction4 extends ActionSupport { 8 9 private String mobile; 10 private String name; 11 public String getMobile() { 12 return mobile; 13 } 14 public void setMobile(String mobile) { 15 this.mobile = mobile; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public String doFirst(){ 24 return "success"; 25 } 26 public String doSecond(){ 27 return "success"; 28 } 29 @Override 30 public void validate() { 31 //等于null是为了防止越过表单方法问 32 if(name==null || "".equals(name.trim())){ 33 //底层有个集合叫fieldErrors.size()>0,说明有验证失败的信息,此时会自动跳转到input视图 34 this.addFieldError("name", "用户名不能为空"); 35 } 36 if(mobile==null || "".equals(mobile.trim())){ 37 this.addFieldError("mobile", "手机号不能为空"); 38 }else if(!Pattern.matches("^1[34578]\d{9}", mobile)){ 39 this.addFieldError("mobile", "手机号不符合规定"); 40 } 41 } 42 }
b:手工编写代码实现对Action中指定方法执行之前的验证
修改被重写的方法名即可。例如:只要吧验证方法名改为validate+actionName;validate固定写法,actionName被指定的验证方法名;这样即可验证指定方法;
1 package com.bjyinfu.struts.actions; 2 3 import java.util.regex.Pattern; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 7 public class LoginAction4 extends ActionSupport { 8 9 private String mobile; 10 private String name; 11 public String getMobile() { 12 return mobile; 13 } 14 public void setMobile(String mobile) { 15 this.mobile = mobile; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public String doFirst(){ 24 return "success"; 25 } 26 public String doSecond(){ 27 return "success"; 28 } 29 30 //这样只会在执行doFirst()方法之前进行验证,而执行doSecond()方法之前不会进行任何验证 31 public void validateDoFirst() { 32 //等于null是为了防止越过表单方法问 33 if(name==null || "".equals(name.trim())){ 34 //底层有个集合叫fieldErrors.size()>0,说明有验证失败的信息,此时会自动跳转到input视图 35 this.addFieldError("name", "用户名不能为空"); 36 } 37 if(mobile==null || "".equals(mobile.trim())){ 38 this.addFieldError("mobile", "手机号不能为空"); 39 }else if(!Pattern.matches("^1[34578]\d{9}", mobile)){ 40 this.addFieldError("mobile", "手机号不符合规定"); 41 } 42 } 43 }
若要将验证没通过的结果展现在对应视图上只需要在对应的input视图页面上引入struts2标签然后在添加<s:fielderror/>即可:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="/struts-tags" prefix="s" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>My First Struts2 Project</title> 9 </head> 10 <body> 11 <!-- 用ognl表达式将Action中的fieldError中的自定义显示内容展现在页面 --> 12 <s:fielderror/> 13 <form action="${pageContext.request.contextPath }/validate/login_validate.action" method="post"> 14 用户名<input type="text" name="name"><br> 15 手机号<input type="text" name="mobile"><br> 16 <input type="submit" value="登录"> 17 </form> 18 </body> 19 </html>
展示结果:
(2)a:通过xml配置文件的方法,对Action中的所有方法执行之前的验证:
首先定义一个xml文件,文件名定义规则:ActionClassName-validation.xml;其中ActionClassName对应的是方法的类名,-validation.xml是固定的,并且此xml文件要放到与对应的方法的文件放到同一个包中,每个xml文件都有约束标签,大部分xml文件中的约束标签是从struts2-core.jar包中找的 图示:
而在此验证配置文件中的约束标签不是从此处找的,而是从xwork.core.jar中找的,图示:
对应的Action方法:
1 package com.bjyinfu.struts.actions; 2 3 4 import com.opensymphony.xwork2.ActionSupport; 5 6 public class LoginAction5 extends ActionSupport { 7 8 private String mobile; 9 private String name; 10 public String getMobile() { 11 return mobile; 12 } 13 public void setMobile(String mobile) { 14 this.mobile = mobile; 15 } 16 public String getName() { 17 return name; 18 } 19 public void setName(String name) { 20 this.name = name; 21 } 22 public String execute(){ 23 System.out.println("执行默认方法"); 24 return "success"; 25 } 26 public String doFirst(){ 27 System.out.println("111111111"); 28 return "success"; 29 } 30 public String doSecond(){ 31 System.out.println("222222222"); 32 return "success"; 33 } 34 }
对应的登录界面,同时发生验证失败是的input视图:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="/struts-tags" prefix="s" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>My First Struts2 Project</title> 9 </head> 10 <body> 11 <!-- 用ognl表达式将Action中的fieldError中的自定义显示内容展现在页面 --> 12 <s:fielderror/> 13 <form action="${pageContext.request.contextPath }/validate/login_validate.action" method="post"> 14 用户名<input type="text" name="name"><br> 15 手机号<input type="text" name="mobile"><br> 16 <input type="submit" value="登录"> 17 </form> 18 </body> 19 </html>
在struts.xml中注册方法:
编写验证xml文件:此文件名为LoginAction5-validation.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE validators PUBLIC 3 "-//Apache Struts//XWork Validator 1.0.3//EN" 4 "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> 5 <validators> 6 <!-- 此name为需要被验证的属性 --> 7 <field name="name"> 8 <!-- 对于此属性进行验证的验证器类型可以有一个或者多个 --> 9 <field-validator type="requiredstring"> 10 <!-- param有0个或者多个,可以看做是判断条件,必须放在message前面 --> 11 <param name="trim">true</param> 12 <!-- 必须要有且唯一 --> 13 <message>用户名不能为空!</message> 14 </field-validator> 15 </field> 16 <field name="mobile"> 17 <field-validator type="requiredstring"> 18 <param name="trim">true</param> 19 <message>手机号不能为空!</message> 20 </field-validator> 21 <field-validator type="regex"> 22 <!-- <![CDATA[]]> CDATA数据区,用于写特殊的表达式 --> 23 <param name="regex"><![CDATA[1[34578]d{9}]]></param> 24 <message>手机号格式不正确!</message> 25 </field-validator> 26 </field> 27 </validators>
b:通过xml配置文件的方法,对Action中的指定方法执行之前的验证:
只需要将验证xml文件的文件名加上对应的struts.xml文件中的注册方法名即可:
例:验证所有方法是 LoginAction5-validation.xml;
将此名称改为:LoginAction5-login_doFirst-validation.xml;
此时就是只对doFirst方法进行验证,而不验证其他方法;