执行流程:
1) 首先进行类型转换
2) 然后进行输入校验(执行 validate 方法)
3) 如果在上述过程中出现了任何错误,都不会再去执行 execute
方法,会转向 struts.xml 中该 action 的名为 input 的 result 所对应的页面。
struts 2提供接口:
package com.opensymphony.xwork2;
/**
* Provides an interface in which a call for a validation check can be done.
*/
public interface Validateable {
/**
* Performs validation.
*/
void validate();
}
ActionSupport实现了该接口,但不对validate()方法做任何实现:
package com.opensymphony.xwork2;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
/**
* Provides a default implementation for the most common actions.
* See the documentation for all the interfaces this class implements for more detailed information.
*/
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
...
/**
* A default implementation that validates nothing.
* Subclasses should override this method to provide validations.
*/
public void validate() {
}
}
示例:
/webs2/validate/register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>register</title>
</head>
<body>
<h2><font color="blue">用户注册</font></h2>
<s:actionerror cssStyle="color:red"/><!-- 以红色显示action级别的错误! -->
<s:fielderror cssStyle="color:blue"/><!-- 以蓝色显示field级别的错误! -->
<form action="register.action">
username: <input type="text" name="username" size="20"><br>
password: <input type="password" name="password" size="20"><br>
repassword: <input type="password" name="repassword" size="20"><br>
age: <input type="text" name="age" size="20"><br>
birthday: <input type="text" name="birthday" size="20"><br>
graduation: <input type="text" name="graduation" size="20"><br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
package discovery.struts2.validate;
import java.util.Calendar;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction 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;
}
@Override
public String execute() throws Exception {
System.out.println("execute() invoked!");
return SUCCESS;
}
// validate()先于execute()执行!
@Override
public void validate() {
System.out.println("validate() invoked start!");
if(null == username || username.length() < 4 || username.length() > 6) {
// 添加Action 级别的错误!
this.addActionError("username invalid!");
// 添加Field级别的错误!
this.addFieldError("username", "username invalid in field!");
}
if(null == password || password.length() < 4 || password.length() > 6) {
// Action 级别的错误!
this.addActionError("password invalid");
// Field级别的错误!
this.addFieldError("password", "password invalid in field!");
}
if(null != birthday && null != graduation) {
Calendar c1 = Calendar.getInstance();
c1.setTime(birthday);
Calendar c2 = Calendar.getInstance();
c2.setTime(graduation);
if(!c1.before(c2))
{
this.addActionError("birthday should be before graduation");
}
}
System.out.println("validate() invoked end!");
}
}
<action name="register" class="discovery.struts2.validate.RegisterAction">
<result name="success">/webs2/validate/registerResult.jsp</result>
<!-- validate()校验不通过时,返回input,跳转到相应页面 -->
<result name="input">/webs2/validate/register.jsp</result>
</action>
时
使用struts2标签显示页面:
<s:form action="register.action">
<s:textfield name="username" label="username"/>
<s:password name="password" label="password"/>
<s:password name="repassword" label="repassword"/>
<s:textfield name="age" label="age"/>
<s:textfield name="birthday" label="birthday"/>
<s:textfield name="graduation" label="graduation"/>
<s:submit value="submit"/>
</s:form>
struts2标签将会把错误信息显示到空间上方!
添加theme="simple"属性:将取消struts2提供的样式,方便使用自己的样式,同时又保留了提交时填写的数据!
<s:form action="register.action" theme="simple">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"/>
<s:password name="repassword" label="repassword"/>
<s:textfield name="age" label="age"/>
<s:textfield name="birthday" label="birthday"/>
<s:textfield name="graduation" label="graduation"/>
<s:submit value="submit"/>
</s:form>
<s:form>标签使用post方法提交数据
查看网页源代码:
当输入不符合要求的数据:
提交:
birthday age graduation 提示不符合类型要求!
先执行类型转换,再进行输入校验!
因为没有setAge(String str)方法可被调用,输入数据又不能转换为int,所以age的值还是默认值0.
又因为没有setBirthday(String str)和setGraduation(String str)方法,填入的数据又不能转换为Date,所以属性值仍然是默认值null.
package discovery.struts2.validate;
import java.util.Calendar;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction 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;
}
@Override
public String execute() throws Exception {
System.out.println("execute() invoked!");
return SUCCESS;
}
// validate()先于execute()执行!
@Override
public void validate() {
if(null == username || username.length() < 4 || username.length() > 6)
{
this.addActionError("username invalid");
this.addFieldError("username", "username invalid in field");
}
if(null == password || password.length() < 4 || password.length() > 6)
{
this.addActionError("password invalid");
}
else if(null == repassword || repassword.length() < 4 || repassword.length() > 6)
{
this.addActionError("repassword invalid");
}
else if(!password.equals(repassword))
{
this.addActionError("the passwords not the same");
}
if(age < 10 || age > 50)
{
this.addActionError("age invalid");
}
if(null == birthday)
{
this.addActionError("birthday invalid");
}
if(null == graduation)
{
this.addActionError("graduation invalid");
}
if(null != birthday && null != graduation)
{
Calendar c1 = Calendar.getInstance();
c1.setTime(birthday);
Calendar c2 = Calendar.getInstance();
c2.setTime(graduation);
if(!c1.before(c2))
{
this.addActionError("birthday should be before graduation");
}
}
}
}
当调用 getActionErrors()方法返回 Action 级别的错误信息列表时,
返回的实际上是集合的一个副本而不是集合本身,因此对集合副
本调用 clear()方法清除的依旧是副本中的元素而非原集合中的元
素,此时原集合中的内容没有收到任何的影响。 换句话说,
Action
级别的错误信息列表对开发者来说是只读的。
Field Error 级别的错误信息底层是用 LinkedHashMap 实现的,该
Map 的 key 是 String 类型,value 是 List<String>类型,这就表示一
个 Field Name 可以对应多条错误信息,这些错误信息都放置在
List<String>集合当中
// this.getActionErrors().clear();
// this.getFieldErrors().clear();
this.clearActionErrors();
this.clearFieldErrors();