struts2 之 拦截器

Struts2规定用户自定义拦截器必须实现com.opensymphony.xwork2.interceptor.Interceptor接口

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class PermissionInterceptor implements Interceptor {

	@Override
	public void destroy() {

	}

	@Override
	public void init() {

	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Object user=ActionContext.getContext().getSession().get("user");
		if(user!=null) 
			return invocation.invoke();//过滤成功则继续执行action中result
		ActionContext.getContext().put("message", "你没有权限执行该操作!");
		return "success";//不成功则执行另一个视图
	}

}

配置Struts2拦截器
Struts2拦截器需要在struts.xml中声明,如下struts.xml配置文件

<package name="employee" namespace="/control/employee" extends="struts-default">
		<interceptors>
			<interceptor name="permission" class="cn.ljf.PermissionInterceptor"></interceptor>
			<interceptor-stack name="permissionStack"> <!-- 拦截器栈 -->
				<interceptor-ref name="defaultStack"></interceptor-ref> <!-- 使用了自定义的拦截器,默认的将失效 -->	
				<interceptor-ref name="permission"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		
		<global-results>
			<result name="success">/message.jsp</result>
		</global-results>
		<action name="list_*" class="cn.ljf.HelloWorldAction" method="{1}">
			<interceptor-ref name="permissionStack"></interceptor-ref><!-- 添加拦截器 -->
		</action>
	</package>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值