struts2学习笔记【十一】 / Interceptor(2)

1.5. 权限认证

一般情况下,在WEB-INF下的内容不登陆是不能被访问的,客户端跳转不能直接访问上述目录中的内容。

 public class VaildInterceptor extends AbstractInterceptor {

 

@Override

public String intercept(ActionInvocation invocation) 

throws Exception 

{

String result "";

Map session = ActionContext.getContext().getSession();

//也可以通过这样的方式进行session的初始化

//Map session =  invocation.getInvocationContext().getSession();

if(session.get("login") != null){

result = Action.SUCCESS;

}else{

 result = invocation.invoke();

}

return result;

}

}

在后面的配置文件中进行配置:

<action name="自定义">

//只能拦截单个页面,不能拦截整个目录

<result>/WEB-INF/admin/manager.jsp</result>

//在此处可以存放结果集,加interceptor或是引用interceptor栈,共用部分可以设为全局。

</action>

 

拦截器同过滤器各有前千秋,要灵活运用.

<filter>

   <filter-name>login</filter-name>

   <filter-class>

   com.sun.demo.LoginFilter

<!--

LoginFilter是实现过滤操作的类,其中可以实现一些列的业务逻辑

转码,判断,查询

权限管理,转向分发等

-->

   </filter-class>

</filter>

<filter-mapping>

   <filter-name>login</filter-name>

   <url-pattern>/admin/*</url-pattern>

<!--

可以对/admin目录下所有的内容进行过滤

-->

</filter-mapping>

 

 

 

1.6. 监听视图

public class TestPreResult implements PreResultListener {

 

public void beforeResult(ActionInvocation invocation, String str {

//编写各种自定义需要处理的业务逻辑

}

}

注意:这个监听不要再struts.xml中进行配置,但是需要同其他拦截器进行绑定:

invocation.addPreResultListener(new TestPreResult());

 

1.7. 注解配置拦截器

注解分为类级别和方法级别

类级别:packageactionresultsresult

 

在使用注解前,必须在struts.xml中配置一个注解拦截器【因为在默认中没有】

<interceptor 

name="annotationInterceptor" 

class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkFlowInterceptor" />

在指定的action中引用这个拦截器。

 

action中配置注解:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值