struts2 在页面加载时禁用验证 [英] struts2 disable validation on page load

查看:21
本文介绍了struts2 在页面加载时禁用验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于 xml 的验证.

I am using xml based validation.

问题:

我不想在第一次加载页面时对输入表单执行验证,因为所有字段都是空白供用户填写.假设注册表单添加新学生.一旦我单击按钮,它就会执行.

I don't want validation to perform on input form when page loaded first time because all the fields are blank for user to fill in. Suppose for register form to add new student. It should perform once I click the button.

注意: 即使我对按钮单击进行验证,我也希望保留相同的 url.新表单 URL 是 http://localhost:8000/Struts2_Spring_Crud/student/add 并且如果验证失败,甚至 URL 应该相同.

Note: I want to retain same url even when I do the validation on button click. New form URL is http://localhost:8000/Struts2_Spring_Crud/student/add and if validation fail even than the url should be same.

struts.xml

<default-action-ref name="list"/>
        <action name="list" class="com.myapp.actions.StudentAction" method="getAllStudents">
            <!--<interceptor-ref name="myInterceptor"/>-->
            <result name="success" type="tiles">/student.list.tiles</result>
        </action>

    <!--<action name="add">
                <result type="tiles">/student.edit.tiles</result>
            </action>-->
            <action name="add" class="com.myapp.actions.StudentAction" method="insertOrUpdateStudent">
                <result name="success" type="redirectAction">list</result>
                <result name="input" type="tiles">/student.edit.tiles</result>
            </action>

输入表单

<s:fielderror/>
    <s:form action="add" method="POST">

        <s:label name="name" value="Name *"/>
        <s:textfield name="student.name" value="%{student.name}"/>
        <s:fielderror fieldName="student.name"/>

        <s:label name="age" value="Age *"/>
        <s:textfield name="student.age" value="%{student.age}"/>

        <s:submit name="saveForm" value="#title"/>
    </s:form>

EDITED: 如果我添加 excludeMethods 比这个 url http://localhost:8000/Struts2_Spring_Crud/student/add 发送我到 http://localhost:8000/Struts2_Spring_Crud/student/list 并且我的添加表单没有显示.

EDITED: If I am adding excludeMethods than this url http://localhost:8000/Struts2_Spring_Crud/student/add is sending me to http://localhost:8000/Struts2_Spring_Crud/student/list and my add form is not showing.

 <action name="add" class="com.myapp.actions.StudentAction" method="insertOrUpdateStudent">
            <interceptor-ref name="validation">
                <param name="excludeMethods">input,back,cancel,browse</param>
            </interceptor-ref>
            <result name="success" type="redirectAction">list</result>
            <result name="input" type="tiles">/student.edit.tiles</result>
        </action>

推荐答案

我遇到了这个问题,终于找到了一种解决方法,可以解决您只希望一个操作来处​​理所有事情并在浏览器上保持相同 URL 的情况:

I went through this problem and I finally found a work around for the case where you want only one action to handle everything and keep the same URL on the browser:

http://localhost:8000/Struts2_Spring_Crud/student/add

struts.xml 只会是:

The struts.xml would only be:

<action name="add" class="com.myapp.actions.StudentAction" method="insertOrUpdateStudent">
    <result name="success" type="redirectAction">list</result>
    <result name="input" type="tiles">/student.edit.tiles</result>
</action>

第一次加载页面时,首先调用与添加"操作关联的类.然后班级中的现场学生为空.然后你可以依靠这个事实来清除 strut 所做的验证.

The first time your page loads, the class associated to "add" action is first called. Then the field student from the class is null. Then you can rely on that fact to clear the validation that strut did.

所以你应该在你的动作类中添加一个 validate() 方法来删​​除那些愚蠢地发现的验证错误:

So you should add a validate() method to your action class that will delete the validation errors that strut stupidly found:

public class StudentAction extends ActionSupport{
   MyStudent student;

   public MyStudent getStudent(){
       return student;
   }
   public void setStudent(MyStudent student){
       this.student=student;
   }

   public String execute(){
       if (student==null) return INPUT; //First time page loads. We show page associated to INPUT result.
       return SUCCESS; // If student is not null and execute was called it means that everything went fine, we should return SUCCESS and go to the page associated to the SUCCESS result.
   }

   public String insertOrUpdateStudent(){
       if (student==null) return INPUT;  
       return SUCCESS;   
   }

 .....
   @Override
   public void validate(){ //
      if (student==null){ //First time page loads, student is null.
         setFieldErrors(null); //We clear all the validation errors that strut stupidly found since there was not form submission.
      }
   }
}

Struts 首先通过 xml 验证器验证表单,然后调用您的 validate 方法.

Struts first validates the form by the xml validator stuff and then calls your validate method.

当您在表单上点击提交时,strut 创建一个 MyStudent 对象,调用 setStudent(...) 然后它逐个字段调用 getStudent() 然后调用 set"FieldName"MyStudent 对象.之后,它通过 xml 验证器验证学生对象,然后调用您的 validate 方法.

When you hit submit on the form, then strut creates a MyStudent object, call the setStudent(...) and then it goes field by field of the form calling the getStudent() and then calling the set"FieldName" of MyStudent object. After that, it validates the student object by the xml validator stuff and then calls your validate Method.

如果在调用 validate() 方法后仍有错误,则 strut 将返回INPUT"结果,而无需在您的情况下调用 execute() 或 insertOrUpdateStudent() 方法.

If after calling the validate() method there are still errors, then strut will return with an "INPUT" result without calling the execute() or the insertOrUpdateStudent() method in your case.

希望对你有帮助!!!

这篇关于struts2 在页面加载时禁用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆