Struts2的自定义拦截器
来源:互联网 发布:增值税打印软件 编辑:程序博客网 时间:2024/05/19 16:49
拦截器文件:
package com.structs2;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class Myinterceptor implements Interceptor {
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation invocation) throws Exception {
// String user = String.valueOf(ActionContext.getContext().getSession().get("user"));
Object user = ActionContext.getContext().getSession().get("user");
// invocation.invoke();//要用这个方法之后--被拦截到得方法执行
System.out.println("uuuuuuuuuuuuuuuuuuuuuuuuuuu"+user);
if(user!=null)//代表用户已登录允许执行Action中的方法
return invocation.invoke();
ActionContext.getContext().put("message", "你无权执行此操作");
return "success";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" namespace="/test" extends="struts-default">
<interceptors>
<interceptor name="My" class="com.structs2.Myinterceptor"></interceptor><!-- 定义拦截器 -->
<interceptor-stack name="MyStack"><!-- 定义拦截器栈 -->
<interceptor-ref name="defaultStack"/><!-- 调用默认的拦截器栈里的所有拦截器,写在前面 -->
<interceptor-ref name="My"/><!--调用拦截器,写在后面-->
</interceptor-stack>
</interceptors>
<!-- <default-interceptor-ref name="MyStack"/> 此包中的所有Action都可以用到此拦截器-->
<global-results>
<result name="success" >/hello.jsp</result>
</global-results>
<action name="list*" class="com.structs2.HelloWordAction"
method="{1}">
<!-- <interceptor-ref name="My"/>调用拦截器 丢掉的默认的许多拦截器 -->
<interceptor-ref name="MyStack"/><!--调用自己定义的拦截器栈只有这个Action可以用到这个拦截器 -->
<!-- <interceptor-ref name="xxx"/> 如果对这个Action再定义拦截器,则默认的放在前边,再定义的放到后边-->
</action>
</package>
</struts>
package com.structs2;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class Myinterceptor implements Interceptor {
}
public void init() {
}
public String intercept(ActionInvocation invocation) throws Exception {
// String user = String.valueOf(ActionContext.getContext().getSession().get("user"));
Object user = ActionContext.getContext().getSession().get("user");
// invocation.invoke();//要用这个方法之后--被拦截到得方法执行
System.out.println("uuuuuuuuuuuuuuuuuuuuuuuuuuu"+user);
if(user!=null)//代表用户已登录允许执行Action中的方法
return invocation.invoke();
ActionContext.getContext().put("message", "你无权执行此操作");
return "success";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" namespace="/test" extends="struts-default">
<interceptors>
<interceptor name="My" class="com.structs2.Myinterceptor"></interceptor><!-- 定义拦截器 -->
<interceptor-stack name="MyStack"><!-- 定义拦截器栈 -->
<interceptor-ref name="defaultStack"/><!-- 调用默认的拦截器栈里的所有拦截器,写在前面 -->
<interceptor-ref name="My"/><!--调用拦截器,写在后面-->
</interceptor-stack>
</interceptors>
<!-- <default-interceptor-ref name="MyStack"/> 此包中的所有Action都可以用到此拦截器-->
<global-results>
<result name="success" >/hello.jsp</result>
</global-results>
<action name="list*" class="com.structs2.HelloWordAction"
method="{1}">
<!-- <interceptor-ref name="My"/>调用拦截器 丢掉的默认的许多拦截器 -->
<interceptor-ref name="MyStack"/><!--调用自己定义的拦截器栈只有这个Action可以用到这个拦截器 -->
<!-- <interceptor-ref name="xxx"/> 如果对这个Action再定义拦截器,则默认的放在前边,再定义的放到后边-->
</action>
</package>
</struts>
- Struts2的自定义拦截器
- Struts2的自定义拦截器
- struts2的自定义拦截器
- Struts2的自定义拦截器
- Struts2的自定义拦截器
- struts2中拦截器的全局拦截和自定义拦截
- Struts2的拦截器、自定义拦截器、Struts2 验证机制
- struts2 自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2---自定义拦截器
- struts2自定义拦截器
- tomcat配置数据库连接池(2种方法)
- JDBC对二进制文件的存取
- 拷贝构造函数(浅拷贝 深拷贝)
- Struts2文件上传
- GNU ARM汇编--(十六)bootloader与kernel之间的commandline的传递
- Struts2的自定义拦截器
- Algorithm - 卡特兰数
- 交通灯管理系统
- Struts2手工配置对Action方法的校验代码
- Hook :学习笔记
- 菜鸟的蜕变1
- 【WF】之基础介绍
- Hook :实例剖析
- 配置tomcat虚拟主机