zoukankan      html  css  js  c++  java
  • Struts2的学习-通配符和session对象

    一、 取得session 3种方法
    1、context.getSession() -->>Map对象

    2、HttpServletRequest request =
    (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);  
    再通过request 对象取得HttpSession

    3、实现SessionAware接口,让strtus自动注入session实例给我们。 
    二、
    1.创建新项目,基本和Struts的学习-属性驱动和模型驱动一样,主要修改两个文件的代码
    2.修改struts.xml

    2.sesstion方法,编写UserAction.java

    package com.nf.action;
    
    import com.nf.entity.User;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    import org.apache.struts2.interceptor.SessionAware;
    
    import java.util.Map;
    
    public class UserAction extends ActionSupport implements ModelDriven<User>{
    
        private User user = new User();
    
        //登录用的
        public String login(){
            System.out.println("用户名:"+user.getUserName());
            if ("ad".equals(user.getUserName())&&"123".equals(user.getUserPassword())){
                //登陆成功后,干什么?
                return this.SUCCESS;
            }else {
                return this.ERROR;
            }
        }
        //跳转到登陆页面的方法
        public String loginView(){
    
            return "loginViewSuccess";
        }
    
        //不需要手动调用
        public User getModel() {
            //告诉struts,你帮我对user进行赋值
            System.out.println("struts 调用我们的getModel,获得user的实例");
            return this.user;
        }
    }

    方法1:

    package com.nf.action;
    
    import com.nf.entity.User;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    import org.apache.struts2.interceptor.SessionAware;
    
    import java.util.Map;
    
    public class UserAction extends ActionSupport implements ModelDriven<User>{
    
        private User user = new User();
    
        //登录用的
        public String login(){
            System.out.println("用户名:"+user.getUserName());
            if ("ad".equals(user.getUserName())&&"123".equals(user.getUserPassword())){
                //登陆成功后,干什么?
    
                //第一种方法:通过context获得Map对象
                //主动获得实例
                ActionContext context = ActionContext.getContext();
                Map<String , Object> session = context.getSession();
                session.put("user",user);
    
                return this.SUCCESS;
            }else {
                return this.ERROR;
            }
    
        }
        //跳转到登陆页面的方法
        public String loginView(){
    
            return "loginViewSuccess";
        }
    
        //不需要手动调用
        public User getModel() {
            //告诉struts,你帮我对user进行赋值
            System.out.println("struts 调用我们的getModel,获得user的实例");
            return this.user;
        }
    }

    方法2:

    package com.nf.action;
    
    import com.nf.entity.User;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    import com.sun.net.httpserver.HttpServer;
    import org.apache.struts2.StrutsStatics;
    import org.apache.struts2.interceptor.SessionAware;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import java.util.Map;
    
    public class UserAction extends ActionSupport implements ModelDriven<User>{
    
        private User user = new User();
    
        //登录用的
        public String login(){
            System.out.println("用户名:"+user.getUserName());
            if ("ad".equals(user.getUserName())&&"123".equals(user.getUserPassword())){
                //登陆成功后,干什么?
    
               /* //第一种方法:通过context获得Map对象
                //主动获得实例
                ActionContext context = ActionContext.getContext();
                Map<String , Object> session = context.getSession();
                session.put("user",user);*/
    
                //第2种方法
                //对javaweb 产生强耦合
                ActionContext context = ActionContext.getContext();
                HttpServletRequest request = (HttpServletRequest)context.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);//这里的错误要导入
                HttpSession session = request.getSession();
                session.setAttribute("user",user);
    
                return this.SUCCESS;
            }else {
                return this.ERROR;
            }
    
        }
        //跳转到登陆页面的方法
        public String loginView(){
    
            return "loginViewSuccess";
        }
    
        //不需要手动调用
        public User getModel() {
            //告诉struts,你帮我对user进行赋值
            System.out.println("struts 调用我们的getModel,获得user的实例");
            return this.user;
        }
    }

    添加之后要选上

    方法3:

    package com.nf.action;
    
    import com.nf.entity.User;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    import com.sun.net.httpserver.HttpServer;
    import org.apache.struts2.StrutsStatics;
    import org.apache.struts2.interceptor.SessionAware;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import java.util.Map;
    
    public class UserAction extends ActionSupport implements ModelDriven<User>,SessionAware{
        //定义为null,不要管它怎么来
        private Map<String , Object> session = null;
    
        private User user = new User();
    
        //登录用的
        public String login(){
            System.out.println("用户名:"+user.getUserName());
            if ("ad".equals(user.getUserName())&&"123".equals(user.getUserPassword())){
                //登陆成功后,干什么?
    
               /* //第一种方法:通过context获得Map对象
                //主动获得实例
                ActionContext context = ActionContext.getContext();
                Map<String , Object> session = context.getSession();
                session.put("user",user);*/
    
                /*//第2种方法
                //对javaweb 产生强耦合
                ActionContext context = ActionContext.getContext();
                HttpServletRequest request = (HttpServletRequest)context.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
                HttpSession session = request.getSession();
                session.setAttribute("user",user);*/
    
                //第3种方法,实现SessionAware直接注入实例
                System.out.println("session:" +session);
                session.put("user",user);
    
    
                return this.SUCCESS;
            }else {
                return this.ERROR;
            }
    
        }
        //跳转到登陆页面的方法
        public String loginView(){
    
            return "loginViewSuccess";
        }
    
        //不需要手动调用
        public User getModel() {
            //告诉struts,你帮我对user进行赋值
            System.out.println("struts 调用我们的getModel,获得user的实例");
            return this.user;
        }
    
        //用于被框架注入实例用的,不需要手动调用//这里为方法SessionAware可以用快捷键ALT+ENTER
      public void setSession(Map <String, Object> map) { session = map; } }

      

  • 相关阅读:
    CF850A Five Dimensional Points 题解
    AT3963 [AGC024F] Simple Subsequence Problem 题解
    jquery事件绑定机制
    前端初级词汇
    一个checkbox细节处理方式(checkbox与后面的文字对其)
    转 CSS hack:针对IE6,IE7,firefox显示不同效果
    想到的几点页面规范
    jQuery UI Dialog:Demo2:实用技巧
    zindex
    递归示例
  • 原文地址:https://www.cnblogs.com/junhua4254/p/7601673.html
Copyright © 2011-2022 走看看