一般情况下,只要是能在配置文件中的都可以通过注解的方式注解到Java代码中(当前的猜想),对于Struts而言,尽管这些配置都放在了注解当中,但是他们的机理还是一样的,还是要用拦截器拦截,然后校验,xml与注解方式的区别主要体现在Java的校验代码中,xml我的理解是通过加载配置文件然后根据配置文件中的数据与对应的action进行校验,而注解则是将action以反射的方式进行提取然后校验,所以本质上是一样的,只是换了一种方式罢了(理解可能有误)。
struts的validate验证其实就是validation拦截器来进行的,我们可以在这里指定使用defaultStack拦截器,并且在里面指定一个参数,名字是validation.excludeMethods,前面说的是验证拦截器的名称validation (我们可以在struts2核心包下的struts-default.xml里看到),后面就是指定的参数名称,这个参数就是指定不被验证的有哪些方法,我们在后面加上toLoginView就可以了,使用逗号隔开,这样就不会被验证了。
@Validations(
requiredStrings = {
@RequiredStringValidator(fieldName = "admin.username", message = "用户名不允许为空!"),
@RequiredStringValidator(fieldName = "admin.password", message = "密码不允许为空!"),
@RequiredStringValidator(fieldName = "admin.email", message = "E-mail不允许为空!")
},
requiredFields = {
@RequiredFieldValidator(fieldName = "admin.isAccountEnabled", message = "是否启用不允许为空!")
},
stringLengthFields = {
@StringLengthFieldValidator(fieldName = "admin.username", minLength = "2", maxLength = "20", message = "用户名长度必须在${minLength}到${maxLength}之间!"),
@StringLengthFieldValidator(fieldName = "admin.password", minLength = "4", maxLength = "20", message = "密码长度必须在${minLength}到${maxLength}之间!")
},
emails = {
@EmailValidator(fieldName = "admin.email", message = "E-mail格式错误!")
},
regexFields = {
@RegexFieldValidator(fieldName = "admin.username", expression = "^[0-9a-z_A-Z\u4e00-\u9fa5]+$", message = "用户名只允许包含中文、英文、数字和下划线!")
}
)
package com.belstar.action;
import Java.util.ArrayList;
import java.util.List;
import org.apache.struts2.interceptor.validation.SkipValidation;
import com.belstar.bean.Book;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.DateRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
//@Validation()
public class BookAction extends ActionSupport {
private static final long serialVersionUID = -38241432793476229L;
public static List<Book> bookList = new ArrayList<Book>();
private String title;
private Book book;
// 添加书籍页面
@SkipValidation
public String initAdd() {
return "initAdd";
}
// 添加书籍
@Validations(requiredStrings = {
@RequiredStringValidator(type = ValidatorType.SIMPLE, trim = true, fieldName = "book.name", message = "请输入书籍名称. *"),
@RequiredStringValidator(type = ValidatorType.SIMPLE, trim = true, fieldName = "book.author", message = "请输入书籍作者. *") }, dateRangeFields = { @DateRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "book.publishedDate", min = "1900-01-01", max = "2200-01-01", message = "日期必须大于 ${min} 小于 ${max}, 当前值为 ${book.publishedDate}.") })
public String add() {
bookList.add(book);
title = "<br/><br/>添加书籍成功<br/><br/>";
return "success";
}
// 书籍列表
@SkipValidation
public String list() {
return "list";
}
// 清空书籍列表
@SkipValidation
public String clear() {
bookList.clear();
title = "<br/><br/>清空书籍列表成功<br/><br/>";
return "list";
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public List<Book> getBookList() {
return bookList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
同时需要指出,若要实现在客户端验证要在<struts:form>标签中加入validate="true"属性,同时在struts.xml文件中需要配置input验证失败对应的页面。