域对象共享数据

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

<a th:href="@{/testRequestByServletAPI}">通过ServletAPI向Request域对象共享数据</a>
<p th:text="${testRequestByServletAPI}"></p>
//使用ServletAPI向Request域对象共享数据
    @RequestMapping("/testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request){
        request.setAttribute("testRequestByServletAPI","hello,servletAPI");
        return "success";
    }

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

 //使用ModelAndViewI向Request域对象共享数据
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        /**
         *  ModelAndView有Model和view的功能
         *  Model 主要用于向请求域共享数据
         *  View 主要用于设置视图,实现页面跳转
         */

        ModelAndView mav = new ModelAndView();
        //向请求域共享数据
        mav.addObject("testScope","hello,ModelAndView");
        //设置视图,实现页面跳转
        mav.setViewName("success");
        return mav;
    }

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

 //使用Model向Request域对象共享数据
    @RequestMapping("/testModel")
    public String testModel(Model model){
        //向请求域共享数据
        model.addAttribute("testRequestScope","hello,Model");
        return "success";
    }

4、使用Map向request域对象共享数据

  //使用Map向Request域对象共享数据
    @RequestMapping("/testMap")
    public String testModel(Map<String,Object>map){
        //向请求域共享数据
        map.put("testMap","hello,Map");
        return "success";
    }

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

 //使用ModelMap向Request域对象共享数据
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        //向请求域共享数据
       modelMap.put("testModelMap","hello,ModelMap");
        return "success";
    }

6、MOdel、ModelMap、Map的关系

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

public interface Model {}
public class ModelMap extends LinkedHashMap<StringObject{}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

7、向Session共享数据(一)

    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        //向请求域共享数据
        session.setAttribute("testSessionScope","hello,session");
        return "success";
    }

8、向Session共享数据(二)

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


原文始发于微信公众号(itmkyuan):域对象共享数据

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/39482.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!