向请求作用域中保存值的3种写法

如果要向请求作用域中保存值有3种写法

@RequestMapping("/hello1")
protected ModelAndView handleRequestInternal() {
    ModelAndView mv = new ModelAndView();
    mv.addObject("currentUser","admin");
    mv.setViewName("hello");//设置要跳转的视图名也会通过视图解析器解析
    return mv;
}
@RequestMapping("/hello2")
protected String hello2(HttpServletRequest request) {
    request.setAttribute("currentUser","admin");
    return "hello";//通过视图解析器调转到对应的hello.jsp页面
}
 @RequestMapping("/hello3")
    protected String hello3(Map map) {
        map.put("currentUser","admin");
        return "hello";
    }

视图解析器配置spring-mvc.xml

  
    
        
        
    

 

你可能感兴趣的:(向请求作用域中保存值的3种写法)