SpringMVC-域对象共享数据

五、域对象共享数据

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

既然使用SpringMVC了,就尽量少用该方法,可以使用以下四种方法中的任意一种。

html中的设置

index.html中

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testRequestByServletAPI}">通过servletAPI向request域对象共享数据</a>
</body>
</html>

success.html中

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
success<br>
<p th:text="${testRequestScope}"></p>
</body>
</html>

控制器中的设置

//使用servletAPI向request域对象共享数据
@RequestMapping("/testRequestByServletAPI")
public String testRequestByServletAPI(HttpServletRequest request){
    //设置名称和共享数据
    request.setAttribute("testRequestScope", "hello,servletAPI");
    return "success";
}

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

该方法为SpringMVC中建议使用的向request域对象共享数据的方法

html中的设置

index.html中

<a th:href="@{/testModelAndView}">通过ModelAndView向request域对象共享数据</a><br>

success.html中

<p th:text="${testRequestScope}"></p>

控制器中的设置

//使用ModelAndView向request域对象共享数据
@RequestMapping("/testModelAndView")
//使用ModelAndView向request域对象共享数据时,方法的返回值必须为ModelAndView
public ModelAndView testModelAndView(){
    ModelAndView mav = new ModelAndView();
    //处理模型数据,即向请求域request共享数据
    mav.addObject("testRequestScope", "hello,ModelAndView");
    //设置视图名称
    mav.setViewName("success");
    return mav;
}

注:

使用ModelAndView向request域对象共享数据时,方法的返回值必须为ModelAndView

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

html中的设置

index.html中

<a th:href="@{/testModel}">通过Model向request域对象共享数据</a><br>

success.html中

<p th:text="${testRequestScope}"></p>

控制器中的设置

//使用Model向request域对象共享数据
@RequestMapping("/testModel")
public String testModel(Model model){
    //设置名称和共享数据
    model.addAttribute("testRequestScope", "hello,model");
    return "success";
}

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

html中的设置

index.html中

<a th:href="@{/testMap}">通过Map集合向request域对象共享数据</a><br>

success.html中

<p th:text="${testRequestScope}"></p>

控制器中的设置

//使用Map向request域对象共享数据
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
    //向map集合中存放名称和共享数据
    map.put("testRequestScope", "hello,map");
    return "success";
}

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

html中的设置

index.html中

<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据</a><br>

success.html中

<p th:text="${testRequestScope}"></p>

控制器中的设置

//使用ModelMap向request域对象共享数据
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
    //设置名称和共享数据
    modelMap.addAttribute("testRequestScope", "hello,ModelMap");
    return "success";
}

6、Model、ModelMap、Map的关系

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

public interface Model{}
public class ModelMap extends LinkHashMap<String, Object>{}
public class ExtendedModel extends ModelMap implements Model{}
public class BingingAwareModelMap extends ExtendedModelMap{}

7、向session域共享数据

html中的设置

index.html中

<a th:href="@{/testSession}">通过servletAPI向Session域对象共享数据</a><br>

success.html中

<!--thymeleaf中要访问Session中的数据时通过session.xxx来访问-->
<p th:text="${session.testSessionScope}"></p>

控制器中的设置

//使用servletAPI向Session域对象共享数据
@RequestMapping("/testSession")
public String testSession(HttpSession session){
    //设置名称和共享数据
    session.setAttribute("testSessionScope", "hello,session");
    return "success";
}

8、向application域共享数据

html中的设置

index.html中

<a th:href="@{/testApplication}">通过servletAPI向Application域对象共享数据</a><br>

success.html中

<!--thymeleaf中要访问Application中的数据时通过application.xxx来访问-->
<p th:text="${application.testApplicationScope}"></p>

控制器中的设置

//使用servletAPI向Application域对象共享数据
@RequestMapping("/testApplication")
public  String testApplication(HttpSession session){
    //通过session.getServletContext()方法获取application对象
    ServletContext application = session.getServletContext();
    //设置名称和共享数据
    application.setAttribute("testApplicationScope", "hello,application");
    return "success";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值