0

我正在使用struts 2,现在我有a、b、c、d 四个动作。四个页面都有一个链接"mobile view",如果点击链接,就会去mobileCheck action设置会话。因此,在执行 mobileCheck 操作后,我想返回到调用操作,例如

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

我的项目中有几十个这样的动作,有没有要设置的变量,我该怎么办,非常感谢。

4

2 回答 2

1

如果所有这些动作都有相同的业务,那么更好的设计是让每个动作都扩展 MobileCheck 动作

修改 MobileCheck Action 以在一个public String mobileView()方法中做业务(而不是执行);

修改 Actions A,B,C,D 以扩展它 ( public class ActionA extends MobileCheck), 他们的业务在execute();

最后,使mobileView()方法返回execute():它将返回execute()您正在运行的 Action 的方法:

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;
    }
}

一个.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 范式 For The Win

于 2013-05-20T09:44:12.227 回答
0

我这样解决了这个问题,我将 url 保存在会话中,并在动作类中,将此会话值获取到nextURL变量,动作结果将使用此变量${nextURL}进行重定向。

1) 在每个 a, b, c, d jsp 中,都包含相同的代码:

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

并保存当前页面 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)在xml中:

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

3)在动作类中:

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;
}

}

它有效,但如果有任何更容易实现的方法,请指出。谢谢。

于 2013-05-27T06:39:03.390 回答