SpringMVC学习(4)—— 域对象(作用域)

域对象有4个:

①page(PageContextImpl):当前jsp页面范围内有效

②request(HttpServletRequest):一次请求响应范围有效,同一客户端的不同请求,无法获取域对象中的值

③session(HttpSession):一次会话范围有效,同一客户端在一次会话内的多个请求,都可以获取到session保存作用域内的值;可以看成一次浏览器关闭

④application(ServletContext):一次应用程序范围;可以看成一次服务器关闭

一.  向request域对象共享数据

1. 通过ServletAPI向request域对象共享数据

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ScopeController {
    @RequestMapping("/")   //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
    public String index(){
        return "index";
    }

    @RequestMapping("/requestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request){
        request.setAttribute("requestScope","hello");
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index页面</h1>

    <a th:href="@{/requestByServletAPI}">通过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>
    <h1>test页面</h1>

    <p th:text="${requestScope}"></p>    <!-- 获取request域对象中的数据 -->
                                         <!-- 获取到的结果是hello -->
</body>
</html>

2. 使用ModelAndView向request域对象共享数据(建议使用)

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ScopeController {
    @RequestMapping("/")   //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
    public String index(){
        return "index";
    }

    @RequestMapping("/requestByModelAndView")
    public ModelAndView testRequestByModelAndView(){
        ModelAndView mav = new ModelAndView();
        //向request域中存数据
        mav.addObject("requestScope","hello,ModelAndView");
        //设置视图名称
        mav.setViewName("test");

        return mav;
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index页面</h1>

    <a th:href="@{/requestByModelAndView}">通过ModelAndView向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>
    <h1>test页面</h1>

    <p th:text="${requestScope}"></p>    <!-- 获取request域对象中的数据 -->
                                         <!-- 获取到的结果是hello,ModelAndView -->
</body>
</html>

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

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ScopeController {
    @RequestMapping("/")   //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
    public String index(){
        return "index";
    }

    @RequestMapping("/requestByModel")
    public String testRequestByModel(Model model){
        model.addAttribute("requestScope", "hello,Model");
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index页面</h1>

    <a th:href="@{/requestByModel}">通过Model向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>
    <h1>test页面</h1>

    <p th:text="${requestScope}"></p>    <!-- 获取request域对象中的数据 -->
                                         <!-- 获取到的结果是hello,Model -->
</body>
</html>

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

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
public class ScopeController {
    @RequestMapping("/")   //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
    public String index(){
        return "index";
    }

    @RequestMapping("/requestByMap")
    public String testRequestByMap(Map<String,Object> map){
        map.put("requestScope", "hello,Map");
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index页面</h1>

    <a th:href="@{/requestByMap}">通过Map向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>
    <h1>test页面</h1>

    <p th:text="${requestScope}"></p>    <!-- 获取request域对象中的数据 -->
                                         <!-- 获取到的结果是hello,Map -->
</body>
</html>

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

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ScopeController {
    @RequestMapping("/")   //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
    public String index(){
        return "index";
    }

    @RequestMapping("/requestByModelMap")
    public String testRequestByModelMap(ModelMap modelMap){
        modelMap.addAttribute("requestScope", "hello,ModelMap");
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index页面</h1>

    <a th:href="@{/requestByModelMap}">通过ModelMap向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>
    <h1>test页面</h1>

    <p th:text="${requestScope}"></p>    <!-- 获取request域对象中的数据 -->
                                         <!-- 获取到的结果是hello,ModelMap -->
</body>
</html>

二.  向session域对象共享数据

1. 通过ServletAPI向session域对象共享数据(建议使用)

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class ScopeController {
    @RequestMapping("/")   //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
    public String index(){
        return "index";
    }

    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("sessionScope", "hello,session");
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index页面</h1>

    <a th:href="@{/testSession}">p向session域对象共享数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>test页面</h1>

    <p th:text="${session.sessionScope}"></p>    <!-- 获取session域对象中的数据,需要加上session. -->
                                                  <!-- hello,session -->
</body>
</html>

三.  向application域对象共享数据

1. 通过ServletAPI向application域对象共享数据(建议使用)

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

@Controller
public class ScopeController {
    @RequestMapping("/")   //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
    public String index(){
        return "index";
    }

    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext application = session.getServletContext();
        application.setAttribute("applicationScope", "hello,application");
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index页面</h1>

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

    <p th:text="${application.applicationScope}"></p>    <!-- 获取application域对象中的数据,需要加上application. -->
                                                         <!-- hello,application -->
</body>
</html>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值