小说网站wordpress情侣博客网站模板下载
news/
2025/10/8 11:04:41/
文章来源:
小说网站wordpress,情侣博客网站模板下载,专业的网站制作正规公司,网站首页设计思路文章目录 域对象共享数据一、三种域对象二、通过ServletAPI向Request域对象共享数据三、使用ModelAndView向Request域对象共享数据四、使用Model向Request域对象共享数据五、使用Map向Request域对象共享数据六、使用ModelMap向Request域对象共享数据七、向Session域对象共享数据… 文章目录 域对象共享数据一、三种域对象二、通过ServletAPI向Request域对象共享数据三、使用ModelAndView向Request域对象共享数据四、使用Model向Request域对象共享数据五、使用Map向Request域对象共享数据六、使用ModelMap向Request域对象共享数据七、向Session域对象共享数据八、向Application域对象共享数据 域对象共享数据
一、三种域对象
ServletContext域对象是一个全局性的域对象它的生命周期与Web应用程序的生命周期一致即当Web应用程序被加载时与之对应的ServletContext对象会被创建只要Web应用程序还存在这个域对象就会一直存在当Web应用程序被卸载或者服务器关闭时ServletContext对象也会随之消亡。ServletContext域对象的主要作用是提供一种机制允许开发者在其内部设置和获取数据这些数据可以在整个Web应用程序的范围内共享。它提供了一些API如setAttribute(String key, Object value)、getAttribute(String key)以及removeAttribute(String key)等方法来实现这些功能。
Request域对象也是一个域对象它主要用于处理一次HTTP请求。它的生命周期与本次请求相关一旦请求结束Request对象也就不再存在了。Request域对象的主要用途是在处理请求时保存和传递数据。它同样提供了一些API如setAttribute(String name, Object value)用于在请求间共享数据以及在请求结束后清除这些数据。
Session域对象则专门用于处理一次HTTP会话。它会随着会话的建立而创建并在会话结束时销毁。Session域对象的主要目的是保存用户的状态信息以便在整个会话期间为不同的请求提供相同的用户状态。它也提供了一些API如setAttribute(String name, Object value)用于在会话间共享数据以及removeAttribute(String name)用于清除会话结束时的数据
二、通过ServletAPI向Request域对象共享数据
2.1.request共享数据 控制类 RequestMapping(servletAPI)public String servletApi(HttpServletRequest request){request.setAttribute(testscope,hello,world!);return scope;}界面 对于request可以直接使用变量名来访问
!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleTitle/title
/head
bodyp th:text${testscope}/p
/body
/html三、使用ModelAndView向Request域对象共享数据
返回类型必须是ModelAndView RequestMapping(ModelAndView)public ModelAndView testModelAndView(){ModelAndView modelAndView new ModelAndView();//向request域共享数据modelAndView.addObject(testscope,hello,ModelAndView!);//设置视图名称 转发到scope.html页面modelAndView.setViewName(scope);return modelAndView;}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleTitle/title
/head
bodyp th:text${testscope}/p
/body
/html四、使用Model向Request域对象共享数据 RequestMapping(model)public String model(Model model){model.addAttribute(testscope,hello,Model!);return scope;}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleTitle/title
/head
bodyp th:text${testscope}/p
/body
/html五、使用Map向Request域对象共享数据
RequestMapping(map)public String map(MapString, Object map){map.put(testscope,hello,Map!);return scope;}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleTitle/title
/head
bodyp th:text${testscope}/p
/body
/html六、使用ModelMap向Request域对象共享数据 RequestMapping(modelMap)public String modelMap(ModelMap map){map.addAttribute(testscope,hello,ModelMap!);return scope;}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleTitle/title
/head
bodyp th:text${testscope}/p
/body
/html七、向Session域对象共享数据 RequestMapping(session)public String testsession(HttpSession session){session.setAttribute(testscope,hello,Session!);return scope;}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleTitle/title
/head
bodyp th:text${testscope}/p
p th:text${session.testscope}/p
/body
/html八、向Application域对象共享数据
RequestMapping(application)public String testapplication(HttpSession session){ServletContext application session.getServletContext();application.setAttribute(testscope,hello,application!);return scope;}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleTitle/title
/head
bodyp th:text${testscope}/p
p th:text${application.testscope}/p
/body
/html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/931408.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!