?? 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 + -