2016-10-07 2 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 작업으로 전달하려고 시도합니다. 다음 내 web.xml 파일입니다 :

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 오류를주고있다 :

HTTP 상태 404 - /ccpserver/logon.do 유형 상태 보고서 message /ccpserver/logon.do 설명 요청한 리소스 (/ccpserver/logon.do)를 사용할 수 없습니다. 제이보스 웹/2.1.3.GA

직접 URL http://localhost:8080/ccpserver/logon.do를 치는 동안, 그것은 잘 작동합니다.

답변

1

은 다음 서블릿 필요하지 않습니다. 로그 오프 한 후 리디렉션 유형 결과를 사용하여 로그인 작업으로 리디렉션해야합니다.

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.


당신은 뭔가

<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이 필요합니다. 그 로그 아웃에 대한
및 매핑은 다음과 같이해야한다 : 당신이 valueStack에서 firm 값이있는 경우

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로 리디렉션되지 코딩 할 수 있습니다. 우리가 로그 아웃 할 때, URL은 struts.xml 설정에 의해 제공되고 $ {firmName} .logon으로 보내지는 /logoff.do입니다. 이것은 LogonServlet에 의해 제공되는 새로운 URL입니다.이 URL은 /logon.do에 전달하지만 실패합니다. – Prashant

관련 문제