struts2自定义拦截器
2013-05-14 10:59
330 查看
struts.xml配置:
<!-- struts2拦截器 -->
<package name="struts2" extends="json-default">
<interceptors>
<!-- 自定义拦截器 -->
<interceptor name="myinterceptor"
class="com.rd.common.util.MyInterceptor">
</interceptor>
<!-- 自定义拦截器栈 -->
<interceptor-stack name="myDefaultStack">
<interceptor-ref name="defaultStack">
</interceptor-ref>
<interceptor-ref name="myinterceptor">
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 将自定义拦截器栈设置默认的拦截器 -->
<default-interceptor-ref name="myDefaultStack">
</default-interceptor-ref>
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.jsp页面 -->
<result name="login_out">/show_login.jsp</result>
</global-results>
</package>
注意:struts.xml中的拦截器配置好后,如果想使用拦截器,可以让那个文件夹继承该文件夹,即extends=”struts2”即可。
拦截器类定义:
package mypackage;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
@SuppressWarnings("serial")
public class MyInterceptor implements Interceptor {
@Override
public void destroy() {
System.out.println("-------销毁了拦截器-------");
}
@Override
public void init() {
System.out.println("------初始化了拦截器------");
}
@SuppressWarnings("rawtypes")
@Override
public String intercept(ActionInvocation arg0) throws Exception {
ActionContext ctx = arg0.getInvocationContext();
Map session = ctx.getSession();
Integer userId = (Integer) session.get("userid");
if (userId == null) {
ctx.put("tip", "您未登录,请重新登录!");
return "login_out";
}
return arg0.invoke();
}
}
<!-- struts2拦截器 -->
<package name="struts2" extends="json-default">
<interceptors>
<!-- 自定义拦截器 -->
<interceptor name="myinterceptor"
class="com.rd.common.util.MyInterceptor">
</interceptor>
<!-- 自定义拦截器栈 -->
<interceptor-stack name="myDefaultStack">
<interceptor-ref name="defaultStack">
</interceptor-ref>
<interceptor-ref name="myinterceptor">
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 将自定义拦截器栈设置默认的拦截器 -->
<default-interceptor-ref name="myDefaultStack">
</default-interceptor-ref>
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.jsp页面 -->
<result name="login_out">/show_login.jsp</result>
</global-results>
</package>
注意:struts.xml中的拦截器配置好后,如果想使用拦截器,可以让那个文件夹继承该文件夹,即extends=”struts2”即可。
拦截器类定义:
package mypackage;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
@SuppressWarnings("serial")
public class MyInterceptor implements Interceptor {
@Override
public void destroy() {
System.out.println("-------销毁了拦截器-------");
}
@Override
public void init() {
System.out.println("------初始化了拦截器------");
}
@SuppressWarnings("rawtypes")
@Override
public String intercept(ActionInvocation arg0) throws Exception {
ActionContext ctx = arg0.getInvocationContext();
Map session = ctx.getSession();
Integer userId = (Integer) session.get("userid");
if (userId == null) {
ctx.put("tip", "您未登录,请重新登录!");
return "login_out";
}
return arg0.invoke();
}
}
相关文章推荐
- struts2中自定义拦截器详解
- struts2的拦截器自定义,使用默认拦截器,组合拦截器堆 的案例
- Struts2自定义拦截器
- struts2自定义拦截器
- Java程序员从笨鸟到菜鸟之(四十七)细谈struts2(九)内置拦截器和自定义拦截器详解(附源码)
- Struts2--自定义拦截器
- JAVAEE——struts2_04:自定义拦截器、struts2标签、登陆功能和校验登陆拦截器的实现
- struts2自定义拦截器
- struts2自定义拦截器一——模拟登陆权限验证
- java_struts2自定义拦截器
- Struts2:自定义拦截器
- struts2 自定义拦截器实现日志保存到数据库的功能
- Struts2 自定义拦截器栈后无法得到表单参数之解决办法
- Struts2开发自定义拦截器
- Struts2自定义拦截器实例—登陆权限验证
- 细谈struts2(九)内置拦截器和自定义拦截器详解(附源码)
- struts2自定义拦截器
- struts2.X心得9--struts2自定义拦截器以及文件上传等几个拦截器使用案例
- 从struts2拦截器到自定义拦截器
- struts2默认拦截器与自定义拦截器在使用时的注意事项