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

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

问题描述

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

I am using xml based validation.

问题:

I第一次加载页面时不希望验证在输入表单上执行,因为所有字段都是空白的,供用户填写。假设注册表单添加新学生。它应该在我点击按钮后执行。

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为 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>

已编辑:如果我添加的排除方式超过此网址 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()方法来删除验证错误strut愚蠢地发现:

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验证器的东西,然后调用您的验证方法。

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

当您点击表单上的提交时,strut会创建一个MyStudent对象,调用setStudent(...)然后它会逐字段地调用表单的字段getStudent()然后调用MyStudent对象的setFieldName。之后,它通过xml验证器东西验证学生对象,然后调用你的验证方法。

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.

希望这会有所帮助!!!

Hope this helps!!!

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

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