Struts_3配置自定义拦截器
来源:互联网 发布:网络选修课答案公众号 编辑:程序博客网 时间:2024/06/07 05:42
strtus.xml配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"><struts> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/user" extends="struts-default"><!--这个是自己配置的拦截器,name是自己起的名字,class是自己配置的拦截器的那个类的地址,注意一定要放在使用的action的前面。在package里面,在action标签的外面--><interceptors> <interceptor name="LoginInt" class="com.sxt.interceptor.LoginInterceptor"></interceptor> </interceptors> <action name="user_*" class="com.sxt.action.BuserAction" method="{1}"> <!--这个是登陆的成功,可忽略--> <result name="success">/WEB-INF/main.jsp</result> <!--这个是用于测试拦截器有没有配置成功--> <result name="toMain">/WEB-INF/main.jsp</result> <!--这个是拦截器拦住了跳转到哪个地方--> <result name="login">/login.jsp</result> <!--这个是登陆错误的跳转,可忽略--> <result name="login_error">/login.jsp</result> <!--这个是注册成功了,直接自动登陆的,可忽略--> <result name="toLogin" type="chain">user_login</result> <!--这个是注册失败的跳转,可忽略--> <result name="reg_error">/reg.jsp</result> <!--这个是默认的拦截器的调用,因为你一旦自己写了后,他将不会调用默认的,所以要重新调用一次--> <interceptor-ref name="defaultStack"></interceptor-ref> <!--这个是要调用的自己定义的监听器的名字,注意和上面自己定义的监听器的名字是对应的--> <interceptor-ref name="LoginInt"> <!--这个里面的param是自己定义的,因为这是用于判断进入main页面之前有没有登录,但是由于开始是没有注册和登陆的,所以根本进不去action里面的(因为拦截器会走在action方法之前的),所有有一些方法要放行,比如登陆,注册,所以这里定义一个要传往自定义拦截器的字符串,用于判断要不要拦截--> <!-- 需要放行的方法 --> <param name="url">user_login,user_register</param> </interceptor-ref> <!--这个是要放行的方法,可忽略--> <allowed-methods>login,register,toMain</allowed-methods> </action> </package></struts>
前台登陆和注册页面
登陆:
<body> <form action="user/user_login" method="post"> 账户:<input type="text" name="b.busername"><br> 密码:<input type="password" name="b.buserpwd"><br> <input type="submit" value="登陆">${login_info }<br> <input type="button" value="注册" onclick="location.href='reg.jsp'"> </form></body>
注册:
<body> <form action="user/user_register" method="post"> 账户:<input type="text" name="b.busername"><br> 密码:<input type="password" name="b.buserpwd"><br> 用户名:<input type="text" name="b.nname"><br> <input type="submit" value="注册">${reg_info }<br> <input type="button" value="登陆" onclick="location.href='login.jsp'"> </form> </body>
action:
public class BuserAction { private Buser b; private BuserService dao = new BuserServiceImpl(); /** * 用户登陆 * * @return */ public String login() { ActionContext sc = ActionContext.getContext(); Map<String, Object> session = sc.getSession(); try { if (b != null) { Buser u = dao.login(b); if (u != null) { // 说明登陆成功 session.put("u", u); return "success"; } else { session.put("login_info", "账户或者密码不正确"); return "login_error"; } } } catch (Exception e) { e.printStackTrace(); } return null; }//用于测试拦截器有没有成功,因为注册和登陆是要带参数的,所有不好测试 public String toMain(){ return "toMain"; } /** * 用户注册 * * @return */ public String register() { ActionContext sc = ActionContext.getContext(); Map<String, Object> session = sc.getSession(); try { boolean a = dao.register(b); if (a) { // 注册成功 return "toLogin"; } else { // 注册失败 session.put("reg_info", "注册失败"); return "reg_error"; } } catch (Exception e) { e.printStackTrace(); } return null; }
拦截器:
自定义的拦截器要继承AbstractInterceptor
重新它的方法
public class LoginInterceptor extends AbstractInterceptor { //这个是从Struts.xml传过来的要忽略的请求 private String url; @Override public String intercept(ActionInvocation invocation) throws Exception { //获取代理 ActionProxy proxy = invocation.getProxy(); //获取action代理所应用的action的名字(也就是请求的中的action的名字,也就是 //`<form action="user/user_login" method="post">`) //中的user_login String actionName = proxy.getActionName(); System.out.println("----------------"+actionName); boolean flag=false; // 默认为false;定义一个标记 if(url!=null){ String[] ss = url.split(",");//用,劈成数组 //用arrays的方法,把它放在集合中, List<String> ls = Arrays.asList(ss); //盘龙这个集合中有没有这个的名字,如果是让放行的话,就把标记改为true,如果没有的话,就是false flag = ls.contains(actionName); } //用登陆的时候有没有往session中放对象来判断是不是登陆过了, //如果没有登陆过,并且不是登陆和注册的方法的话,就拦截下来, //如果是登陆过了,就放行, //如果是没有登陆过,是登陆或者注册的话,就放行 ActionContext ac = ActionContext.getContext(); Map<String, Object> session = ac.getSession(); Buser b=(Buser)session.get("u"); if(b==null && !flag){ //打回去 Action.LOGIN这个里面返回的是login return Action.LOGIN; }else{ //放行 return invocation.invoke(); } }
注意,这个拦截器只拦截从action中跳转到main里面的,如果从路径那个地方直接敲main.jsp的话拦截器不起任何作用,所以为了在路径敲也无用,把main.jsp放在WEB-INF
文件夹下面了,和web.xml在在同一个文件夹内,这样就无法再页面敲入地址直接访问,但这样做,在struts.xml的配置文件中跳转的时候要加上/WEB-INF
,也就是这个<result name="success">/main.jsp</result>
,变为了这个<result name="success">/WEB-INF/main.jsp</result>
阅读全文
0 0
- Struts_3配置自定义拦截器
- struts2配置自定义拦截器
- springMVC自定义拦截器配置
- 配置自定义拦截器(全局配置)
- 扩展拦截器接口的自定义拦截器配置
- 继承方法拦截器的自定义拦截器配置
- 如何配置自定义struts2拦截器
- Struts2 自定义拦截器配置和使用
- struts2自定义拦截器注解配置方法
- struts2的自定义拦截器和配置
- struts2中自定义拦截器的配置
- springmvc自定义拦截器配置和使用
- Spring MVC配置自定义拦截器
- struts2自定义拦截器及其配置(备份)
- struts2自定义拦截器并配置拦截器使其生效
- springmvc自定义注解拦截器方式实现注解功能拦截器的配置
- struts2 自定义拦截器
- 自定义拦截器(转)
- ArrayList扩容机制
- 小辣椒LA2-W线刷刷机教程_红辣椒LA2-W线刷rom包_救砖刷机包
- 文章标题
- modelSIM编程出现错误代码vlog-66
- 需求推动技术发展
- Struts_3配置自定义拦截器
- boost asio 应用方法学(二)——深入框架
- python画图第二篇
- 基于 txt 文件的通讯录系统
- NDK开发系列一
- maven的安装与详细配置亲测可用
- 如何从0到1搭建电商促销系统?
- cocos2dx的Http请求与zip文件多线程解压
- opencv imread函数一点心得