遇到类型转换错误的时候(也就是说不能进行类型转换),struts2框架自动生成一条错误信息,并且将该错误信息放到addFieldError里面
类型转换与输入校验的流程
1. 首先Struts2对客户端传来的数据进行类型转换
2. 类型转换完毕后再进行输入校验
3. 如果类型转换和输入校验都没有错误发生,那么进入execute方法(调用商业逻辑)
注意:如果类型转换不成功,也同样要进行输入校验
1,新建一个register2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <table> <s:fielderror/> <s:form action ="register2"> <s:textfield name="username" label="username"></s:textfield> <s:password name="password" label="password"></s:password> <s:password name="repassword" label="repassword"></s:password> <s:textfield name="age" label="age"></s:textfield> <s:textfield name="birthday" label="birthday"></s:textfield> <s:textfield name="graduation" label="graduation"></s:textfield> <s:submit value="submit"></s:submit> </s:form> </table> </body> </html>
2,struts.xml配置如下
<action name="register2" class="com.test.action.RegisterAction2"> <result name="success">/success.jsp</result> <result name="input">/register2.jsp</result> </action>
3,RegisterAction.java,在validate方法中进行校验,注意这里是addActionError
package com.test.action; import java.sql.Date; import java.util.Calendar; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction2 extends ActionSupport { private String username; private String password; private String repassword; private int age; private Date birthday; private Date graduation; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRepassword() { return repassword; } public void setRepassword(String repassword) { this.repassword = repassword; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Date getGraduation() { return graduation; } public void setGraduation(Date graduation) { this.graduation = graduation; } public String execute() throws Exception { System.out.println("execute....."); return SUCCESS; } public void validate() { System.out.println("validate ......."); if(age < 0){ this.addActionError( "your age is error.action error"); } if(null == username || username.equals("")){ this.addActionError("please input your username ,action error"); } if(null == password || null == repassword || !password.equals(repassword)){ this.addActionError("your password is invalid ,action error"); } if(null != birthday && null != graduation){ Calendar c1 =Calendar.getInstance(); c1.setTime(birthday); Calendar c2 =Calendar.getInstance(); c2.setTime(graduation); if(c1.after(c2)){ this.addActionError("birthday should be before graduation"); } } } }
4,可以在action中指定方法,如
<action name="register2" class="com.test.action.RegisterAction2" method="abc"> <result name="success">/success.jsp</result> <result name="input">/register2.jsp</result> </action>
在程序执行到RegisterAction2时,会去执行abc方法 ,不再去执行execute,但会继续调用validate方法进行输入校验。也可以为abc编写专用的校验方法validateAbc,注意:执行完专用的validateAbc后,struts2仍然会执行validate方法,如果想不执行,可以将validate改名为validateExecute。那么只有在执行execute时,才会执行validateExecute。