六、页面传值(二)

@SessionAttributes类注解

将模型中的某个属性暂存到HttpSession中,以便多个请求之间可以共享这个属性。

  • 搭建环境
@Controller
public class MyController {
    @RequestMapping("first")
    public String  show(Model model){
        model.addAttribute("name","itlike001");
        return "/second.jsp";
    }
}
<h1>request=${requestScope.name}</h1>
<h1>session=${sessionScope.name}</h1>

可以发现,属性只存于request域中,而不在session域。

11861407-7e0775f7ea8b2320.PNG
1.PNG
  • 使用@SessionAttributes注解
@Controller
@SessionAttributes("name") //把"name"属性存到session当中(默认是value)
public class MyController {
    @RequestMapping("first")
    public String  show(Model model){
        model.addAttribute("name","itlike001");
        return "/second.jsp";
    }
}
11861407-7e0daefb508d571e.PNG
1.PNG
  • types参数

指定参数类型的模型数据放到session域当中

@Controller
@SessionAttributes(types = String.class) //把String类型的参数都保存到session
public class MyController {
    @RequestMapping("first")
    public String  show(Model model){
        model.addAttribute("name","itlike001");
        return "/second.jsp";
    }
}
  • value参数

通过指定key将model数据放到session域当中@SessionAttributes("name")

@SessionAttribute注解

使用@SessionAttribute来取出预先存在的全局会话属性

@Controller
public class MyController {
   @RequestMapping("first")
   /**
    *  在参数中使用@SessionAttribute注解
    */
   public String  show(@SessionAttribute("name") String name){
       /**
        *  从浏览器中取出key=name的session记录,绑定到参数name
        *  若浏览器没有该session,则报错
        */
       System.out.println(name);   //itlike001
       return "/second.jsp";
   }
}

@ModelAttribute

修改key名称

  • 原先:key == 首字母小写的类名
<form action="${pageContext.request.contextPath}/first">
    名称:<input type="text" name="name"><br>
    颜色:<input type="text" name="color"><br>
    <input type="submit" value="提交">
</form>
@Controller
public class MyController {
    @RequestMapping("first")
    public String  show(Dog dog, Model model){
        /**
         * Jsp发送页面数据,Dog类接收,并自动注入到model中
         * key值:类名(首字母小写)
         * 可以dog.name、dog.color在Jsp取出属性
         */
        System.out.println(dog); //toString:Dog{name='111', color='222'}
        System.out.println(model.asMap()); //Map:{dog=Dog{name='111', color='222'}
        return "/second.jsp";
    }
}
  • 修改key
@Controller
public class MyController {
    @RequestMapping("first")
    public String  show(@ModelAttribute("MyMine") Dog dog, Model model){
        /**
         * 可以看到:类名key => MyMine
         * {MyMine=Dog{name='111', color='222'}
         */
        System.out.println(dog); //toString
        System.out.println(model.asMap()); //Map:{MyMine=Dog{name='111', color='222'}
        return "/second.jsp";
    }
}

@ModelAttribute修饰方法

  • 在方法上使用@ModelAttribute,在对应映射之前执行该方法。
@ModelAttribute
public void first(){ // 方法名和访问名相同
    System.out.println("first----------");
}
@RequestMapping("first")
public String  show(Dog dog, Model model){
    System.out.println(model.asMap());
    return "/second.jsp";
}
//输出结果:
first----------
{MyMine=Dog{name='111', color='222'}
  • 在执行show之前,first默认传入Model
@ModelAttribute
public void first(Model model){ // 方法名和访问名相同
    // 在执行show之前,first默认传入Model
    System.out.println("first----------");
    model.addAttribute("name","itlike");
}
@RequestMapping("first")
public String  show(Dog dog, Model model){
    System.out.println(model.asMap());
    return "/second.jsp";
}
//输出结果:
first----------
{name=itlike, dog=Dog{name='111', color='222'}

注意点

Model会添加session里的内容。

猜你喜欢

转载自blog.csdn.net/weixin_34194379/article/details/87685091
今日推荐