简体   繁体   中英

how to return to the invoke action in struts2

I'm using struts 2, and I have a, b, c, d four action now. There's a link "mobile view" in all of the four pages, if click the link, it will go to mobileCheck action to set the session. So after executing mobileCheck action, I want to return to the invoke action like

1. click link in action a ==>mobileCheck action ==> return to a.action

2. click link in action b ==>mobileCheck action ==> return to b.action

3. click link in action c ==>mobileCheck action ==> return to c.action

4. click link in action d ==>mobileCheck action ==> return to d.action

there're dozens of actions like this in my project, is there any variable to set, What should I do, thanks a lot.

If all of this Actions have the same business in common, then a better design would be making each Action extending MobileCheck action .

Modify MobileCheck Action to do the business in a public String mobileView() method (instead of execute);

Modify Actions A,B,C,D to extend it ( public class ActionA extends MobileCheck ), with their business in execute() ;

Finally, make the mobileView() method returning execute() : it will return the execute() method of the Action you are running:

public class MobileCheck extends ActionSupport {
    public String execute() throws Exception{
        log.debug("MobileCheck execute() should never be invoked");
        return SUCCESS;
    }

    public String mobileView() throws Exception{
        // do something
        log.debug("mobileView() business performed");
        return execute(); /* THIS IS THE TRICK */ 
    }
}

public class ActionA extends MobileCheck {
    public String execute() throws Exception{
        // do something
        log.debug("ActionA execute() is invoked");
        return SUCCESS;
    }
}

public class ActionB extends MobileCheck {
    public String execute() throws Exception{
        // do something
        log.debug("ActionB execute() is invoked");
        return SUCCESS;
    }
}

a.jsp

<s:form>
    <s:submit method="mobileView" action="actionA" 
                      value="call mobileView() and re-execute Action A" />
</s:form>

b.jsp

<s:form>
    <s:submit method="mobileView" action="actionB" 
                      value="call mobileView() and re-execute Action B" />
</s:form>

struts.xml

<action name="actionA" class="foo.bar.ActionA">
    <result>a.jsp</result>
</action>
<action name="actionB" class="foo.bar.ActionB">
    <result>b.jsp</result>
</action>

KISS paradigm For The Win

I solved this problem like this, I save the url in the session, and in the action class, get this session value to nextURL variable, and action result will use this variable ${nextURL} for redirection.

1) In every a, b, c, d jsp, it include the same code:

<ul>
    <li><a href="<s:url action="viewFullSite.action" namespace="" />" id="viewFullSite">View Full Site</a></li>
</ul>

and save the current page uri:

HttpServletRequest req = ServletActionContext.getRequest();
    String uri = "";
    String paramString = "";
    if(ServletActionContext.getActionMapping()!=null){
        String namespace = ServletActionContext.getActionMapping().getNamespace();
        String action = ServletActionContext.getActionMapping().getName();
        uri = namespace + "/" + action + ".action";
        if (req.getQueryString() != null){
            paramString = "?" + req.getQueryString();
            uri += paramString;
        }   
        req.getSession().setAttribute(RequestParam.invoke_uri.toString(), uri);
    }

2) in the xml:

<action name="viewFullSite" class="tlc.ui.action.MobileOrFullViewSwitch" method="switchFullSiteView">
        <result name="success_nextURL" type="redirect">${nextURL}</result>
    </action>

3) in the action class:

public class MobileOrFullViewSwitch  extends TLCAction {

private static final Logger logger = LoggerManager.getLogger(Account.class);
private String nextURL = "";

public String switchFullSiteView(){
    request.getSession().setAttribute(SessionConstants.IGNORE_MOBILEVIEW, "true");
    // set the nextURL property if it was specified
    this.nextURL = (String)request.getSession().getAttribute(RequestParam.invoke_uri.toString());
    if (this.nextURL == null) this.nextURL = "";

    // security check: only support relative URLs; anything else is ignored
    if (!this.nextURL.startsWith("/")) this.nextURL = "";

    if (isEmpty(this.nextURL)) {
        this.nextURL = "/home.action";
    }
    return ResultCode.SUCCESS_NEXT_URL;
}

public String switchMobileView(){
    return ResultCode.SUCCESS_NEXT_URL;
}
public String getNextURL() {
    return nextURL;
}

}

it works, but if there's any way more easy to implement, please point out. thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM