📄 struts 2的基石——拦截器(interceptor) - max on java - blogjava.htm
字号:
<SPAN style="COLOR: #ff0000"></SPAN><SPAN style="COLOR: #0000ff">/></SPAN>
</DIV>
<H2>配置和使用拦截器</H2>
<P>在struts-default.xml中已经配置了以上的拦截器。如果您想要使用上述拦截器,只需要在应用程序struts.xml文件中通过“<include
file="struts-default.xml"
/>”将struts-default.xml文件包含进来,并继承其中的struts-default包(package),最后在定义Action时,使用“<interceptor-ref
name="xx" />”引用拦截器或拦截器栈(interceptor
stack)。一旦您继承了struts-default包(package),所有Action都会调用拦截器栈
——defaultStack。当然,在Action配置中加入“<interceptor-ref name="xx"
/>”可以覆盖defaultStack。</P>
<P>下面是关于拦截器timer使用的例子。首先,新建Action类tuotrial/TimerInterceptorAction.java,内容如下:</P>
<DIV
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/None.gif"
align=top> <SPAN style="COLOR: #0000ff">package</SPAN> <SPAN
style="COLOR: #000000">tutorial;<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/None.gif"
align=top><BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/None.gif"
align=top></SPAN> <SPAN style="COLOR: #0000ff">import</SPAN> <SPAN
style="COLOR: #000000">com.opensymphony.xwork2.ActionSupport;<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/None.gif"
align=top><BR><IMG id=Codehighlighter1_124_281_Open_Image
onclick="this.style.display='none'; Codehighlighter1_124_281_Open_Text.style.display='none'; Codehighlighter1_124_281_Closed_Image.style.display='inline'; Codehighlighter1_124_281_Closed_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedBlockStart.gif"
align=top><IMG id=Codehighlighter1_124_281_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_124_281_Closed_Text.style.display='none'; Codehighlighter1_124_281_Open_Image.style.display='inline'; Codehighlighter1_124_281_Open_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ContractedBlock.gif"
align=top></SPAN> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">class</SPAN> <SPAN
style="COLOR: #000000">TimerInterceptorAction </SPAN><SPAN
style="COLOR: #0000ff">extends</SPAN> <SPAN style="COLOR: #000000">ActionSupport
</SPAN><SPAN id=Codehighlighter1_124_281_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/dot.gif">
</SPAN><SPAN id=Codehighlighter1_124_281_Open_Text><SPAN
style="COLOR: #000000">{<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top> @Override<BR><IMG id=Codehighlighter1_162_279_Open_Image
onclick="this.style.display='none'; Codehighlighter1_162_279_Open_Text.style.display='none'; Codehighlighter1_162_279_Closed_Image.style.display='inline'; Codehighlighter1_162_279_Closed_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_162_279_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_162_279_Closed_Text.style.display='none'; Codehighlighter1_162_279_Open_Image.style.display='inline'; Codehighlighter1_162_279_Open_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ContractedSubBlock.gif"
align=top> </SPAN> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN
style="COLOR: #000000">String execute() </SPAN><SPAN
id=Codehighlighter1_162_279_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/dot.gif">
</SPAN><SPAN id=Codehighlighter1_162_279_Open_Text><SPAN
style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_170_209_Open_Image
onclick="this.style.display='none'; Codehighlighter1_170_209_Open_Text.style.display='none'; Codehighlighter1_170_209_Closed_Image.style.display='inline'; Codehighlighter1_170_209_Closed_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_170_209_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_170_209_Closed_Text.style.display='none'; Codehighlighter1_170_209_Open_Image.style.display='inline'; Codehighlighter1_170_209_Open_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ContractedSubBlock.gif"
align=top> </SPAN> <SPAN
style="COLOR: #0000ff">try</SPAN> <SPAN style="COLOR: #000000"></SPAN><SPAN
id=Codehighlighter1_170_209_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/dot.gif">
</SPAN><SPAN id=Codehighlighter1_170_209_Open_Text><SPAN
style="COLOR: #000000">{<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top> </SPAN> <SPAN
style="COLOR: #008000">//</SPAN> <SPAN style="COLOR: #008000">模拟耗时的操作</SPAN>
<SPAN style="COLOR: #008000"><BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top> </SPAN><SPAN style="COLOR: #000000">
Thread.sleep(</SPAN> <SPAN style="COLOR: #000000">500</SPAN> <SPAN
style="COLOR: #000000">);<BR><IMG id=Codehighlighter1_230_258_Open_Image
onclick="this.style.display='none'; Codehighlighter1_230_258_Open_Text.style.display='none'; Codehighlighter1_230_258_Closed_Image.style.display='inline'; Codehighlighter1_230_258_Closed_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_230_258_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_230_258_Closed_Text.style.display='none'; Codehighlighter1_230_258_Open_Image.style.display='inline'; Codehighlighter1_230_258_Open_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ContractedSubBlock.gif"
align=top> }</SPAN> </SPAN><SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">catch</SPAN> <SPAN
style="COLOR: #000000">(Exception e) </SPAN><SPAN
id=Codehighlighter1_230_258_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/dot.gif">
</SPAN><SPAN id=Codehighlighter1_230_258_Open_Text><SPAN
style="COLOR: #000000">{<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top> e.printStackTrace();<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif"
align=top> }</SPAN> </SPAN><SPAN
style="COLOR: #000000"><BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top> </SPAN> <SPAN
style="COLOR: #0000ff">return</SPAN> <SPAN
style="COLOR: #000000">SUCCESS;<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif"
align=top> }</SPAN> </SPAN><SPAN style="COLOR: #000000"><BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedBlockEnd.gif"
align=top>}</SPAN> </SPAN></DIV>
<P>配置Action,名为Timer,配置文件如下:</P>
<DIV
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN
style="COLOR: #0000ff"><!</SPAN> <SPAN style="COLOR: #ff00ff">DOCTYPE struts
PUBLIC<BR> "-//Apache Software Foundation//DTD Struts
Configuration 2.0//EN"<BR>
"http://struts.apache.org/dtds/struts-2.0.dtd"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">struts</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">include </SPAN><SPAN style="COLOR: #ff0000">file</SPAN>
<SPAN style="COLOR: #0000ff">="struts-default.xml"</SPAN> <SPAN
style="COLOR: #0000ff">/></SPAN> <SPAN style="COLOR: #000000">
<BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">package </SPAN><SPAN style="COLOR: #ff0000">name</SPAN>
<SPAN style="COLOR: #0000ff">="InterceptorDemo"</SPAN> <SPAN
style="COLOR: #ff0000">extends</SPAN> <SPAN
style="COLOR: #0000ff">="struts-default"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">action </SPAN><SPAN style="COLOR: #ff0000">name</SPAN>
<SPAN style="COLOR: #0000ff">="Timer"</SPAN> <SPAN
style="COLOR: #ff0000">class</SPAN> <SPAN
style="COLOR: #0000ff">="tutorial.TimerInterceptorAction"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">interceptor-ref
</SPAN><SPAN style="COLOR: #ff0000">name</SPAN> <SPAN
style="COLOR: #0000ff">="timer"</SPAN> <SPAN style="COLOR: #ff0000"></SPAN><SPAN
style="COLOR: #0000ff">/></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">result</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000">/Timer.jsp</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">result</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">action</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">package</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"></</SPAN>
<SPAN style="COLOR: #800000">struts</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> </DIV>
<P>至于Timer.jsp可以随意写些什么到里面。发布运行应用程序,在浏览器的地址栏键入<A
href="http://localhost:8080/Struts2_Interceptor/Timer.action">http://localhost:8080/Struts2_Interceptor/Timer.action</A>,在出现Timer.jsp页面后,查看服务器的后台输出。</P>
<DIV
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN
style="COLOR: #000000">2006</SPAN> <SPAN style="COLOR: #000000">-</SPAN> <SPAN
style="COLOR: #000000">12</SPAN> <SPAN style="COLOR: #000000">-</SPAN> <SPAN
style="COLOR: #000000">6</SPAN> <SPAN style="COLOR: #000000"></SPAN><SPAN
style="COLOR: #000000">14</SPAN> <SPAN style="COLOR: #000000">:</SPAN> <SPAN
style="COLOR: #000000">27</SPAN> <SPAN style="COLOR: #000000">:</SPAN> <SPAN
style="COLOR: #000000">32</SPAN> <SPAN
style="COLOR: #000000">com.opensymphony.xwork2.interceptor.TimerInterceptor
doLog<BR>信息: Executed action </SPAN><SPAN
style="FONT-WEIGHT: bold; COLOR: #800000">[</SPAN> <SPAN
style="COLOR: #800000">//Timer!execute</SPAN> <SPAN
style="FONT-WEIGHT: bold; COLOR: #800000">]</SPAN> <SPAN
style="COLOR: #000000">took </SPAN><SPAN style="COLOR: #000000">2859</SPAN>
<SPAN style="COLOR: #000000">ms.</SPAN> </DIV>
<P>在您的环境中执行Timer!execute的耗时,可能上述的时间有些不同,这取决于您PC的性能。但是无论如何,2859 ms与500
ms还是相差太远了。这是什么原因呢?其实原因是第一次加载Timer时,需要进行一定的初始工作。当你重新请求Timer.action时,以上输出会变为:</P>
<DIV
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN
style="COLOR: #000000">2006</SPAN> <SPAN style="COLOR: #000000">-</SPAN> <SPAN
style="COLOR: #000000">12</SPAN> <SPAN style="COLOR: #000000">-</SPAN> <SPAN
style="COLOR: #000000">6</SPAN> <SPAN style="COLOR: #000000"></SPAN><SPAN
style="COLOR: #000000">14</SPAN> <SPAN style="COLOR: #000000">:</SPAN> <SPAN
style="COLOR: #000000">29</SPAN> <SPAN style="COLOR: #000000">:</SPAN> <SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -