sturts2拦截器

来源:互联网 发布:python webdriver 编辑:程序博客网 时间:2024/06/07 00:47

sturts2自带了很多拦截器,使用方便。在网上商城项目中,我们自己定义了一个拦截器,操作如下:


写一个拦截器的类:

package cn.itcast.shop.interceptor;import org.apache.struts2.ServletActionContext;import cn.itcast.shop.adminuser.vo.AdminUser;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;/* * 后台校验拦截器 * 没有登录的用户不能操作 * */public class Privileget extends MethodFilterInterceptor {@Override//执行拦截的方法protected String doIntercept(ActionInvocation actionInvocation) throws Exception {// 判断session中是否保存了用户信息AdminUser existAdminUser=(AdminUser) ServletActionContext.getRequest().getSession().getAttribute("existAdminUser");if(existAdminUser==null){//没有登录ActionSupport actionSupport= (ActionSupport) actionInvocation.getAction();actionSupport.addActionError("亲!您还没有登录,没有操作权限!");return "loginFail";}else{//已经登录return actionInvocation.invoke();}}}


在struts.xml中进行配置

<interceptors><interceptor name="Privileget" class="cn.itcast.shop.interceptor.Privileget"></interceptor></interceptors>


在需要限制的模块中应用

<interceptor-ref name="Privileget"/><interceptor-ref name="defaultStack"/>




原创粉丝点击