2016-10-07 29 views
2

プロジェクトには、ログアウト時に呼び出されるサーブレットがあります。Struts2でログオフ後リクエストをリダイレクトする方法

public class LogonServlet extends HttpServlet { 

private static final long serialVersionUID = -4899047924930198118L; 

public void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
    setFirmAndRedirect(request, response); 
} 

public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
    setFirmAndRedirect(request, response); 
} 

private void setFirmAndRedirect(HttpServletRequest request, 
    HttpServletResponse response) throws ServletException, IOException { 
    String firm; 
    String serverPath = request.getServletPath(); 

    firm = serverPath.replaceAll("/", "").replaceAll(".logon", "").trim() 
        .toLowerCase(); 

    request.setAttribute("firm", firm); 

    RequestDispatcher dispatcher = request.getRequestDispatcher("/logon.do"); 
    dispatcher.forward(request, response); 
} 
} 

サーブレットは、要求をstrutsのlogon.doアクションに転送しようとします。 LogonServletは、以下のstruts.xmlごとにログアウト要求を提供するために使用される

<web-app> 
    <context-param> 
    <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name> 
    <param-value>/WEB-INF/tiles-defs.xml</param-value> 
    </context-param> 

    <filter> 
    <filter-name>HibernateSessionFilter</filter-name> 
    <filter-class>com.rolfeandnolan.ccp.filters.HibernateSessionFilter</filter-class> 
    </filter> 

    <!-- Struts 2 --> 
    <filter> 
    <filter-name>struts2</filter-name> 
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    </filter> 

    <!-- Should only apply to Alerts Direct requests --> 
    <filter-mapping> 
    <filter-name>HibernateSessionFilter</filter-name> 
    <url-pattern>*.do</url-pattern> 
    </filter-mapping> 

    <filter-mapping> 
    <filter-name>struts2</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <servlet> 
    <servlet-name>logon</servlet-name> 
    <servlet-class>com.rolfeandnolan.ccp.servlets.LogonServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>logon</servlet-name> 
    <url-pattern>*.logon</url-pattern> 
    </servlet-mapping> 

:フォワーディングで

<action name="logoff" 
      class="com.rolfeandnolan.ccp.struts.actions.LogoffAction"> 
      <param name="allowedRoles">admin,margin,alerts,limitadmin,workfloweditor,eval,theocalc,stress,alertscustomer,useradmin</param> 
     <result name="success">/logon.jsp</result> 
     <result name="firmlogon" type="dispatcher"> 
     <param name="location">${firm}.logon</param> 
     </result> 

    </action> 

、それは404エラーを与えている:

私のweb.xmlファイルを以下に示し

HTTPステータス404 - /ccpserver/logon.do タイプステータスレポート メッセージ/ccpserver/logon.do 説明要求されたリソース(/ccpserver/logon.do)は使用できません。 のJBossのWeb/2.1.3.GA

直接URL http://localhost:8080/ccpserver/logon.doを打つが、それは正常に動作しています。

答えて

1

あなたはvalueStackfirm値を持っているなら、あなたは、サーブレットは必要ありません。ログオフ後、リダイレクトタイプの結果を使用してログインアクションにリダイレクトする必要があります。

Redirect Action Result

This result uses the ActionMapper provided by the ActionMapperFactory to redirect the browser to a URL that invokes the specified action and (optional) namespace. This is better than the ServletRedirectResult because it does not require you to encode the URL patterns processed by the ActionMapper in to your struts.xml configuration files. This means you can change your URL patterns at any point and your application will still work. It is strongly recommended that if you are redirecting to another action, you use this result rather than the standard redirect result.

See examples below for an example of how request parameters could be passed in.

<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters"> 
    <!-- Pass parameters (reportType, width and height) --> 
    <!-- 
    The redirectAction url generated will be : 
    /genReport/generateReport.action?reportType=pie&amp;width=100&amp;height=100#summary 
    --> 
    <action name="gatherReportInfo" class="..."> 
     <result name="showReportResult" type="redirectAction"> 
     <param name="actionName">generateReport</param> 
     <param name="namespace">/genReport</param> 
     <param name="reportType">pie</param> 
     <param name="width">100</param> 
     <param name="height">100</param> 
     <param name="empty"></param> 
     <param name="suppressEmptyParameters">true</param> 
     <param name="anchor">summary</param> 
     </result> 
    </action> 
</package> 

See ActionMapper for more details.


あなたはそれが同じURLにリダイレクトされていない何か

<result name="firmlogon" type="redirectAction"> 
    <param name="actionName">logon</param> 
    <param name="namespace">/</param> 
    <param name="firm">${firm}</param> 
</result> 

+0

解決策は私のために働いていましたが、まだ私はサーブレットからのフォワードがなぜ機能していないのか理解できません。 – Prashant

+0

サーブレットに転送しないでください。http://stackoverflow.com/a/31483491/573032を参照してください。 –

0

/logon.doを使用している場合、ログインのためにLogonServletがあります。後で同じサーブレットのsetFirmAndRedirectに同じurlへの転送要求がありますか?すなわち、/logon.do

あなたはログアウトコードを含むサーブレットである/logoutのような別のURLが必要です。
とそのログアウトのためのマッピングは以下のようにする必要があります:

RequestDispatcher dispatcher = request.getRequestDispatcher("logout"); 
dispatcher.forward(request, response); 

<servlet> 
    <servlet-name>logout</servlet-name> 
    <servlet-class>com.rolfeandnolan.ccp.servlets.logout</servlet-class> 
    </servlet> 
<servlet-mapping> 
    <servlet-name>logout</servlet-name> 
    <url-pattern>/logout</url-pattern> 
</servlet-mapping> 
+0

をコーディングすることができます。ログアウトすると、URLはstruts.xml設定によって提供され、$ {firmName} .logonにディスパッチされた/logoff.doです。これはLogonServletによって提供される新しいURLで、これを/logon.doに転送するには失敗しますが失敗します – Prashant

関連する問題