7.3简单拦截器
package lee;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.*;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class SimpleInterceptor extends AbstractInterceptor
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String intercept(ActionInvocation invocation) throws Exception
{
LoginAction class="tags" href="/tags/ACTION.html" title=action>action = (LoginAction)invocation.getAction();
System.out.println(name + " 拦截器的动作---------" + "开始执行登陆Action的时间为:" + new Date());
long start = System.currentTimeMillis();
String result = invocation.invoke();
System.out.println(name + " 拦截器的动作---------" + "执行完登陆Action的时间为:" + new Date());
long end = System.currentTimeMillis();
System.out.println(name + " 拦截器的动作---------" + "执行完该Action的事件为" + (end - start) + "毫秒");
return result;
}
}
class="tags" href="/tags/STRUTS.html" title=struts>struts.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE class="tags" href="/tags/STRUTS.html" title=struts>struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://class="tags" href="/tags/STRUTS.html" title=struts>struts.apache.org/dtds/class="tags" href="/tags/STRUTS.html" title=struts>struts-2.0.dtd">
<class="tags" href="/tags/STRUTS.html" title=struts>struts>
<constant name="class="tags" href="/tags/STRUTS.html" title=struts>struts.custom.i18n.resources" value="globalMessages"/>
<constant name="class="tags" href="/tags/STRUTS.html" title=struts>struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="class="tags" href="/tags/STRUTS.html" title=struts>struts-default">
<interceptors>
<interceptor name="mysimple" class="lee.SimpleInterceptor">
<param name="name">简单拦截器</param>
</interceptor>
</interceptors>
<class="tags" href="/tags/ACTION.html" title=action>action name="login" class="lee.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
<!-- 拦截器一般配置在result元素之后! -->
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="mysimple">
<param name="name">改名后的拦截器</param>
</interceptor-ref>
</class="tags" href="/tags/ACTION.html" title=action>action>
</package>
</class="tags" href="/tags/STRUTS.html" title=struts>struts>
7.4深入拦截 器
1方法过滤
package lee;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import java.util.*;
public class MyFilterInterceptor extends MethodFilterInterceptor
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String doIntercept(ActionInvocation invocation)
throws Exception
{
LoginAction class="tags" href="/tags/ACTION.html" title=action>action = (LoginAction)invocation.getAction();
System.out.println(name + " 拦截器的动作---------" + "开始执行登陆Action的时间为:" + new Date());
long start = System.currentTimeMillis();
String result = invocation.invoke();
System.out.println(name + " 拦截器的动作---------" + "执行完登陆Action的时间为:" + new Date());
long end = System.currentTimeMillis();
System.out.println(name + " 拦截器的动作---------" + "执行完该Action的事件为" + (end - start) + "毫秒");
return result;
}
}
class="tags" href="/tags/STRUTS.html" title=struts>struts.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE class="tags" href="/tags/STRUTS.html" title=struts>struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://class="tags" href="/tags/STRUTS.html" title=struts>struts.apache.org/dtds/class="tags" href="/tags/STRUTS.html" title=struts>struts-2.0.dtd">
<class="tags" href="/tags/STRUTS.html" title=struts>struts>
<constant name="class="tags" href="/tags/STRUTS.html" title=struts>struts.custom.i18n.resources" value="globalMessages"/>
<constant name="class="tags" href="/tags/STRUTS.html" title=struts>struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="class="tags" href="/tags/STRUTS.html" title=struts>struts-default">
<interceptors>
<interceptor name="myfilter" class="lee.MyFilterInterceptor">
<param name="name">方法过滤拦截器</param>
</interceptor>
</interceptors>
<class="tags" href="/tags/ACTION.html" title=action>action name="login" class="lee.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
<!-- 拦截器一般配置在result元素之后! -->
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="myfilter">
<param name="name">改名后的方法过滤拦截器</param>
<param name="excludeMethods">haha</param>
<param name="includeMethods">execute</param>
</interceptor-ref>
</class="tags" href="/tags/ACTION.html" title=action>action>
</package>
</class="tags" href="/tags/STRUTS.html" title=struts>struts>
excludeMethods 过滤掉方法为haha
includeMethods 对execute方法进行拦截
2拦截器的顺序
如果是在拦截方法之前,则配置在前面的拦截器,会先对用户的请求起作用。如果是在拦截方法之后,则配置在后面的拦截器,会先对用户的请求起作用。
例如:
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="mysimple">
<param name="name">第一个</param>
</interceptor-ref>
<interceptor-ref name="mysimple">
<param name="name">第二个</param>
</interceptor-ref>
结果:
第一个 拦截器的动作---------开始执行登陆Action的时间为:Wed Jan 12 10:17:16 CST 2011
第二个 拦截器的动作---------开始执行登陆Action的时间为:Wed Jan 12 10:17:16 CST 2011
进入execute方法执行体..........
第二个 拦截器的动作---------执行完登陆Action的时间为:Wed Jan 12 10:17:24 CST 2011
第二个 拦截器的动作---------执行完该Action的事件为8522毫秒
第一个 拦截器的动作---------执行完登陆Action的时间为:Wed Jan 12 10:17:24 CST 2011
第一个 拦截器的动作---------执行完该Action的事件为8522毫秒
3拦截结果的监听器
public class MyPreResultListener implements PreResultListener
{
public void beforeResult(ActionInvocation invocation,String resultCode)
{
System.out.println("返回的逻辑视图为:" + resultCode);
}
}
配置中 没有配置,怎么对跳转的结果进行拦截?
execute方法执行之前的拦截...
进入execute方法执行体..........
返回的逻辑视图为:error
execute方法执行之后的拦截......
4 ...*
5拦截器实现权限控制
package lee;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.*;
public class AuthorityInterceptor extends AbstractInterceptor
{
public String intercept(ActionInvocation invocation) throws Exception
{
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
String user = (String)session.get("user");
if (user != null && user.equals("pp") )
{
return invocation.invoke();
}
ctx.put("tip" , "您还没有登陆,请输入pp,911r登陆系统");
return Action.LOGIN;
}
}
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE class="tags" href="/tags/STRUTS.html" title=struts>struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://class="tags" href="/tags/STRUTS.html" title=struts>struts.apache.org/dtds/class="tags" href="/tags/STRUTS.html" title=struts>struts-2.0.dtd">
<class="tags" href="/tags/STRUTS.html" title=struts>struts>
<constant name="class="tags" href="/tags/STRUTS.html" title=struts>struts.custom.i18n.resources" value="globalMessages"/>
<constant name="class="tags" href="/tags/STRUTS.html" title=struts>struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="class="tags" href="/tags/STRUTS.html" title=struts>struts-default">
<interceptors>
<interceptor name="authority" class="lee.AuthorityInterceptor"/>
</interceptors>
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<class="tags" href="/tags/ACTION.html" title=action>action name="login" class="lee.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</class="tags" href="/tags/ACTION.html" title=action>action>
<class="tags" href="/tags/ACTION.html" title=action>action name="viewBook">
<result>/WEB-INF/jsp/viewBook.jsp</result>
<!-- 拦截器一般配置在result元素之后! -->
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authority"/>
</class="tags" href="/tags/ACTION.html" title=action>action>
</package>
</class="tags" href="/tags/STRUTS.html" title=struts>struts>
页面中<a href="viewBook.class="tags" href="/tags/ACTION.html" title=action>action">查看作者李刚出版的图书</a>
点击链接时进入了拦截. 如果用户名为pp就可以跳转到 <result>/WEB-INF/jsp/viewBook.jsp</result>
${requestScope.tip}
否则提示
您还没有登陆,请输入pp,911登陆系统