最後是一個套用JSR303的Action sample code。
代碼:
package com.gss.gmo.cao.struts2.interceptor;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import lombok.Setter;
import com.gss.gmo.cao.struts2.interceptor.validation.ValidateFieldConfig;
import com.gss.gmo.cao.struts2.interceptor.validation.ValidateGroupConfig;
import com.gss.gmo.cao.struts2.interceptor.validation.ValidateMessageFormatter;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport implements ValidateMessageFormatter {
private static final long serialVersionUID = 1L;
@Setter
@NotNull
private String username;
@Setter
@Min(1)
@Max(99)
private int age;
@Setter
private UserAction user;
@ValidateGroupConfig
public String execute() {
return SUCCESS;
}
@ValidateFieldConfig(fields = "user")
public String validateFieldConfig() {
return SUCCESS;
}
@Override
public String format(String fieldName, String errorMessage) {
return fieldName + " - " + errorMessage;
}
}
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import lombok.Setter;
import com.gss.gmo.cao.struts2.interceptor.validation.ValidateFieldConfig;
import com.gss.gmo.cao.struts2.interceptor.validation.ValidateGroupConfig;
import com.gss.gmo.cao.struts2.interceptor.validation.ValidateMessageFormatter;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport implements ValidateMessageFormatter {
private static final long serialVersionUID = 1L;
@Setter
@NotNull
private String username;
@Setter
@Min(1)
@Max(99)
private int age;
@Setter
private UserAction user;
@ValidateGroupConfig
public String execute() {
return SUCCESS;
}
@ValidateFieldConfig(fields = "user")
public String validateFieldConfig() {
return SUCCESS;
}
@Override
public String format(String fieldName, String errorMessage) {
return fieldName + " - " + errorMessage;
}
}