码迷,mamicode.com
首页 > 其他好文 > 详细

拦截器

时间:2015-05-15 17:36:45      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

一.自定义拦截器(步骤)

  1.编写interceptor

public class PrivilegerInterceptor implements Interceptor {

    @Override
    public void destroy() {
    }

    @Override
    public void init() {
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        
        String username = ServletActionContext.getRequest().getParameter("username");
        if ("admin".equals(username)) {
            return invocation.invoke();
        }else {
            ActionContext.getContext().getValueStack().push("权限不足");
            return "error";
        }
    }
}

 

  2.写一个Action

public class TestAction extends ActionSupport {
    public String test1(){
        ActionContext.getContext().getValueStack().push("login success");
        return SUCCESS;
    }
}

  3.在struts.xml中配置

        <interceptors>
            <interceptor name="privilege" class="com.sn.interceptor.PrivilegerInterceptor">
            </interceptor>
            
            <interceptor-stack name="privilegeStack">
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <interceptor-ref name="privilege"></interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="privilegeStack"></default-interceptor-ref>

 

二、拦截器的应用

  

      <form action="${pageContext.request.contextPath}/propertydriver_test1.action" method="post">
          <input type="text" name="username"><br/>
          <input type="password" name="password"><br/>
          <input type="submit" value="submit"><br/>
      </form>

 

  1.属性驱动

  

public class Demo1Action extends ActionSupport implements {

    private String username;
    private String password;
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String test(){
        System.out.println(this.username);
        System.out.println(this.password);
        return SUCCESS;
    }
}

注意事项:

  • 1、 必须使用struts2默认的拦截器栈中的ParametersInterceptor
  • 2、 Action中的属性和表单中的name属性的值保持一致
  • 3、 利用valueStack.setValue方法可以赋值了

 

  2.模型驱动

public class Demo1Action extends ActionSupport implements ModelDriven<User> {
    
    private User user = new User();
    
    public String test1(){
        System.out.println(this.user.getUsername());
        System.out.println(this.user.getPassword());
        return SUCCESS;
    }

    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return this.user;
    }
}

ModelDriverInterceptor有两个作用:

  • 1、 当前请求的action必须实现ModelDriver接口
  • 2、 把model对象放入到了栈顶

  

 

 

 

 

  

拦截器

标签:

原文地址:http://www.cnblogs.com/jsnan/p/4506327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!