1. 程式人生 > 其它 >springMVC域物件共享資料

springMVC域物件共享資料

1、使用ServletAPI向request域物件共享資料

//1、使用ServletAPI向request域物件共享資料
    @RequestMapping("/textBySerlvetApi")
    public String textByServletApi(HttpServletRequest request){
        request.setAttribute("text","hello servletAPI");
        return "succes";

2、使用ModelAndView向request域物件共享資料

//2、使用ModelAndView向request域物件共享資料
    @RequestMapping("/testModelAndView")
    public ModelAndView testModeiAndView(){
        /**
         * ModelAndView有Model和View的功能
         * Model主要用於向請求域共享資料
         * View主要用於設定檢視,實現頁面跳轉
         */
        ModelAndView mav=new ModelAndView();
        //向請求域共享資料
        mav.addObject("text","hello modelandview");
        //設定檢視,實現頁面跳轉
        mav.setViewName("succes");
        return mav;
    }

3、使用Model向request域物件共享資料

//2、使用Model向request域物件共享資料
    @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("text","hello model");
        return "succes";
    }

4、使用map向request域物件共享資料

//4、使用Map向request域物件共享資料
    @RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
        map.put("text","hello map");
        return "succes";
    }

5、使用ModelMap向request域物件共享資料

//5、使用ModelMap向request域物件共享資料
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("text","hello modelmap");
        return "succes";

    }

6、Model、ModelMap、Map的關係

Model、ModelMap、Map型別的引數其實本質上都是 BindingAwareModelMap 型別的

public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

7、向session域共享資料

//6 使用servletAPI向session域共享資料
   @RequestMapping("/testSession")
   public  String testSession(HttpSession session){
       session.setAttribute("text","hello session");
       return "succes";
   }

8、向application域共享資料

//7 使用servletAPI向application域共享資料
    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext application= session.getServletContext();
        application.setAttribute("test","hello application");
        return "succes";
    }

html5
index.html5

<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>succes</h1>
<a th:href="@{/textBySerlvetApi}">使用ServletAPI向request域物件共享資料</a><br>
<a th:href="@{/testModelAndView}">使用ModelAndView向request域物件共享資料</a><br>
<a th:href="@{/testModel}">使用Model向request域物件共享資料</a><br>
<a th:href="@{/testMap}">使用Map向request域物件共享資料</a><br>
<a th:href="@{/testModelMap}">使用ModelMap向request域物件共享資料</a><br>
<a th:href="@{/testSession}">使用servletAPI向session域物件共享資料</a><br>
<a th:href="@{/testApplication}">使用servletAPI向application域物件共享資料</a><br>
</body>
</html>

succes.html5

<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--12345-->
<p th:text="${text}"></p>
<!--6-->
<p th:text="${session.text}"></p>
<!--7-->
<p th:text="${application.test}"></p>
</body>
</html>