1.拦截器是AOP的一种实现。
拦截器和AOP底层都是通过动态代理实现的
2.如何配置自己的拦截器,实现如果用户没有登录,直接请求BookList,跳转到登录页。
1.建一个用户实体类:
public class UserInfo { 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; } }
2.建两个Action类:
01.BookList:
public class BookList extends ActionSupport implements SessionAware{ private UserInfo info; Map<String, Object> map; @Override public String execute() throws Exception { if (info!=null) { if (info.getUsername().equals("1")&&info.getPassword().equals("1")) { map.put("uname", info.getUsername()); return SUCCESS; }else { return LOGIN; } }else { return LOGIN; } } @Override public void setSession(Map<String, Object> map) { this.map=map; } public UserInfo getInfo() { return info; } public void setInfo(UserInfo info) { this.info = info; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } }
2.MyList:
public class MyList extends ActionSupport{ public String list(){ return "list"; } }
02.建一个拦截器类:
public class MyInterceptor implements Interceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("拦截器执行----------intercept"+invocation.getProxy().getMethod()); //获取session对象 Map<String, Object> session = ActionContext.getContext().getSession(); //1.获取session的value值 Object name = session.get("uname"); String actionName = invocation.getProxy().getActionName(); //3.保存invoke()放行的结果 “success” String value=""; if (actionName.equals("loginAction")) { value = invocation.invoke(); }else { if (name!=null) { //证明用户登录了 //放行 请求执行其他拦截器 后者具体的Action value= invocation.invoke(); }else{ value="login"; } System.out.println("逻辑视图名"+value); } return value; } /** * 1,2 拦截器销毁的方法,在销毁之前执行,只被执行一次 */ @Override public void destroy() { System.out.println("拦截器销毁----------destory"); } /** * 1,1 初始化方法,只被执行一次 */ @Override public void init() { System.out.println("拦截器执行----------init"); }
3.Struts-xml:
<struts> <!-- 修改文件 tomcat不用重新启动 --> <constant name="struts.devMode" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <package name="main" namespace="/" extends="struts-default"> <!-- 配置拦截器 --> <interceptors> <interceptor name="myinter" class="cn.happy.interceptor.MyInterceptor"></interceptor> <interceptor-stack name="mystack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="myinter"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="mystack"/> <action name="loginAction" class="cn.happy.action.BookList"> <result name="success">/myinter/success.jsp</result> <result name="login">/myinter/login.jsp</result> </action> </package> <include file="cn/happy/action/list.xml"></include> </struts>
4.list.xml:
<struts> <!-- 修改文件 tomcat不用重新启动 --> <constant name="struts.devMode" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- extends:struts.xml里name属性的值 --> <package name="book" namespace="/book" extends="main"> <action name="bookAction" class="cn.happy.action.MyList" method="list"> <result name="list">/myinter/list.jsp</result> <result name="success">/myinter/success.jsp</result> <result name="login">/myinter/login.jsp</result> </action> </package> </struts>