struts2拦截器
程序员文章站
2022-07-12 16:14:12
...
1. struts2拦截器简介
2. struts2预定义拦截器和拦截器栈
3. 自定义拦截器-登录验证拦截器
拦截器简介 Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现。 优点:通用功能的封装,提供了可重用性; 新建项目HeadFirstStruts2Chap03
2. struts2预定义拦截器和拦截器栈
HelloAction.java package com.andrew.action; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String execute() throws Exception { System.out.println("执行了HelloAction的默认方法"); return SUCCESS; } } MyInterceptor.java package com.andrew.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class MyInterceptor implements Interceptor { public void init() { System.out.println("MyInterceptor初始化"); } public String intercept(ActionInvocation invocation) throws Exception { System.out.println("在Action执行之前"); String result = invocation.invoke(); System.out.println("result:" + result); System.out.println("在Action执行之后"); return result; } public void destroy() { System.out.println("MyInterceptor销毁"); } } struts.xml <package name="manage" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.andrew.interceptor.MyInterceptor"></interceptor> </interceptors> <action name="hello" class="com.andrew.action.HelloAction"> <result name="success">success.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> success.jsp Name:${name } MyInterceptor初始化 http://localhost:8080/HeadFirstStruts2Chap03/hello?name=struts2 运行结果: Name:struts2 控制台: 在Action执行之前 执行了HelloAction的默认方法 result:success 在Action执行之后
3. 自定义拦截器-登录验证拦截器
User.java package com.andrew.model; public class User { 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; } } UserService.java package com.andrew.service; import com.andrew.model.User; public class UserService { public boolean login(User user) { if ("andrew".equals(user.getUserName()) && "123456".equals(user.getPassword())) { return true; } else { return false; } } } UserAction.java package com.andrew.action; import java.util.Map; import com.andrew.model.User; import com.andrew.service.UserService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private UserService userService = new UserService(); private User user; private String error; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getError() { return error; } public void setError(String error) { this.error = error; } @Override public String execute() throws Exception { if (userService.login(user)) { ActionContext actionContext = ActionContext.getContext(); Map<String, Object> session = actionContext.getSession(); session.put("currentUser", user); return SUCCESS; } else { this.error = "用户名或密码错误"; return "error"; } } } GrilAction.java package com.andrew.action; import com.opensymphony.xwork2.ActionSupport; public class GrilAction extends ActionSupport { @Override public String execute() throws Exception { System.out.println("GrilAction execute方法执行!"); return SUCCESS; } } MyInterceptor.java package com.andrew.interceptor; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class LoginInterceptor implements Interceptor { public void init() { System.out.println("LoginInterceptor初始化"); } public String intercept(ActionInvocation invocation) throws Exception { System.out.println("在Action执行之前"); ActionContext actionContext = invocation.getInvocationContext(); Map<String, Object> session = actionContext.getSession(); Object currentUser = session.get("currentUser"); String result = null; if (currentUser != null) { result = invocation.invoke(); } else { HttpServletRequest request = (HttpServletRequest)invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST); request.setAttribute("error", "请先登录!"); result = "error"; } System.out.println("result:" + result); System.out.println("在Action执行之后"); return result; } public void destroy() { System.out.println("LoginInterceptor销毁"); } } struts.xml <package name="manage" namespace="/" extends="struts-default"> <interceptors> <interceptor name="loginInterceptor" class="com.andrew.interceptor.LoginInterceptor"></interceptor> <interceptor-stack name="loginStack"> <interceptor-ref name="loginInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="loginStack"></default-interceptor-ref> <global-results> <result name="error">error.jsp</result> </global-results> <action name="user" class="com.andrew.action.UserAction"> <result name="success">success.jsp</result> <interceptor-ref name="defaultStack"></interceptor-ref> </action> <action name="gril" class="com.andrew.action.GrilAction"> <result name="success">success.jsp</result> </action> </package> success.jsp 当前用户:${currentUser.userName } login.jsp <form action="user" method="post"> 用户名:<input type="text" name="user.userName"/> 密码:<input type="password" name="user.password"/> <input type="submit" value="登录"/> </form> error.jsp 错误信息:${error } <a href="login.jsp">登录</a> LoginInterceptor初始化 http://localhost:8080/HeadFirstStruts2Chap03/login.jsp 123 123 submit 运行结果: 错误信息:用户名或密码错误 登录 http://localhost:8080/HeadFirstStruts2Chap03/gril 运行结果: 错误信息:请先登录! 登录 控制台: 在Action执行之前 result:error 在Action执行之后 http://localhost:8080/HeadFirstStruts2Chap03/login.jsp andrew 123456 submit 运行结果: 当前用户:andrew http://localhost:8080/HeadFirstStruts2Chap03/gril 运行结果: 当前用户:andrew 控制台: 在Action执行之前 GrilAction execute方法执行! result:success 在Action执行之后
上一篇: struts2标签
下一篇: struts2值栈与OGNL