springmvc域对象共享数据:request、sssion、application

一、通过原生servletAPI向request域中共享数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
servletapi:<p th:text="${name}"></p><br/>
</body>
</html>
@RequestMapping("testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request) {
        request.setAttribute("name", "job");
        return "success";
    }

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

不管用的是什么方法,最后都会封装成ModelAndView对象。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
servletapi:<p th:text="${name}"></p><br/>
</body>
</html>
@RequestMapping("testRequestByModelAndView")
    public ModelAndView testRequestByModelAndView() {
        ModelAndView mav = new ModelAndView();
        //向域对象中共享数据
        mav.addObject("name", "modelAmdView");
        //设置视图名称
        mav.setViewName("success");
        return mav;
    }

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

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
servletapi:<p th:text="${name}"></p><br/>
</body>
</html>
@RequestMapping("testRequestByModel")
    public String testRequestByModel(Model model) {
        model.addAttribute("name", "model");
        return "success";
    }

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

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
servletapi:<p th:text="${name}"></p><br/>
</body>
</html>
@RequestMapping("testRequestByMap")
    public String testRequestByMap(Map<String,Object> map) {
        map.put("name","map");
        return "success";
    }

五、使用modelMap向request域对象共享数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
servletapi:<p th:text="${name}"></p><br/>
</body>
</html>
@RequestMapping("testRequestByModelMap")
    public String testRequestByModelMap(ModelMap modelMap) {
        modelMap.addAttribute("name", "modelMap");
        return "success";
    }

六、Model、ModelMap与Map之间的关系

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

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

七、向session域中共享数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
request:<p th:text="${name}"></p><br/>
session:<p th:text="${session.name}"></p><br/>
</body>
</html>
@RequestMapping("tesSessionByServletApi")
    public String tesSessionByServletApi(HttpSession session){
        session.setAttribute("name","session");
        return "success";
    }

八、向application域共享数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
request:<p th:text="${name}"></p><br/>
session:<p th:text="${session.name}"></p><br/>
application:<p th:text="${application.name}"></p><br/>
</body>
</html>
@RequestMapping("testApplicationByServletApi")
    public String testApplicationByServletApi(HttpSession session){
        ServletContext application = session.getServletContext();
        application.setAttribute("name","application");
        return "success";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值