struts2自訂攔截器,struts2自訂攔截
首先是struts2架構中主要的兩個檔案:一個是攔截器類,另外一個是struts.xml設定檔。
簡單來說,一個是說明要做的事,一個是說明在哪裡做的。
攔截器類
package com.test.interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class TestInterceptor extends AbstractInterceptor{/** * */private static final long serialVersionUID = 1L;@Overridepublic String intercept(ActionInvocation invocation) throws Exception {System.out.println("攔截器之前。。。。。。。。。。。");String result=invocation.invoke();System.out.println("。。。。。。。。。。攔截器之後。");return result;}}
這個攔截器非常簡單,就是在所有動作之前和之後輸出兩段話。
struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="" extends="struts-default"> <!-- 這裡是攔截器的配置 --> <interceptors> <interceptor name="test" class="com.test.interceptor.TestInterceptor"> </interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="test"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <!-- 正確和錯誤的分別跳轉,input是在驗證失敗後會跳轉的頁面 --> <action name="data" class="com.test.action.ValideAction"> <interceptor-ref name="myStack"></interceptor-ref> <!-- 使用攔截器的動作 --> <result name="success">/result.jsp</result> <result name="input">/login.jsp</result> </action> </package></struts>
輸出結果
對於struts2自訂攔截器的問題
攔截器跟struts2沒關係,這個是在web.xml中配置的,下面這個就是所謂的攔截器了:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
下面這一段就是所謂登入攔截器 實現:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String currentURL = httpRequest.getRequestURI();
// 取得根目錄所對應的絕對路徑:
String targetURL = currentURL.substring(currentURL.lastIndexOf("/"),
currentURL.length());
// 截取到當前檔案名稱用於比較
HttpSession session = httpRequest.getSession(false);
if (!("/userLogin!logIn.action".equals(targetURL)
|| "/login.jsp".equals(targetURL) || "/login_admin.jsp"
.equals(targetURL))) {
if (session == null || session.getAttribute("userInfo") == null) {
// *使用者登入以後需手動添加session
httpResponse.sendRedirect(h......餘下全文>>
Struts2 自訂攔截器,怎實現先於 action 執行
在Action前用攔截器是直接return act.invoke()。只要把收尾操作放到act.invoke()和return result;之間就行了,見下。
public class OneInterceptor implements Interceptor{
...
public String intercept(ActionInvocation act) throws Exception {
//在Action之前執行的代碼
......
//開始執行Action
String result=act.invoke();//
//在Action之後的代碼
......
//結束攔截器
return result;
}
}
其實是很簡單的原理,只要理解攔截器的工作原理就行。我也沒想到這個方法,後來用google到外國網站搜到的……
另外,站長團上有產品團購,便宜有保證