域对象共享数据

1.使用servletAPI向rquest域对象共享数据

//使用servletAPI向request域对象共享数据
@RequestMapping("/servletAPI")
public String servletAPI(HttpServletRequest request){
    request.setAttribute("testRequestScope","hello,servletAPI");
    return "success";

}
<a th:href="@{/servletAPI}">使用servletAPI向request域对象共享数据</a><br/>

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

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

    return mav;
}
<a th:href="@{/modelAndView}">使用ModelAndView向request域对象共享数据</a><br/>

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

//使用Model向request域对象共享数据
@RequestMapping("/model")
public String model(Model model){
    model.addAttribute("testRequestScope","hello,model");
    return "success";
}
<a th:href="@{/model}">使用model向request域对象共享数据</a><br/>

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

//使用map向request域对象共享数据
@RequestMapping("/map")
public String map(Map<String,Object> map){
    map.put("testRequestScope","hello,map");
    return "success";
}
<a th:href="@{/map}">使用map向request域对象共享数据</a><br/>

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

//使用ModelMap向request域对象共享数据
@RequestMapping("/modelMap")
public String modelMap(ModelMap modelMap){
    modelMap.addAttribute("testRequestScope","hello,modelMap");
    return "success";
}
<a th:href="@{/modelMap}">使用modelMap向request域对象共享数据</a><br/>

以上五种方法,键都是一样的,值可以是任意的

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

用Debug调试断点发现:

控制器方法执行之后,都需要返回统一的ModelandView对象

6.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 {}

 7.向session域共享数据

//向session域共享数据
@RequestMapping("/session")
public String session(HttpSession session){
    session.setAttribute("testSessionScope","hello,session");
    return "success";
}
<a th:href="@{/session}">向session域共享数据</a><br/>
<p th:text="${session.testSessionScope}"></p>

8.向application域共享数据

//向application域共享数据
@RequestMapping("/application")
public String application(HttpSession session){
    ServletContext application = session.getServletContext();
    application.setAttribute("testApplicationScope","hello,application");
    return "success";
}
<a th:href="@{/application}">向application域共享数据</a><br/>
<p th:text="${application.testApplicationScope}"></p>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鲸叫我照顾大海

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

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

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

打赏作者

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

抵扣说明:

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

余额充值