Search code examples
struts2

Struts2 Usage of RequestAware Interface


If my action class is as per below:

<!-- language: lang-java -->

package org.tutorial.struts2.action;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import org.tutorial.struts2.service.TutorialFinder;
import com.opensymphony.xwork2.Action;

public class TutorialAction implements Action, RequestAware {
    private String language;
    private String bestTutorialSite;

    public String execute() {
        System.out.println(language);
        setBestTutorialSite(new TutorialFinder().getBestTutorialSite(language));
        System.out.println(bestTutorialSite);       
        if (getBestTutorialSite().contains("Java"))
            return SUCCESS;
        else
        return ERROR;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getBestTutorialSite() {
        return bestTutorialSite;
    }

    public void setBestTutorialSite(String bestTutorialSite) {
        this.bestTutorialSite = bestTutorialSite;
    }

    @Override
    public void setRequest(Map<String, Object> requestObj) {
        System.out.println(bestTutorialSite);
        requestObj.put("message", bestTutorialSite);
    }

}

When this action is invoked prior to the execute method, the language is already populated by Struts2 framework. In the execute method the setBestTutorialSite method is to populate the private field bestTutorialSite.

Now I thought of setting this private field bestTutorialSite into the request attributes (in the setRequest method). However I notice that this method is invoked first before any private field (like the language) is populated. Hence in the setRequest method, the system print of bestTutorialSite is always null.

I thought I was able to set this attribute with the bestTutorialSite (captured from the execute method) prior to calling the JSP page.

I don't think i fully grasp the understanding of Struts2 flow - obviously! :OP

Please help. Thanks.


Solution

  • I assume you're using defaultStack which looks like this:

    <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,back,cancel,browse</param>
        </interceptor-ref>
    </interceptor-stack>
    

    as you can see, servletConfig interceptor is before params interceptor which means first Request will be set on your action (with servletConfig) and then your action will be populated with request parameters (with params).

    What you want to achieve is to change the order of the interceptors which can be harmful when used in wrong way.