struct 拦截器 配置+实例

自定义拦截器及拦截器栈

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
<package name="p1" extends="struts-default">
<interceptors>

<interceptor name="privilage" class="cn.qyc.interceptor.Inter"></interceptor>
<interceptor-stack name="mystack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="privilage"></interceptor-ref>

</interceptor-stack>
</interceptors>
	<action name="login" class="cn.qyc.loGIN.Login" method="login">
	<result name="success" type="redirect">/book.jsp</result>
	<result name="error" type="dispatcher">/error.jsp</result>
	</action>
	<action name="book_*" class="cn.qyc.book.BookseOperation" method="{1}">
	<result name="success" type="redirect">/booksuccess.jsp</result>
	<result name="login" type="redirect">/index.jsp</result>
	<interceptor-ref name="mystack"></interceptor-ref>
	</action>
</package>
</struts>

继承AbstractInterceptor

package cn.qyc.interceptor;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class Inter extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation invocations) throws Exception {
		// TODO Auto-generated method stub
		ActionContext context = invocations.getInvocationContext();
		ActionContext context2 = ActionContext.getContext();
		String usernameString = (String) context.getSession().get("username");
		if (usernameString!=null) {
			context2.getSession().put("msg", "已登陆");
			return invocations.invoke();
		} 
		else {
			context2.getSession().put("msg","未登录");
			System.out.println(context2.getSession().get("msg"));
			return Action.LOGIN;
		}
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值