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. 注解配置拦截器
注解分为类级别和方法级别
类级别:package,action,results,result
在使用注解前,必须在struts.xml中配置一个注解拦截器【因为在默认中没有】
<interceptor
name="annotationInterceptor"
class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkFlowInterceptor" />
在指定的action中引用这个拦截器。
在action中配置注解: