SpringMVC——域对象共享数据

域对象共享数据

文章目录

  • 域对象共享数据
    • 1、使用 ServletAPI 向 request 域对象共享数据
    • 2、使用 ModelAndView 向request 域对象共享数据:推荐使用
    • 3、使用 Model 向 request 域对象共享数据
    • 4、使用 map 向 request 域对象共享数据
    • 5、使用 ModelMap 向 request 域对象共享数据
    • 6、Model、ModelMap、Map 的关系
    • 7、向 session 域共享数据
    • 8、向 application 域共享数据

1、使用 ServletAPI 向 request 域对象共享数据

<a th:href="@{/testRequestByServletAPI}">通过servletAPI向request域对象共享数据a><br/>
@RequestMapping("/testRequestByServletAPI")
//使用servletAPI向request域对象共享数据
public String testRequestByServletAPI(HttpServletRequest request){
    request.setAttribute("testRequestScope", "hello, servletAPI");
    return "success";
}
<p th:text="${testRequestScope}">p>

2、使用 ModelAndView 向request 域对象共享数据:推荐使用

<a th:href="@{/testModelAndView}">通过ModelAndView向request域对象共享数据a><br/>
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
    ModelAndView mav = new ModelAndView();
    //处理模型数据,即向请求域request共享数据
    mav.addObject("testRequestScope", "hello, ModelAndView");
    //设置视图名称
    mav.setViewName("success");
    return mav;
}

注意:

  • 返回值必须是 ModelAndView;

3、使用 Model 向 request 域对象共享数据

<a th:href="@{/testModel}">通过Model向request域对象共享数据a><br/>
@RequestMapping("/testModel")
public String testModel(Model model){
    model.addAttribute("testRequestScope", "hello, Model");
    return "success";
}

4、使用 map 向 request 域对象共享数据

<a th:href="@{/testMap}">通过Map向request域对象共享数据a><br/>
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
    map.put("testRequestScope", "hello, Map");
    return "success";
}

5、使用 ModelMap 向 request 域对象共享数据

<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据a><br/>
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
    modelMap.addAttribute("testRequestScope", "hello, ModelMap");
    return "success";
}

6、Model、ModelMap、Map 的关系

Model、ModelMap、Map 类型的参数其实本质上都是 BindingAwareModelMap 类型的。

@RequestMapping("/testModel")
public String testModel(Model model){
    model.addAttribute("testRequestScope", "hello, Model");
    System.out.println(model.getClass().getName());
    //org.springframework.validation.support.BindingAwareModelMap
    return "success";
}

@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
    map.put("testRequestScope", "hello, Map");
    System.out.println(map.getClass().getName());
    //org.springframework.validation.support.BindingAwareModelMap
    return "success";
}

@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
    modelMap.addAttribute("testRequestScope", "hello, ModelMap");
    System.out.println(modelMap.getClass().getName());
    //org.springframework.validation.support.BindingAwareModelMap
    return "success";
}

public interface Model{}

public class ModelMap extends LinkedHashMap {}

public class ExtendedModelMap extends ModelMap implements Model {}

public class BindingAwareModelMap extends ExtendedModelMap {}


7、向 session 域共享数据

<a th:href="@{/testSession}">通过servletAPI向session域对象共享数据a><br/>
@RequestMapping("/testSession")
public String testSession(HttpSession httpSession){
    httpSession.setAttribute("testSessionScope", "hello, session");
    return "success";
}
<p th:text="${session.testSessionScope}">p>

8、向 application 域共享数据

<a th:href="@{/testApplication}">通过servletAPI向application域对象共享数据a><br/>
@RequestMapping("/testApplication")
public String testApplication(HttpSession httpSession){
    ServletContext application = httpSession.getServletContext();
    application.setAttribute("testApplicationScope", "hello, application");
    return "success";
}
<p th:text="${application.testApplicationScope}">p>

你可能感兴趣的:(java,mvc,前端)