Struts2 注销后如何重定向请求
How to redirect request after logoff in Struts2
该项目有一个 servlet,当有人注销时会调用它。
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);
}
}
servlet 尝试将请求转发到 struts 的 logon.do 操作。
以下是我的 web.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>
LogonServlet 用于根据以下 struts.xml:
为注销请求提供服务
<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
输入状态报告
消息/ccpserver/logon.do
描述请求的资源(/ccpserver/logon.do)不可用。
JBoss Web/2.1.3.GA
直击 URL http://localhost:8080/ccpserver/logon.do
时,它工作正常。
您有 LogonServlet
的登录权限,您正在使用 /logon.do
。稍后在 setFirmAndRedirect
方法中的同一个 servlet 为什么要将请求转发到同一个 url
?即 /logon.do
您需要另一个 url,例如 /logout
,它是一个包含您的注销代码的 servlet。
该注销的映射应如下所示:
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>
如果 valueStack
中有 firm
值,则不需要 servlet。注销后,您应该使用重定向类型结果重定向到登录操作。
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&width=100&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>
该项目有一个 servlet,当有人注销时会调用它。
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);
}
}
servlet 尝试将请求转发到 struts 的 logon.do 操作。 以下是我的 web.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>
LogonServlet 用于根据以下 struts.xml:
为注销请求提供服务 <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 输入状态报告 消息/ccpserver/logon.do 描述请求的资源(/ccpserver/logon.do)不可用。 JBoss Web/2.1.3.GA
直击 URL http://localhost:8080/ccpserver/logon.do
时,它工作正常。
您有 LogonServlet
的登录权限,您正在使用 /logon.do
。稍后在 setFirmAndRedirect
方法中的同一个 servlet 为什么要将请求转发到同一个 url
?即 /logon.do
您需要另一个 url,例如 /logout
,它是一个包含您的注销代码的 servlet。
该注销的映射应如下所示:
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>
如果 valueStack
中有 firm
值,则不需要 servlet。注销后,您应该使用重定向类型结果重定向到登录操作。
Redirect Action Result
This result uses the
ActionMapper
provided by theActionMapperFactory
to redirect the browser to a URL that invokes the specified action and (optional) namespace. This is better than theServletRedirectResult
because it does not require you to encode the URL patterns processed by theActionMapper
in to yourstruts.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&width=100&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>