struts 2 注解 校验参数
最近由于项目需要,又来了解Struts2注解。
在我们头得正确领导下,还是把这个东西弄出来了。大家一起参考下。
?
/**
?* @fun
?* @author summer
?* @date 2011-8-8 15:45:41
?* @remark
?*/
@ParentPackage("json-default")
@Namespace("/struts/test")
@Results({
??? @Result(name="success",location="/sp/test/result.jsp"),
??? @Result(name="input",location="/bjsp/test/error.jsp"),? //拦截不符合条件,默认返回input
??? @Result(name="test2",type="json",params={"root","test"})
})
@Validation()
public class TestAction{
??? /**
??? ?*
??? ?*/
??? private static final long serialVersionUID = 1L;
???
??? private TestEntity test = new TestEntity();
??? @Action(value="validate_test1",params={"test.flag","1"})
??? public String test1(){
??? ??? System.out.println("***************test1*************");
??? ??? return SUCCESS;
??? }
??? @Action("validate_test2")
??? @SkipValidation? //该方法使用实体类时不使用校验
??? public String test2(){
??? ??? System.out.println("***************test2*************");
??? ??? return "test2";
??? }
??? @VisitorFieldValidator() //用实体类获取数据时开启验证
??? public TestEntity getTest() {
??? ??? return test;
??? }
??? public void setTest(TestEntity test) {
??? ??? this.test = test;
??? }
}
?
?
/**
?* @fun 实体类
?* @author summer
?* @date 2011-8-8 15:50:46
?* @remark
?*/
@Entity
@Table(name="TBL_TEST",schema="summer")
public class TestEntity implements java.io.Serializable{
??? /**
??? ?*
??? ?*/
??? private static final long serialVersionUID = 1L;
??? private Long id;
??? private String name;
??? private String email;
??? private String phone;
??? private String memo;
??? @Id
??? @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="SUMMER.AUTO_ID")
???????? @SequenceGenerator(name="SUMMER.AUTO_ID",sequenceName="SUMMER.AUTO_ID",allocationSize=1,initialValue=1)
@Column(name="TBTE_PK")
??? public Long getId() {
??? ??? return id;
??? }
??? public void setId(Long id) {
??? ??? this.id = id;
??? }
??? @Column(name="TBTE_NAME")
??? public String getName() {
??? ??? return name;
??? }
??? @RequiredStringValidator(type = ValidatorType.FIELD, message = "You must enter a value for name.")
??? @StringLengthFieldValidator(minLength="6",maxLength="32",shortCircuit =true,trim = true, message = "name length must be between ${minLength} and ${maxLength}, current is ${name.length()}.")//判断长度
??? public void setName(String name) {
??? ??? this.name = name;
??
??? @Column(name="TBTE_EMAIL")
??? public String getEmail() {
??? ??? return email;
??? }
??? @RequiredStringValidator(type = ValidatorType.FIELD, message = "You must enter a value for Email.")
??? @EmailValidator(message = "Email 格式不对")//email校验
??? public void setEmail(String email) {
??? ??? this.email = email;
??? }
??? @Column(name="TBTE_PHONE")
??? public String getPhone() {
??? ??? return phone;
??? }
??? @RequiredStringValidator(type = ValidatorType.FIELD, message = "You must enter a value for phone.")//不能为空
??? @RegexFieldValidator(expression="13\\d{9}",message="电话号码不对")//电话号码校验
??? public void setPhone(String phone) {
??? ??? this.phone = phone;
??? }
???
??? @Column(name="TBTE_MEMO")
??? @JSON(serialize = false)
??? public String getMemo() {
??? ??? return memo;
??? }
??? public void setMemo(String memo) {
??? ??? this.memo = memo;
??? }
???
???
}