美文网首页
Java mac idea Struts2的使用04

Java mac idea Struts2的使用04

作者: 编程_书恨少 | 来源:发表于2018-08-24 13:42 被阅读0次

    1. 拦截器创建

    拦截器生命周期:随项目的启动而创建,随项目关闭而销毁

    方式一:

    //拦截器:第一种创建方式
    //拦截器生命周期:随项目的启动而创建,随项目关闭而销毁
    public class MyInterceptor implements Interceptor {
        @Override
        //初始化方法
        public void init() {
            
        }
    
        @Override
        //拦截方法
        public String intercept(ActionInvocation arg0) throws Exception {
            return null;
        }
    
        
        @Override
        //销毁方法
        public void destroy() {
            
        }
    }
    

    方式二:

    //创建方式2: 继承AbstractInterceptor -> struts2的体贴
    //帮我们空实现了init 和 destory方法. 我们如果不需要实现这两个方法,就可以只实现intercept方法
    public class MyInterceptor2 extends AbstractInterceptor {
    
        @Override
        public String intercept(ActionInvocation arg0) throws Exception {
            
            return null;
        }
    
    }
    

    方式三:

    //继承:MethodFilterInterceptor 方法过滤拦截器
    //功能: 定制拦截器拦截的方法.
    //  定制哪些方法需要拦截.
    //  定制哪些方法不需要拦截
    public class MyInterceptor3 extends MethodFilterInterceptor{
    
        @Override
        protected String doIntercept(ActionInvocation invocation) throws Exception {
            //前处理
            System.out.println("MyInterceptor3 的前处理!");
            //放行
            String result = invocation.invoke();
            //后处理
            System.out.println("MyInterceptor3 的后处理!");
            
            // 根据结果来进行页面的跳转处理
            return result;
        }
    }
    

    2. 拦截器配置

    拦截action的请求
    Demo1Action

    public class Demo1Action extends ActionSupport {
    
    
        @Override
        public String execute() throws Exception {
            System.out.println("Demo1Action");
            return SUCCESS;
        }
    }
    

    拦截器MyInterceptor3

    //继承:MethodFilterInterceptor 方法过滤拦截器
    //功能: 定制拦截器拦截的方法.
    //  定制哪些方法需要拦截.
    //  定制哪些方法不需要拦截
    public class MyInterceptor3 extends MethodFilterInterceptor{
    
        @Override
        protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
    
            //前处理
            System.out.println("MyInterceptor3 的前处理!");
            //放行
            String result = actionInvocation.invoke();
            //后处理
            System.out.println("MyInterceptor3 的后处理!");
    
            return result;
        }
    
    }
    

    在配置中设置哪些方法进行拦截,哪些方法不拦截

    <struts>
    
        <package name="inter" namespace="/" extends="struts-default" >
            <interceptors>
                <!-- 1.注册拦截器 -->
                <interceptor name="myInter3" class="test.MyInterceptor3"></interceptor>
    
                <!-- 2.注册拦截器栈 -->
                <interceptor-stack name="myInterceptorStack">
                    <!-- 自定义拦截器引入(建议放在20个拦截器之前) -->
                    <interceptor-ref name="myInter3">
                        <!-- 指定哪些方法不拦截
                         <param name="excludeMethods">add,delete</param> -->
                        <!-- 指定哪些方法需要拦截 -->
                        <param name="includeMethods">add,delete,execute</param>
                    </interceptor-ref>
                    <!-- 引用默认的拦截器栈(20个) -->
                    <interceptor-ref name="defaultStack"></interceptor-ref>
                </interceptor-stack>
            </interceptors>
    
            <!-- 3.指定包中的默认拦截器栈 -->
            <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>
    
            <action name="Demo1Action_*" class="test.Demo1Action" method="{1}" >
                <!-- 也可以为Action单独指定走哪个拦截器(栈)
                <interceptor-ref name="myStack"></interceptor-ref>
                -->
                <result name="success" type="dispatcher" >/index.jsp</result>
            </action>
        </package>
    
    
    
        <package name="test" namespace="/test" extends="struts-default">
    
            <action name="TestAction_*" class="test.TestAction" method="{1}">
    
                <result name="success" type="dispatcher" >/hello.jsp</result>
    
            </action>
        </package>
    </struts>
    

    3. 登录拦截

    1. 提供登录的action
    public class UserAction extends ActionSupport implements ModelDriven<User> {
    
        private UserService userService = new UserServiceImpl();
        private User user = new User();
    
    
        public String login() throws Exception {
    
            User u = userService.findUser(user);
    
            ActionContext.getContext().getSession().put("user", u);
    
            return "toHome";
        }
    
        @Override
        public User getModel() {
            return user;
        }
    }
    
    
    public class UserServiceImpl implements UserService {
    
        private UserDao userDao = new UserDaoImpl();
    
        @Override
        public User findUser(User user) {
    
            Session session = HibernateUtils.getCurrentSession();
    
            // 开启事务
            session.beginTransaction();
    
            User findUser = userDao.findUser(user);
    
            // 提交事务
            session.getTransaction().commit();
    
            if (findUser == null) {
                throw  new RuntimeException("账户名不存在");
            }
    
            if (!findUser.getUser_password().equals(user.getUser_password())) {
                throw new RuntimeException("密码错误");
            }
    
            return findUser;
        }
    }
    
    
    public class UserDaoImpl implements UserDao {
    
        @Override
        public User findUser(User user) {
    
            Session session = HibernateUtils.getCurrentSession();
    
            String hql = "from User where user_code = ?";
    
            Query query = session.createQuery(hql);
    
            query.setParameter(0, user.getUser_code());
    
            User user1 = (User) query.uniqueResult();
    
            return user1;
        }
    }
    
    1. 提供
      登录过滤器LoginInterceptor
    public class LoginInterceptor extends MethodFilterInterceptor {
        
        @Override
        protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
    
            // 1. 获取session
            Map<String, Object> session = ActionContext.getContext().getSession();
    
            // 2.获取登录标识
            User user = (User) session.get("user");
    
            // 3.判断登录标识是否存在
            if (user == null) {
                return "toLogin";
            } else  {
                actionInvocation.invoke();
            }
            return null;
        }
    }
    
    1. 配置文件中进行配置

    注意点:

    1.指定包中的默认拦截器栈,并且指定过滤哪些方法
    2.定义全局结果集,用来给所有的Action进行统一的结果处理
    3.定义全包的异常处理,error对应到相应的结果集
    <struts>
    
        <!-- i18n:国际化. 解决post提交乱码 -->
        <constant name="struts.i18n.encoding" value="UTF-8"></constant>
        <constant name="struts.devMode" value="true"></constant>
    
    
        <package name="crm" namespace="/" extends="struts-default">
    
            <!--注册拦截器-->
            <interceptors>
                <interceptor name="loginInterceptor" class="web.interceptor.LoginInterceptor">
                    <param name="excludeMethods">login</param>
                </interceptor>
    
                <!--注册拦截器栈-->
                <interceptor-stack name="crmInterceptors">
                    <interceptor-ref name="loginInterceptor"></interceptor-ref>
                    <interceptor-ref name="defaultStack"></interceptor-ref>
                </interceptor-stack>
            </interceptors>
            
            <!--指定包中的默认拦截器栈-->
            <default-interceptor-ref name="crmInterceptors"></default-interceptor-ref>
            
            <!-- 定义全局结果集,用来给所有的Action进行统一的结果处理 -->
            <global-results>
                <result name="toLogin" type="redirect" >/login.jsp</result>
            </global-results>
    
            <!--定义全包的异常处理,error对应到相应的结果集-->
            <global-exception-mappings>
                <!-- 如果出现java.lang.RuntimeException异常,就将跳转到名为error的结果 -->
                <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
            </global-exception-mappings>
    
            <action name="CustomerAction_*" class="web.CustomerAction" method="{1}" >
           
                <result name="list" type="dispatcher" >/jsp/customer/list.jsp</result>
    
                <!--重定向到 CustomerAction_list-->
                <result name="toList" type="redirectAction" >
                    <param name="actionName" >CustomerAction_list</param>
                    <param name="nameSpace">/</param>
                </result>
            </action>
    
            <action name="UserAction_*" class="web.UserAction" method="{1}">
                <result name="toHome" type="redirect">/index.htm</result>
                <result name="error"  type="redirect">/login.jsp</result>
            </action>
        </package>
    
    </struts>
    
    

    jsp页面登录拦截

      <script type="text/javascript">
          window.onload=function(){
    
              if(window.parent != window){// 如果是在框架中
                  //就让框架页面跳转到登陆页面
                  window.parent.location.href = "${pageContext.request.contextPath}/login.jsp";
              }
    
          };
      </script>
    

    4. struts2标签

    相关文章

      网友评论

          本文标题:Java mac idea Struts2的使用04

          本文链接:https://www.haomeiwen.com/subject/tdmyiftx.html