springMVC 域对象共享数据

1. servletApi

@GetMapping("testServletApi")
public String testServletApi(HttpServletRequest request){
    request.setAttribute("price","100");
    return "success";
}
<p th:text="${price}">金额:</p>

 

2 使用springMVC 中的方法共享域数据

2.1 ModelAndView

@GetMapping("modelAndView")
public ModelAndView modelAndView(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("price","888");
    modelAndView.setViewName("success");
    return  modelAndView;
}

2.2 Model

@GetMapping("model")
public String modelTest(Model model){
    model.addAttribute("price","999");
    return  "success";
}

2.3 Map

@GetMapping("map")
public String mapTest(Map<String,Object> map){
    map.put("price","111");
    return  "success";
}

 

3 往 session 中共享数据

 @GetMapping("session")
 public String session(HttpSession session){
     session.setAttribute("price","222");
     return  "success";
 }

 

4 application共享数据

@GetMapping("appliaction")
public String appliaction(HttpSession session){
    ServletContext servletContext = session.getServletContext();
    servletContext.setAttribute("price","333");
    return  "success";
}

 

posted @ 2022-09-06 22:48  写代码的小哥哥  阅读(30)  评论(0)    收藏  举报