範例原始碼下載:http://goo.gl/mzuXt6 使用 Intellij IDEA
Action 的生命週期
由此看出,攔截器式運作在 Action 之前
自訂攔截器流程:
1.練立一個類別 implements com.opensymphony.xwork2.interceptor.Interceptor
2.implement 當中的 intercept(ActionInvocation invocation) 方法
3.在 struts.xml(struts 配置檔)中配置自訂的 interceptor
4.依照需求配置到所要運作的 action 當中
簡單做一個範例畫面如下:
點擊測試之後,將input field 中的值導到歡迎頁面中
=== Action ===
package test.actions;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
private String msg;
@Override
public String execute() throws Exception {
System.out.println("HelloWorld Action execute Method");
return SUCCESS;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
=== Interceptor ===
package test.interceptors;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* Created by Hsu on 2015/4/22.
*/
public class SaysomethingInterceptor implements Interceptor {
@Override
public void destroy() {
System.out.println("SaysomethingInterceptor is destory");
}
@Override
public void init() {
System.out.println("SaysomethingInterceptor is initial");
}
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("SaysomethingInterceptor before invoke");
// Start execute Action
String result = actionInvocation.invoke();
System.out.println("SaysomethingInterceptor after invoke");
return result;
}
}
在 SaysomethingInterceptor 中,我們只單純加了幾個 sysout,以便觀察運作流程
String result = actionInvocation.invoke();return result;
actionInvocation.invoke(); 主要就是執行 Action 中的流程
=== 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.devMode" value="true"/>
<constant name="struts.locale" value="zh_TW"/>
<!-- 定義系統預設編碼集 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="test" extends="struts-default">
<interceptors>
<interceptor name="Saysomething" class="test.interceptors.SaysomethingInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="Saysomething"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="HelloAction" class="test.actions.HelloWorld">
<interceptor-ref name="myStack"/>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
上面的配置,主要就是宣告自訂 攔截器,並將該攔截器配置到我們所想要執行的 Action 中
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
程式運作畫面:
參考資料:
Struts 2 creating own interceptor
沒有留言 :
張貼留言