Struts2简单的自定义拦截器MethodFilterInterceptor

一、什么是拦截器

二、本文的实现

1、定义一个拦截器import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;/** *权限校验的拦截器 * @author CodyLee * */public class PrivilegeInterceptor extends MethodFilterInterceptor{@Override//执行拦截的方法protected String doIntercept(ActionInvocation actionInvocation) throws Exception{//判断是否登陆,如果登陆,放行;如果没有登陆,跳转到登陆页面AdminUser adminUser = (AdminUser)ServletActionContext.getRequest().getSession().getAttribute("existAdminUser");if(adminUser != null){//已经登陆过return actionInvocation.invoke();}else{//跳转到登陆页面ActionSupport support = (ActionSupport)actionInvocation.getAction();support.addActionError("您还没有登陆!没有权限访问!");return ActionSupport.LOGIN;}} }2、配置自定义拦截器<!– 配置自定义拦截器 –><interceptors><interceptor name="privilegeInterceptor" class="cn.itcast.shop.interceptor.PrivilegeInterceptor"/></interceptors><action name="adminCategory_*" class="adminCategoryAction" method="{1}"><result name="findAll">/admin/category/list.jsp</result><interceptor-ref name="privilegeInterceptor"/><interceptor-ref name="defaultStack"/> </action>

三、注意事项

版权声明:本文为博主原创文章,,不经博主允许注明链接即可转载。

积极的人在每一次忧患中都看到一个机会,而消极的人则在每个机会都看到某种忧患。

Struts2简单的自定义拦截器MethodFilterInterceptor

相关文章:

你感兴趣的文章:

标签云: