1

我在Struts 2中写了一些实现Struts 1功能的方法

支柱 1 ActionForward

new ActionForward("/home/ShowPage.do?page=edit",true);

我在 Struts 2 中所做的相同:

public String myActionForward(String url,boolean isRedirect) throws Exception
{
    if(isRedirect){
        response.sendRedirect(url);
    }else{
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
    }
    return null;
}
myActionForward("/home/ShowPage.do?page=edit",true);

Struts 1:获取结果路径:

String url = mapping.findForward(result).getPath();

我在 Struts 2 中所做的相同:

public String myGetPath(String result) throws Exception
{
    Map<String,ResultConfig> results = ActionContext.getContext().getActionInvocation().getProxy().getConfig().getResults();
    ResultConfig rc = results.get(result);
    String path = rc.getParams().get("location");
    return path;
}

String url = myGetPath(result);

就像我在 Struts 2 中写了一些替代方法一样。

上述替代方法工作正常。但我需要知道,这些方法是否会在将来产生任何问题,或者我需要更改上述方法中的任何内容以实现与 Struts 1 中相同的效果。

4

1 回答 1

2

您应该使用 Struts2Result实现而不是直接使用 servlet 的东西,直到您知道自己在做什么。应该使用 or 和结果类型来代替直接的servlet redirectAPI redirectActiondispatcherStruts 可能会包装 servlet 内容以进行某些功能更改,并且将来您可能会得到无法工作的代码。

第二种方法是有效的,因为它使用 Struts2 API。但是,您需要了解如何在来自ActionMapping.

于 2013-08-12T20:00:51.403 回答