我正在尝试在我的Struts2 Web应用程序(使用
JQuery插件)中使用Ajax验证,我有一个问题.
我在使用@Validations注释时遇到了问题所以我只是覆盖了validate()方法,并且在验证过程中所有的xhtml表单都可以正常工作.字段错误很好地收到并打印在gui中.
事实是,在验证表单时,会向服务器发送第二个POST以启动Action.但是,我不知道为什么,框架没有设置模型的属性,并且在执行Action之后生成HTML响应时,框架无法访问属性(仅当我使用jsonValidationWorkflowStack时).
我创建了一个简单的例子:
那个行动
@Namespace("/actions") @ParentPackage("actions") @InterceptorRef(value = "jsonValidationWorkflowStack") public class ActionTest extends ActionSupport implements ModelDriven<ModelTest>,ParameterAware { private static final long serialVersionUID = 1L; private ModelTest model; private Map<String,String[]> parameters; public ActionTest() { model = new ModelTest(); } @Action(value = "actionTest",results = { @Result(name = SUCCESS,location = "/jsp/win.jsp"),@Result(name = ERROR,location = "/jsp/loose.jsp"),}) public String executeAction1() throws Exception { System.out.println("pass: "+getModel().getPass()); //the field is always null if ("test".equals(getModel().getPass())) return SUCCESS; return ERROR; } @Override public ModelTest getModel() { return model; } @Override public void validate() { String field; if (parameters.get("pass") != null) { field = parameters.get("pass")[0]; if (field == null || field.isEmpty()) addFieldError("pass","the field is not set"); else if (!"test".equals(field)) addFieldError("pass","the password is \"test\""); } } @Override public void setParameters(Map<String,String[]> arg0) { this.parameters = arg0; } }
该模型
public class ModelTest { private String pass; public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }
表格
<s:form action="actionTest" theme="xhtml"> <s:textfield name="pass"/> <sj:submit validate="true"/> </s:form>
在这个例子中:
– 即使密码是“test”,我总是被重定向到loose.jsp
– 如果我想在这个动作重定向的另一个jsp中打印一个,我什么都没看到(即使我在模型中设置了一个默认值).
你能帮我一把吗?
PS:对不起我的英语不好:/
解决方法
实际上模型驱动的拦截器带有defaultStack
<interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> <interceptor-ref name="i18n"/> <interceptor-ref name="chain"/> <interceptor-ref name="debugging"/> <interceptor-ref name="profiling"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkBox"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,browse</param> </interceptor-ref> </interceptor-stack>
并且jsonValidationWorkflowStack不包含只包含basicStack
<interceptor-stack name="jsonValidationWorkflowStack"> <interceptor-ref name="basicStack"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,cancel</param> </interceptor-ref> <interceptor-ref name="jsonValidation"/> <interceptor-ref name="workflow"/> </interceptor-stack>
这是basicStack
<interceptor-stack name="basicStack"> <interceptor-ref name="exception"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> <interceptor-ref name="checkBox"/> <interceptor-ref name="multiselect"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> </interceptor-stack>
尝试添加
@Namespace("/actions") @ParentPackage("actions") @InterceptorRefs({ @InterceptorRef("jsonValidationWorkflowStack"),@InterceptorRef("defaultStack") })