print?步驟:
步驟:
1:創建一個類實現com.opensymphony.xwork2.interceptor.Interceptor接口
2:實現intercept(ActionInvocation invocation) 方法
3:在struts.xml中配置攔截器
4: 鏈接到相應的action
代碼下載
文件目錄:
struts2自身有很多的攔截器,在struts-core.jar中的struts-default.xml中。
首先創建一個HelloAction
[java]
package com.topwqp.common.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
public String execute() throws Exception {
System.out.println("HelloAction execute() is called");
return SUCCESS;
}
}
package com.topwqp.common.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
public String execute() throws Exception {
System.out.println("HelloAction execute() is called");
return SUCCESS;
}
}
然後創建相應攔截器:
init()方法是在啟動服務器時加載攔截器時初始化的
destory()方法是在退出服務器調用的,但是這個方法不一定調用
ActionInvocation也是一個接口,對應Action或者其他的攔截器
解釋:
[plain]
An ActionInvocation represents the execution state of an Action. It holds the Interceptors and the Action instance. By repeated re-entrant execution of the invoke() method, initially by the ActionProxy, then by the Interceptors, the Interceptors are all executed, and then the Action and the Result.
An ActionInvocation represents the execution state of an Action. It holds the Interceptors and the Action instance. By repeated re-entrant execution of the invoke() method, initially by the ActionProxy, then by the Interceptors, the Interceptors are all executed, and then the Action and the Result.關於invoke()方法是必須的:
[plain]
invoke
String invoke()
throws Exception
Invokes the next step in processing this ActionInvocation.
If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor to execute. If there are no more Interceptors to be applied, the Action is executed. If the ActionProxy.getExecuteResult() method returns true, the Result is also executed.
Returns:
the return code.
Throws:
Exception - can be thrown.
invoke
String invoke()
throws Exception
Invokes the next step in processing this ActionInvocation.
If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor to execute. If there are no more Interceptors to be applied, the Action is executed. If the ActionProxy.getExecuteResult() method returns true, the Result is also executed.
Returns:
the return code.
Throws:
Exception - can be thrown.
[java]
package com.topwqp.common.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class PrintMessageInterceptor implements Interceptor{
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destory() method is invoked");
}
@Override
public void init() {
// TODO Auto-generated method stub
System.out.println("init() method is invoked");
}
//put interceptor code here
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
System.out.println("before invocation.invoked..........");
String result = invocation.invoke();
System.out.println(result);
System.out.println("after invocation.invoked..........");
return result;
}
}
package com.topwqp.common.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class PrintMessageInterceptor implements Interceptor{
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destory() method is invoked");
}
@Override
public void init() {
// TODO Auto-generated method stub
System.out.println("init() method is invoked");
}
//put interceptor code here
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
System.out.println("before invocation.invoked..........");
String result = invocation.invoke();
System.out.println(result);
System.out.println("after invocation.invoked..........");
return result;
}
}
對應的struts.xml配置:
[html]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="printMessageInterceptor"
class="com.topwqp.common.interceptor.PrintMessageInterceptor">
</interceptor>
<interceptor-stack name="newStack">
<interceptor-ref name="printMessageInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="helloAction"
class="com.topwqp.common.action.HelloAction" >
<interceptor-ref name="newStack"/>
<result name="success">pages/hello.jsp</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="printMessageInterceptor"
class="com.topwqp.common.interceptor.PrintMessageInterceptor">
</interceptor>
<interceptor-stack name="newStack">
<interceptor-ref name="printMessageInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="helloAction"
class="com.topwqp.common.action.HelloAction" >
<interceptor-ref name="newStack"/>
<result name="success">pages/hello.jsp</result>
</action>
</package>
</struts>
對應的hello.jsp配置:
[html]
<html>
<body>
<h2>Hello TOPWQP</h2>
</body>
</html>
<html>
<body>
<h2>Hello TOPWQP</h2>
</body>
</html>
結果:
[plain]
<SPAN style="COLOR: #3c3c3c">五月 05, 2013 4:16:29 下午 org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.24.
五月 05, 2013 4:16:30 下午 org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
五月 05, 2013 4:16:32 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:Strut2OwnInterceptor' did not find a matching property.
五月 05, 2013 4:16:38 下午 org.apache.coyote.http11.Http11AprProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
五月 05, 2013 4:16:38 下午 org.apache.coyote.ajp.AjpAprProtocol init
INFO: Initializing Coyote AJP/1.3 on ajp-8009
五月 05, 2013 4:16:38 下午 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 13193 ms
五月 05, 2013 4:16:38 下午 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
五月 05, 2013 4:16:38 下午 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.35
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Parsing configuration file [struts-default.xml]
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Unable to locate configuration files of the name struts-plugin.xml, skipping
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Parsing configuration file [struts-plugin.xml]
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Parsing configuration file [struts.xml]
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for class com.opensymphony.xwork2.ObjectFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for class com.opensymphony.xwork2.conversion.impl.XWorkConverter
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.TextProvider
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.ActionProxyFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.conversion.ObjectTypeDeterminer
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface org.apache.struts2.dispatcher.mapper.ActionMapper
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (jakarta) for interface org.apache.struts2.dispatcher.multipart.MultiPartRequest
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for class org.apache.struts2.views.freemarker.FreemarkerManager
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface org.apache.struts2.components.UrlRenderer
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.validator.ActionValidatorManager
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.ValueStackFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.reflection.ReflectionProvider
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.reflection.ReflectionContextFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.PatternMatcher
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface org.apache.struts2.dispatcher.StaticContentLoader
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.UnknownHandlerManager
</SPAN>init() method is invoked<SPAN style="COLOR: #3c3c3c">
五月 05, 2013 4:16:44 下午 org.apache.coyote.http11.Http11AprProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
五月 05, 2013 4:16:45 下午 org.apache.coyote.ajp.AjpAprProtocol start
INFO: Starting Coyote AJP/1.3 on ajp-8009
五月 05, 2013 4:16:45 下午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 7060 ms
</SPAN><SPAN style="BACKGROUND-COLOR: rgb(204,204,204)">before invocation.invoked..........
HelloAction execute() is called
success
after invocation.invoked..........</SPAN>
五月 05, 2013 4:16:29 下午 org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.24.
五月 05, 2013 4:16:30 下午 org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
五月 05, 2013 4:16:32 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:Strut2OwnInterceptor' did not find a matching property.
五月 05, 2013 4:16:38 下午 org.apache.coyote.http11.Http11AprProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
五月 05, 2013 4:16:38 下午 org.apache.coyote.ajp.AjpAprProtocol init
INFO: Initializing Coyote AJP/1.3 on ajp-8009
五月 05, 2013 4:16:38 下午 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 13193 ms
五月 05, 2013 4:16:38 下午 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
五月 05, 2013 4:16:38 下午 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.35
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Parsing configuration file [struts-default.xml]
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Unable to locate configuration files of the name struts-plugin.xml, skipping
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Parsing configuration file [struts-plugin.xml]
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Parsing configuration file [struts.xml]
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for class com.opensymphony.xwork2.ObjectFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for class com.opensymphony.xwork2.conversion.impl.XWorkConverter
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.TextProvider
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.ActionProxyFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.conversion.ObjectTypeDeterminer
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface org.apache.struts2.dispatcher.mapper.ActionMapper
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (jakarta) for interface org.apache.struts2.dispatcher.multipart.MultiPartRequest
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for class org.apache.struts2.views.freemarker.FreemarkerManager
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface org.apache.struts2.components.UrlRenderer
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.validator.ActionValidatorManager
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.ValueStackFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.reflection.ReflectionProvider
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.reflection.ReflectionContextFactory
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.util.PatternMatcher
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface org.apache.struts2.dispatcher.StaticContentLoader
五月 05, 2013 4:16:43 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Choosing bean (struts) for interface com.opensymphony.xwork2.UnknownHandlerManager
init() method is invoked
五月 05, 2013 4:16:44 下午 org.apache.coyote.http11.Http11AprProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
五月 05, 2013 4:16:45 下午 org.apache.coyote.ajp.AjpAprProtocol start
INFO: Starting Coyote AJP/1.3 on ajp-8009
五月 05, 2013 4:16:45 下午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 7060 ms
before invocation.invoked..........
HelloAction execute() is called
success
after invocation.invoked..........