struts2自定义拦截器

1.先写一个action

package action;

public class InterceptorAction {
	
	public String fun1() {
		return "ok";
	}
}


2.配置action

	<package name="base" extends="struts-default">
		<global-results>
		<result name="message">/index.jsp</result>
		<result name="message">/WEB-INF/page/message.jsp</result>
	</global-results>
	</package>

	<package name="delversi" namespace="/test" extends="base">
	
	<interceptors>
		<interceptor name="permission" class="interceptor.InterceptorDemo"></interceptor>
		<interceptor-stack name="permissionStack">
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="permission"></interceptor-ref>
		</interceptor-stack>
	</interceptors>
	<action name="interceptor_*" class="action.InterceptorAction" method="{1}">
		<interceptor-ref name="permission"></interceptor-ref>
		<result name="ok">/ddd.jsp</result>
	</action>

定义拦截器栈,是为了能使用系统自带的拦截器


3.写一个权限拦截器

package interceptor;

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

public class InterceptorDemo implements Interceptor {

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void init() {
		// TODO Auto-generated method stub
		
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("进入拦截器");
		if (ActionContext.getContext().getSession().get("user") !=null ) {
			return invocation.invoke();
		}else {
			ActionContext.getContext().put("message", "无权限");
			return "message";
		}
		
	}

}
登录了,返回ok,未登录返回message


4.写一个jsp,向session中注入user

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<% 
request.getSession().setAttribute("user","111");
%>


5.未登录



登陆后



转载请注明出处

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值