2013-08-12 35 views
1

我已經寫了一些方法來實現的Struts 1種功能的Struts 2另類一些的Struts Struts中1種方法2

的Struts 1 ActionForward

同我在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中的相同。

回答

2

您應該使用Struts2 Result實現,而不是直接使用servlet的東西知道你在做什麼。有redirectredirectActiondispatcher應該使用的結果類型,而不是直接的servlet API。 Struts可能會包裝servlet的某些內容以進行某些功能更改,您將來可能無法使用代碼。

第二種方法是有效的,因爲它使用Struts2 API。但是,您需要查看how to create URLs programmatically in the actions from ActionMapping