标签:
一.自定义拦截器(步骤)
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; } }
注意事项:
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有两个作用:
标签:
原文地址:http://www.cnblogs.com/jsnan/p/4506327.html