3.SpringMvc使用ModelAndView、Model、Map、ModelMap向requestsession、application域对象共享数据(入门)

四大域对象包括:pageContext、request、session和application,而pageContext有效范围为一个jsp页面,太小了,而application有效范围为整个web工程,又太大了。因此我们平时的开发中在域对象中共享数据主要是用request和session
 

四个域对象分别是:

pageContext (PageContextImpl 类) 同一个 jsp 页面范围内有效
request (HttpServletRequest 类)一次请求内有效
session (HttpSession 类) 一个会话范围内有效(打开浏览器访问服务器,直到关闭浏览器)
application (ServletContext 类) 整个 web 工程范围内都有效(只要 web 工程不停止,数据都在)

一、向 request 域对象共享数据

(一)使用ServletAPI向request域对象共享数据

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;

@Controller
public class MyController {

    @RequestMapping(value = "/testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request){
        request.setAttribute("testRequestScope","hello servletAPI");
        return "target";
    }
}

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

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
这是target.html<br>
<p th:text="${testRequestScope}"></p>
<!--这是thymeleaf的语法,访问request域中数据只需要写attribute的名字,
而如果是session域,就要写session.名字;
如果是application域,就要写application.名字-->
</body>
</html>

二、向 session 域对象共享数据

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;

@Controller
public class MyController {

    @RequestMapping(value = "/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("testSessionScope","hello session");
        return "target";
    }
}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这个是thymeleaf命名空间-->
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
这是index.html<br>
<a th:href="@{/testSession}">向session域对象共享数据</a><br>
</body>
</html>

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
这是target.html<br>
<p th:text="${session.testSessionScope}"></p>
<!--这是thymeleaf的语法,访问request域中数据只需要写attribute的名字,
而如果是session域,就要写session.名字;
如果是application域,就要写application.名字-->
</body>
</html>

三、向 application 域对象共享数据

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

@Controller
public class MyController {

    @RequestMapping(value = "/testApplication")
    public String testApplication(HttpSession session){
        ServletContext application = session.getServletContext();
        application.setAttribute("testApplicationScope","hello application");
        return "target";
    }
}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这个是thymeleaf命名空间-->
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
这是index.html<br>
<a th:href="@{/testApplication}">向application域对象共享数据</a><br>
</body>
</html>

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
这是target.html<br>
<p th:text="${application.testApplicationScope}"></p>
<!--这是thymeleaf的语法,访问request域中数据只需要写attribute的名字,
而如果是session域,就要写session.名字;
如果是application域,就要写application.名字-->
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值