Struts 2 @VisitorFieldValidator验证全部表单而非指定表单的问题咨询
各位Struts2的老哥们,我最近在做Struts2的实验项目时碰到了一个关于@VisitorFieldValidator的困惑,想过来请教下大家的解决方案。
先说明下我的环境:使用Struts2 core + convention plugin 6.7.0,Action用的是默认拦截器栈。
下面是我的Action代码:
package name.matthewgreet.basicstrutsexperiments.experiment4.action; import org.apache.struts2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.apache.struts2.interceptor.validation.SkipValidation; import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator; import name.matthewgreet.basicstrutsexperiments.experiment4.form.ArithmeticForm; import name.matthewgreet.basicstrutsexperiments.experiment4.form.DummyForm; @InterceptorRef("defaultStack") @Results({ @Result(name=ActionSupport.ERROR, location="/pages/experiment4/Arithmetic.jsp"), @Result(name=ActionSupport.INPUT, location="/pages/experiment4/Arithmetic.jsp"), @Result(name=ActionSupport.SUCCESS, location="/pages/experiment4/Arithmetic.jsp") }) public class ArithmeticAction extends ActionSupport { private static final long serialVersionUID = -6084392315736424463L; private ArithmeticForm form; private DummyForm dummyForm; private String resultText; @Actions({ @Action("/experiment4/"), @Action("/experiment4/Arithmetic") }) @Override @SkipValidation public String execute() throws Exception { try { int result; if (form == null) { resultText = ""; } else if (form.getOperand2() != null) { switch (form.getOperator()) { case "+": result = form.getOperand1() + form.getOperand2(); break; case "-": result = form.getOperand1() - form.getOperand2(); break; case "/": result = form.getOperand1() / form.getOperand2(); break; case "*": result = form.getOperand1() + form.getOperand2(); break; default: result = 0; break; } resultText = form.getOperand1() + " " + form.getOperator() + " " + form.getOperand2() + " = " + result; } else { resultText = "Operand 2 not set"; } return SUCCESS; } catch (Exception e) { System.out.println("Action failed reason=" + e.getMessage()); return ERROR; } } @Action("/Arithmetic_submit") public String submit() throws Exception { try { int result; if (form.getOperand2() != null) { switch (form.getOperator()) { case "+": result = form.getOperand1() + form.getOperand2(); break; case "-": result = form.getOperand1() - form.getOperand2(); break; case "/": result = form.getOperand1() / form.getOperand2(); break; case "*": result = form.getOperand1() + form.getOperand2(); break; default: result = 0; break; } resultText = form.getOperand1() + " " + form.getOperator() + " " + form.getOperand2() + " = " + result; } else { resultText = "Operand 2 not set"; } return SUCCESS; } catch (Exception e) { System.out.println("Action failed reason=" + e.getMessage()); return ERROR; } } @VisitorFieldValidator(appendPrefix = true) public ArithmeticForm getForm() { if (form == null) { form = new ArithmeticForm(); } return form; } @VisitorFieldValidator(appendPrefix = true) public DummyForm getDummyForm() { if (dummyForm == null) { dummyForm = new DummyForm(); } return dummyForm; } public String getResultText() { return resultText; } }
然后是我的ArithmeticForm代码:
package name.matthewgreet.basicstrutsexperiments.experiment4.form; import java.io.Serializable; import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator; import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator; import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator; public class ArithmeticForm implements Serializable { private static final long serialVersionUID = -5287580238710655353L; private int operand1; private String operator; private Integer operand2; @ConversionErrorFieldValidator(message = "Operand 1 must be a number!", repopulateField = true, shortCircuit = true) @RequiredFieldValidator(message = "Operand 1 is required") public int getOperand1() { return operand1; } public void setOperand1(int operand1) { this.operand1 = operand1; } @RequiredStringValidator(message = "Operator is required") public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } @ConversionErrorFieldValidator(message = "Operand 2 must be a number!", repopulateField = true, shortCircuit = true) @RequiredFieldValidator(message = "Operand 2 is required") public Integer getOperand2() { return operand2; } public void setOperand2(Integer operand2) { this.operand2 = operand2; } @Override public String toString() { return "ArithmeticForm [operand1=" + operand1 + ", operator=" + operator + ", operand2=" + operand2 + "]"; } }
(注:DummyForm是我加的一个测试用表单类,结构和ArithmeticForm类似,也给字段加了对应的验证注解)
现在说下我的问题:
我在Action里给ArithmeticForm和DummyForm的getter方法都加了@VisitorFieldValidator(appendPrefix = true)注解,本来是想让Struts2验证表单字段,但现在发现一个问题:当我提交ArithmeticForm的表单数据时,Struts2不仅会验证ArithmeticForm的字段,还会去验证DummyForm的字段——可DummyForm只是我用来测试的,这次请求根本没用到它,结果导致了不必要的验证错误。
我本来以为@VisitorFieldValidator只会验证当前请求关联的表单对象,或者可以配置只验证指定的表单,但现在看来只要加了这个注解的getter对应的表单都会被触发验证。
想请教下各位,有没有办法让Struts2只验证我指定的表单(比如这次请求里用到的ArithmeticForm),而不是所有带有@VisitorFieldValidator注解的表单呢?
内容来源于stack exchange

