📄 struts 2的基石——拦截器(interceptor) - max on java - blogjava.htm
字号:
style="COLOR: #000000">18</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">500</SPAN> <SPAN
style="COLOR: #000000">ms.</SPAN> </DIV>
<P>OK,这正是我们期待的结果。上述例子演示了拦截器timer的用途——用于显示执行某个action方法的耗时,在我们做一个粗略的性能调试时,这相当有用。</P>
<H2>自定义拦截器</H2>
<P>作为“框架(framework)”,可扩展性是不可或缺的,因为世上没有放之四海而皆准的东西。虽然,Struts
2为我们提供如此丰富的拦截器实现,但是这并不意味我们失去创建自定义拦截器的能力,恰恰相反,在Struts 2自定义拦截器是相当容易的一件事。</P>
<P> </P>
<TABLE
style="BORDER-RIGHT: #f0c000 1px solid; BORDER-TOP: #f0c000 1px solid; MARGIN-TOP: 8px; MARGIN-BOTTOM: 8px; BORDER-LEFT: #f0c000 1px solid; BORDER-BOTTOM: #f0c000 1px solid; BACKGROUND-COLOR: #ffffce; TEXT-ALIGN: left"
cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD
style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; PADDING-TOP: 4px"><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/lightbulb_on.gif">
</TD>
<TD
style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; PADDING-TOP: 4px; FONT-FAMILY: 黑体, 宋体, Tahoma, Arial">大家在开始着手创建自定义拦截器前,切记以下原则:<BR>拦截器必须是无状态的,不要使用在API提供的ActionInvocation之外的任何东西。</TD></TR></TBODY></TABLE>
<P>要求拦截器是无状态的原因是Struts 2不能保证为每一个请求或者action创建一个实例,所以如果拦截器带有状态,会引发并发问题。</P>
<P>所有的Struts
2的拦截器都直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。除此之外,大家可能更喜欢继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor。</P>
<P>以下例子演示通过继承AbstractInterceptor,实现授权拦截器。</P>
<P>首先,创建授权拦截器类tutorial.AuthorizationInterceptor,代码如下:</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">java.util.Map;<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.Action;<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.ActionInvocation;<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.interceptor.AbstractInterceptor;<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/None.gif"
align=top><BR><IMG id=Codehighlighter1_261_662_Open_Image
onclick="this.style.display='none'; Codehighlighter1_261_662_Open_Text.style.display='none'; Codehighlighter1_261_662_Closed_Image.style.display='inline'; Codehighlighter1_261_662_Closed_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedBlockStart.gif"
align=top><IMG id=Codehighlighter1_261_662_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_261_662_Closed_Text.style.display='none'; Codehighlighter1_261_662_Open_Image.style.display='inline'; Codehighlighter1_261_662_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">AuthorizationInterceptor </SPAN><SPAN
style="COLOR: #0000ff">extends</SPAN> <SPAN
style="COLOR: #000000">AbstractInterceptor </SPAN><SPAN
id=Codehighlighter1_261_662_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_261_662_Open_Text><SPAN
style="COLOR: #000000">{<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top><BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top> @Override<BR><IMG id=Codehighlighter1_338_659_Open_Image
onclick="this.style.display='none'; Codehighlighter1_338_659_Open_Text.style.display='none'; Codehighlighter1_338_659_Closed_Image.style.display='inline'; Codehighlighter1_338_659_Closed_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_338_659_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_338_659_Closed_Text.style.display='none'; Codehighlighter1_338_659_Open_Image.style.display='inline'; Codehighlighter1_338_659_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 intercept(ActionInvocation ai) </SPAN><SPAN
style="COLOR: #0000ff">throws</SPAN> <SPAN style="COLOR: #000000">Exception
</SPAN><SPAN id=Codehighlighter1_338_659_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_338_659_Open_Text><SPAN
style="COLOR: #000000">{<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top> Map session </SPAN><SPAN
style="COLOR: #000000">=</SPAN> <SPAN
style="COLOR: #000000">ai.getInvocationContext().getSession();<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top> String role </SPAN><SPAN
style="COLOR: #000000">=</SPAN> <SPAN style="COLOR: #000000">(String)
session.get(</SPAN> <SPAN style="COLOR: #000000">"</SPAN> <SPAN
style="COLOR: #000000">ROLE</SPAN> <SPAN style="COLOR: #000000">"</SPAN> <SPAN
style="COLOR: #000000">);<BR><IMG id=Codehighlighter1_461_619_Open_Image
onclick="this.style.display='none'; Codehighlighter1_461_619_Open_Text.style.display='none'; Codehighlighter1_461_619_Closed_Image.style.display='inline'; Codehighlighter1_461_619_Closed_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_461_619_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_461_619_Closed_Text.style.display='none'; Codehighlighter1_461_619_Open_Image.style.display='inline'; Codehighlighter1_461_619_Open_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ContractedSubBlock.gif"
align=top> </SPAN> <SPAN
style="COLOR: #0000ff">if</SPAN> <SPAN style="COLOR: #000000">(</SPAN> <SPAN
style="COLOR: #0000ff">null</SPAN> <SPAN style="COLOR: #000000"></SPAN><SPAN
style="COLOR: #000000">!=</SPAN> <SPAN style="COLOR: #000000">role) </SPAN><SPAN
id=Codehighlighter1_461_619_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_461_619_Open_Text><SPAN
style="COLOR: #000000">{<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top> Object o </SPAN><SPAN
style="COLOR: #000000">=</SPAN> <SPAN
style="COLOR: #000000">ai.getAction();<BR><IMG
id=Codehighlighter1_523_592_Open_Image
onclick="this.style.display='none'; Codehighlighter1_523_592_Open_Text.style.display='none'; Codehighlighter1_523_592_Closed_Image.style.display='inline'; Codehighlighter1_523_592_Closed_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_523_592_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_523_592_Closed_Text.style.display='none'; Codehighlighter1_523_592_Open_Image.style.display='inline'; Codehighlighter1_523_592_Open_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ContractedSubBlock.gif"
align=top> </SPAN> <SPAN
style="COLOR: #0000ff">if</SPAN> <SPAN style="COLOR: #000000">(o </SPAN><SPAN
style="COLOR: #0000ff">instanceof</SPAN> <SPAN style="COLOR: #000000">RoleAware)
</SPAN><SPAN id=Codehighlighter1_523_592_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_523_592_Open_Text><SPAN
style="COLOR: #000000">{<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top> RoleAware
action </SPAN><SPAN style="COLOR: #000000">=</SPAN> <SPAN
style="COLOR: #000000">(RoleAware) o;<BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/InBlock.gif"
align=top>
action.setRole(role);<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">ai.invoke();<BR><IMG
id=Codehighlighter1_626_654_Open_Image
onclick="this.style.display='none'; Codehighlighter1_626_654_Open_Text.style.display='none'; Codehighlighter1_626_654_Closed_Image.style.display='inline'; Codehighlighter1_626_654_Closed_Text.style.display='inline';"
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_626_654_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_626_654_Closed_Text.style.display='none'; Codehighlighter1_626_654_Open_Image.style.display='inline'; Codehighlighter1_626_654_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">else</SPAN> <SPAN
style="COLOR: #000000"></SPAN><SPAN id=Codehighlighter1_626_654_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_626_654_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: #0000ff">return</SPAN> <SPAN
style="COLOR: #000000">Action.LOGIN;<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/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> <BR><IMG
src="Struts 2的基石——拦截器(Interceptor) - Max On Java - BlogJava.files/ExpandedBlockEnd.gif"
align=top>}</SPAN> </SPAN></DIV>
<P>以上代码相当简单,我们通过检查session是否存在键为“ROLE”的字符串,判断用户是否登陆。如果用户已经登陆,将角色放到Action中,调用Action;否则,拦截直接返回Action.LOGIN字段。为了方便将角色放入Action,我定义了接口tutorial.RoleAware,代码如下:</P>
<DIV
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -