سؤال

I am new to struct 2.0. I am trying to run my first small apllication with interceptor and want to use validation for only excecute method.

But it call validate function before both populate and excecute method. Can anyone please tell me what i am missing.

Following is my SampleAction class.

package demo;

import com.opensymphony.xwork2.ActionSupport;

public class SampleAction extends ActionSupport {

private static final long serialVersionUID = 1L;
    public void validate()
    {
        System.out.println("validate() method called");
    }

    public String populate()
    {
        System.out.println("populate() method called");
        return "populate";
    }

    public String execute()
    {
        System.out.println("execute() method called");
        return SUCCESS;
    }
}

And Following is my struct.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" extends="struts-default">       
        <action name="*Sample" method="{1}" class="demo.SampleAction">
        <interceptor-ref name="defaultStack" >
        <param name="validation.excludeMethods"> populate</param>
        <result name="populate">/first.jsp</result>
        <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>
هل كانت مفيدة؟

المحلول

Change part where you declare excludeMethods parameters to that:

<interceptor-ref name="defaultStack">
    <param name="validation.excludeMethods">populate</param>
</interceptor-ref>

Update

From ValidationInterceptor javadoc

alwaysInvokeValidate - Defaults to true. If true validate() method will always be invoked, otherwise it will not.

programmatic - Defaults to true. If true and the action is Validateable call validate(), and any method that starts with "validate".

declarative - Defaults to true. Perform validation based on xml or annotations.

Also read about validation in Struts2 http://struts.apache.org/2.x/docs/validation.html.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top