Struts 2 validate specific action in xml

前端 未结 3 478
鱼传尺愫
鱼传尺愫 2021-01-26 13:30

I want to validate specific method only in action class. Action method is this.

public String add() throws Exception {

         


        
相关标签:
3条回答
  • 2021-01-26 13:58

    I had similar problems but then these are couple of things that i rectified :

    1) DOCTYPE in the yourAction-yourAlias-validation.xml should be

        <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
    

    2)Created a custom interceptor to exclude the methods that need not be validated.Below is a sample for my candidate registration where i had to skip validation for district method which fetched districts for a state.

     <interceptor-stack name="clientValidation">
      <interceptor-ref name="basicStack"/>
       <interceptor-ref name="validation">
         <param name="excludeMethods">input,district</param>
        </interceptor-ref>
      <interceptor-ref name="workflow"/>
    </interceptor-stack>
    
    
    <package name="registration" namespace="/candidate" extends="struts-default">
                <action name="candidateRegistration" class="com.app.action.CandidateAction">
                <interceptor-ref name="clientValidation"/>
                    <result name="input">/jsps/student/studentregistration.jsp</result>
                    <result name="failure1">/jsps/student/registrationfailure.jsp</result>
                    <result name="cancelRegistration">/jsps/login.jsp</result>
                    <result name="district">/jsps/includes/dropdown_districts_regoffice.jsp</result>
                </action>
            </package>
    
    0 讨论(0)
  • 2021-01-26 14:04

    To exclude some methods from validation you can override parameters of the validation interceptor. For example in interceptor stack you can write

    <interceptor-ref name="validation">
      <param name="excludeMethods">input,back,cancel,browse,yourmethod</param>
    </interceptor-ref>
    

    in the action configuration you can use above code and

    <interceptor-ref name="defaultStack">
      <param name="validation.excludeMethods">input,back,cancel,browse,yourmethod</param>
    </interceptor-ref>
    

    Note yourmethod is a method name.

    You can specify a validation.xml for only one action by a postfix of action name in this file. For example AcrionClass-add-validation.xml will work only for the action name add in the action config. Note here add is an action name, not a method name.

    Often, the name attribute matches the method name, but they may also differ.

    0 讨论(0)
  • 2021-01-26 14:04

    Use an action alias: map different methods of an Action to different action aliases in struts.xml, and instead of the single yourAction-validation.xml file, add a yourAction-yourAlias-validation.xml for the validators you want to perform for that method only.

    Read more here.

    EDIT

    No its not validation anything now.

    <validators> 
        <field name="aoName">
            <field-validator type="required"> 
                <message>You cannot leave the aoName address field empty.</message>
            </field-validator> 
        </field>
    </validators>
    

    name is OraganisationAction-add-validation.xml and put it with action class package. Is there anything to enable validation in struts.xml ?

    If you have a correctly configured (for example the default) Interceptor Stack, validation is already enabled. If you are using a file named OraganisationAction-add-validation.xml, that means that:

    • your action CLASS must be named OraganisationAction.java;
    • your action mapping in struts.xml must be the following:

      <action name="add" class="your.package.OraganisationAction" method="add">
          <result>yourResult.jsp</result>
      </action>
      

    Look for typos, check your Interceptor Stack, and it will work automatically.

    0 讨论(0)
提交回复
热议问题