我是struts2的新手,如何使用注释实现动作(方法)级别验证。 我的动作类有两个方法,添加和编辑。 当调用添加操作然后我想验证所有字段,但是当我调用编辑操作时,我不想验证所有字段。 所以我在添加和编辑方法上添加了基于注释的验证。 它正在添加,但是当我提交编辑时,它正在尝试验证所有字段(添加+编辑方法)。 我添加了类级别的validateAnnotatedMethodOnly注释,但仍然给出了同样的问题。
@InterceptorRefs({ @InterceptorRef(value = "defaultStack", params = { "validation.validateAnnotatedMethodOnly", "true",
"validation.excludeMethods", "add, edit" }) })
我该如何解决这个问题,请帮帮我
=============================================== ==
这是完整的代码
@InterceptorRefs({ @InterceptorRef(value = "defaultStack", params = { "validation.validateAnnotatedMethodOnly", "true" }) })
public class CustomerAction extends ActionSupport implements SessionAware, Preparable, ModelDriven<Customer> {
private static final Logger logger = Logger.getLogger(CustomerAction.class);
private AtomicLong atomicLong = null;
private String startString;
private Customer customer = new Customer();
protected Map session;
public Customer getModel() {
return customer;
}
public Map getSession() {
return session;
}
public void setSession(Map sess) {
this.session = sess;
}
@Override
@SkipValidation
public String execute() {
logger.info(" in execure action !! ");
return SUCCESS;
}
@Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
if (!session.isEmpty()) {
logger.info("inside prepare ::::::");
customer = (Customer) session.get("CUSTOMER");
// logger.info("---------------" + customer.getEmail());
CustomerDAO customerDAO = CustomerDAOFactory.getCustomerDAO();
customer = customerDAO.getCustomerDetails(customer.getEmail(), "");
}
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "company_name", type = ValidatorType.FIELD, message = "Company name is required"),
@RequiredStringValidator(fieldName = "mobile", type = ValidatorType.FIELD, message = "Mobile is required"),
@RequiredStringValidator(fieldName = "email", type = ValidatorType.FIELD, message = "Email is required") }, emails = { @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "email", message = "Please enter valid email.") })
// ,@RequiredStringValidator(fieldName = "password", type = ValidatorType.FIELD, message = "Password is required")
public String addCustomer() {
// add customer code
return SUCCESS;
}
public String editCustomer() {
//edit customer code
return SUCCESS;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
答案 0 :(得分:0)
我得到了解决方案。感谢所有回复。 解决方法如下,我只在struts.xml文件中添加了验证注释方法,然后才能正常工作。
<action name="createCustomer" class="com.struts.action.CustomerAction" method="createCustomer">
<interceptor-ref name="defaultStack">
<param name="validation.validateAnnotatedMethodOnly">true</param>
<param name="validation.excludeMethods">editProfile</param>
</interceptor-ref>
<result name="success" type="tiles">/register.tiles</result>
<result name="input" type="tiles">/register.tiles</result>
</action>
<action name="editProfile" class="com.struts.action.CustomerAction" method="editProfile">
<interceptor-ref name="defaultStack">
<param name="validation.validateAnnotatedMethodOnly">true</param>
<param name="validation.excludeMethods">createCustomer</param>
</interceptor-ref>
<result name="success" type="tiles">/welcome.tiles</result>
<result name="input" type="tiles">/welcome.tiles</result>
</action>