java springMVC之域对象

目录

测试网页代码

request域对象

原生Servlet共享数据

ModelAndView共享数据

Model共享数据

map共享数据

ModelMap共享数据

其他知识

session域对象

application域对象


测试网页代码

测试网页的所有代码,使用了thymeleaf。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<a th:href="@{/testAPI}">测试原生API向request域对象共享数据</a><br>
<a th:href="@{/testModelAndView}">测试ModelAndView向request域对象共享数据</a><br>
<a th:href="@{/testMode1}">测试Model向request域对象共享数据</a><br>
<a th:href="@{/testMap}">测试Map向request域对象共享数据</a><br>
<a th:href="@{/testModelMap}">测试ModelMap向request域对象共享数据</a><br>
<a th:href="@{/testSession}">测试session域对象共享数据</a><br>
<a th:href="@{/testApplication}">测试testApplication域对象共享数据</a><br>
</body>
</html>

接收网页的所有代码,使用了thymeleaf。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
<h1>测试成功</h1>
<p th:text="${key1}"></p>
<p th:text="${key2}"></p>
<p th:text="${key3}"></p>
<p th:text="${key4}"></p>
<p th:text="${key5}"></p>
<p th:text="${session.key6}"></p>
<p th:text="${application.key7}"></p>
</body>
</html>

request域对象

request域的各种共享数据的方法使用起来大同小异

原生Servlet共享数据

原生API作为基础不多解释,直接看代码

html代码

<a th:href="@{/testAPI}">测试原生API向request域对象共享数据</a><br>

Servlet代码

@RequestMapping(value = "/testAPI")
public String testAPI(HttpServletRequest request) {
    request.setAttribute("key1", "value1");
    return "test";
}

 页面接收代码

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

ModelAndView共享数据

html代码

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

接收代码

@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
    ModelAndView mav = new ModelAndView();
    mav.addObject("key2", "value2");//添加数据
    mav.setViewName("test");//设置视图名称
    return mav;
}

页面接收代码

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

 这种方式代码长度和原生API相比还更加长了,但在springMVC中推荐我们使用这一种方式。

Model共享数据

html代码

<a th:href="@{/testMode1}">测试Model向request域对象共享数据</a><br>

 接收代码

@RequestMapping("/testMode1")
public String testMode1(Model model) {
    model.addAttribute("key3", "value3");
    System.out.println(model.getClass().getName());
    return "test";
}

 页面接收代码

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

map共享数据

 html代码

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

 接收代码

@RequestMapping("/testMap")
public String testMap(Map<String, Object> map) {
    map.put("key4", "value4");
    System.out.println(map.getClass().getName());
    return "test";
}

页面接收代码

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

ModelMap共享数据

html代码

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

接收代码

@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap) {
    modelMap.addAttribute("key5", "value5");
    System.out.println(modelMap.getClass().getName());
    return "test";
}

页面接收代码 

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

其他知识

(1)Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的。上面的Model、ModelMap、Map方法中的输出内容都是BindingAwareModelMap。

 (2)不管使用上面方法中的哪一种方法,最终返回的都是打包好的ModelAndView对象,也就是上面所写的第二种方法,这也是为什么springMVC推荐使用这一种方式来进行共享数据操作。

session域对象

html代码 

<a th:href="@{/testSession}">测试session域对象共享数据</a><br>

接收代码

@RequestMapping("/testSession")
public String testSession(HttpSession session) {
    session.setAttribute("key6", "value6");
    return "test";
}

页面接收代码

session域接收数据时略有不同,要指定是session域下的键值

<p th:text="${session.key6}"></p>

application域对象

html代码

<a th:href="@{/testApplication}">测试testApplication域对象共享数据</a><br>

接收代码

@RequestMapping("/testApplication")
public String testApplication(HttpSession session) {
    ServletContext application = session.getServletContext();
    application.setAttribute("key7", "value7");
    return "test";
}

 页面接收代码

<p th:text="${application.key7}"></p>

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算不出来没办法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值