Java框架 SpringMVC--域对象共享数据

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

Java代码:

@Controller
@RequestMapping("/test/scope")
public class TestScopeController {

    @RequestMapping("/servlet")
    public String testScopeByServletAPI(HttpServletRequest request) {
        request.setAttribute("testRequestScope", "hello servlet");
        return "target";
    }
}

index.html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
    <title>首页</title>
</head>
<body>
    <h1>首页</h1>
    <a th:href="@{/test/scope/servlet}">测试ServletAPI向request域对象共享数据</a>
</body>
</html>

target.html 代码:

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

点击 index.html 中的超链接,结果为:

 

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

Java代码:

    @RequestMapping("/modelAndView")
    public ModelAndView testScopeByModelAndView() {
        /**
         * ModelAndView有Model和View的功能
         * Model主要用于向请求域共享数据
         * View主要用于设置视图,实现页面跳转
         */
        ModelAndView mav = new ModelAndView();
        // 向请求域共享数据
        mav.addObject("testScopeByModelAndView", "hello testScopeByModelAndView");
        // 设置视图,实现页面跳转
        mav.setViewName("target");
        return mav;
    }

index.html代码:

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

点击 index.html 中的目标超链接,结果为:

 

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

Java代码:

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

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

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

index.html代码:

    <a th:href="@{/test/scope/model}">测试 Model 向 request 域对象共享数据</a><br/>
    <a th:href="@{/test/scope/modelMap}">测试 ModelMap 向 request 域对象共享数据</a><br/>
    <a th:href="@{/test/scope/map}">测试 Map 向 request 域对象共享数据</a><br/>

ModelModelMapMap类型的参数其实本质上都是 BindingAwareModelMap 类型的

public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

4、向session域共享数据

Java代码:

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

index.html代码:

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

target.html代码:

<body>
    <h1>Hello World</h1>
    <p th:text="${testRequestScope}"></p>
    <p th:text="${session.testSessionScope}"></p>
</body>

点击 index.html 中的对应的目标超链接,结果为:

5、向application域共享数据

Java代码:

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

index.html代码:

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

target.html代码:

<body>
    <h1>Hello World</h1>
    <p th:text="${testRequestScope}"></p>
    <p th:text="${session.testSessionScope}"></p>
    <p th:text="${application.testApplication}"></p>
</body>

点击 index.html 中的对应的目标超链接,结果为:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值