当前位置: 首页 > news >正文

做网站开发平台北京广告公司有哪些

做网站开发平台,北京广告公司有哪些,黄浦区网站建设公司,wordpress网站服务器Spring MVC 前言域对象共享数据使用 ModelAndView 向 request 域对象中共享数据使用 Map 、Model 或 ModelMap 向 request 域对象中共享数据使用 SesionAttributes 注解向 session 域对象中共享数据使用 Servlet API 向 application 域对象中共享数据 附 前言 在上一章中… Spring MVC 前言域对象共享数据使用 ModelAndView 向 request 域对象中共享数据使用 Map 、Model 或 ModelMap 向 request 域对象中共享数据使用 SesionAttributes 注解向 session 域对象中共享数据使用 Servlet API 向 application 域对象中共享数据 附 前言 在上一章中谈到处理器 Controller 获取请求参数的实现。那么在处理器 Controller 处理请求后下一步是将模型数据通过域对象共享的方式结果会封装成模型视图 ModelAndView 对象返回给前端控制器 DispatcherServlet 。 模型数据Model 层中请求处理后的结果会返回给处理器 Controller 域对象共享数据 域对象共享数据指在 Spring MVC 应用中多个 Controller 或 Model 对象可以共享相同的数据。通过共享数据可以避免重复的数据获取和数据传递提高程序的性能和可维护性。 在 Spring MVC 中域对象共享数据可以通过多种方式实现 使用 ModelAndView 向 request 域对象中共享数据使用 Map 、Model 或 ModelMap 向 request 域对象中共享数据使用 SesionAttributes 注解向 session 域对象中共享数据使用 Servlet API 向 application 域对象中共享数据 使用 ModelAndView 向 request 域对象中共享数据 ModelAndView 对象有 model 和 view 两个属性。model 属性用于向请求域共享数据而 view 属性用于设置视图。 简单示例 1.首先进行注入字符串 package cn.edu.springmvcdemo.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;Controller public class DomainObjectSDDemo {RequestMapping(/mavTest)public ModelAndView mavTest(){ModelAndView mav new ModelAndView();//1.字符串注入mav.addObject(name,admin); //添加数据模型mav.setViewName(DomObjSharedData); //设置视图return mav;} }创建 DomObjSharedData.jsp %--Created by IntelliJ IDEA.User: dellDate: 2023/7/22Time: 15:04To change this template use File | Settings | File Templates. --% % page contentTypetext/html;charsetUTF-8 languagejava % html bodyname ${name} %-- 或者 ${requestScope.name} --% /body /html结果如图 2.接着进行注入对象 先创建一个实体类 DomainObject 定义编号、名字和年龄属性 下面例子中使用到的对象都是 DomainObject package cn.edu.springmvcdemo.model;public class DomainObject {private int id;private String name;private int age;public DomainObject() {super();}public DomainObject(int id, String name, int age) {this.id id;this.name name;this.age age;}public int getId() {return id;}public void setId(int id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}Overridepublic String toString() {return DomainObject{ id id , name name \ , age age };} }接着对象注入 package cn.edu.springmvcdemo.controller;import cn.edu.springmvcdemo.model.DomainObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;Controller public class DomainObjectSDDemo {RequestMapping(/mavTest)public ModelAndView mavTest(){ModelAndView mav new ModelAndView();//1.字符串注入mav.addObject(name,admin); //添加数据模型//2.对象注入DomainObject domainObject new DomainObject();domainObject.setId(722);domainObject.setName(admin);domainObject.setAge(18);mav.addObject(admin,domainObject);mav.setViewName(DomObjSharedData); //设置视图return mav;} }然后DomObjSharedData.jsp 添加获取对象的内容 % page contentTypetext/html;charsetUTF-8 languagejava % html bodyname ${name} %-- 或者 ${requestScope.name} --%bruser ${admin} /body /html结果如图 3.最后进行注入 list 集合与 map 集合 先在 pom.xml 中添加依赖 !-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -- dependencygroupIdorg.apache.taglibs/groupIdartifactIdtaglibs-standard-impl/artifactIdversion1.2.5/version /dependency!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-spec -- dependencygroupIdorg.apache.taglibs/groupIdartifactIdtaglibs-standard-spec/artifactIdversion1.2.5/version /dependency接着list 集合和 map 集合注入 package cn.edu.springmvcdemo.controller;import cn.edu.springmvcdemo.model.DomainObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;Controller public class DomainObjectSDDemo {RequestMapping(/mavTest)public ModelAndView mavTest(){ModelAndView mav new ModelAndView();//1.字符串注入mav.addObject(name,admin); //添加数据模型//2.对象注入DomainObject domainObject1 new DomainObject();domainObject1.setId(722);domainObject1.setName(admin);domainObject1.setAge(18);mav.addObject(admin,domainObject1);//3. list 集合注入DomainObject domainObject2 new DomainObject();domainObject2.setId(723);domainObject2.setName(administrator);domainObject2.setAge(20);mav.addObject(administrator,domainObject2);ListDomainObject domainObjects new ArrayList();domainObjects.add(domainObject1);domainObjects.add(domainObject2);mav.addObject(domainObjects,domainObjects); //存入 List 类型数据//3. map 集合注入MapInteger,DomainObject map new HashMapInteger,DomainObject();map.put(1,domainObject1);map.put(2,domainObject2);mav.addObject(map,map); //存入 Map 类型数据mav.setViewName(DomObjSharedData); //设置视图return mav;} }然后DomObjSharedData.jsp 添加获取 list 集合和 map 集合的内容 % page contentTypetext/html;charsetUTF-8 languagejava % % taglib prefixc urihttp://java.sun.com/jsp/jstl/core % html bodyname ${name} %-- 或者 ${requestScope.name} --%bruser ${admin}brc:forEach vardomainObject items${domainObjects}domainObjects ${domainObject}brdomainObject_names ${domainObject.name}br/c:forEachbrc:forEach varmap items${map}map ${map}br/c:forEach /body /html注这次需要重启 JRebel 才能正常显示 结果如图 使用 Map 、Model 或 ModelMap 向 request 域对象中共享数据 使用 Map 、Model 或 ModelMap 向 request 域对象中共享数据是项目开发中相对比较常用的方式。与使用 ModelAndView 相比更简单便捷些。 简单示例 在上面案例的基础上只需在 DomainObjectSDDemo 类中进行修改 1.使用 Map 向 request 域对象中共享数据 package cn.edu.springmvcdemo.controller;import cn.edu.springmvcdemo.model.DomainObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Map;Controller public class DomainObjectSDDemo {RequestMapping(/mvTest1)public String mvTest1(MapString,Object map){//注入字符串map.put(name,admin); //对比使用 ModelAndView mav.addObject(name,admin);//注入对象DomainObject domainObject1 new DomainObject();domainObject1.setId(722);domainObject1.setName(admin);domainObject1.setAge(18);map.put(admin,domainObject1); //对比使用 ModelAndView mav.addObject(admin,domainObject1);//主要区别在于 map.put() 和 mav.addObject()不一一演示return DomObjSharedData; /**对比使用 ModelAndView ModelAndView mav new ModelAndView();mav.setViewName(DomObjSharedData); //设置视图return mav;**/} }结果如图 2.使用 Model 向 request 域对象中共享数据 RequestMapping(/mvTest2) //对比使用 map 的方法参数MapString,Object map public String mvTest2(Model model){//注入字符串//对比使用 Mapmap.put(name,admin); //对比使用 ModelAndView mav.addObject(name,admin);model.addAttribute(name,admin);//注入对象DomainObject domainObject1 new DomainObject();domainObject1.setId(722);domainObject1.setName(admin);domainObject1.setAge(18);//对比使用 Mapmap.put(admin,domainObject1); //对比使用 ModelAndView mav.addObject(admin,domainObject1);model.addAttribute(admin,domainObject1);//主要区别在于 model.addAttribute()、map.put() 和 mav.addObject()不一一演示return DomObjSharedData; /**对比使用 ModelAndView ModelAndView mav new ModelAndView();mav.setViewName(DomObjSharedData); //设置视图return mav;**/ }结果如图 3.使用 ModelMap 向 request 域对象中共享数据 RequestMapping(/mvTest3) //而 ModelMap 将 model 和 map 综合起来了。即既可以使用 addAttribute() 也可以使用 put() //方法参数为 ModelMap public String mvTest3(ModelMap modelMap){modelMap.addAttribute(name,admin); // 或者 modelMap.put(name,admin);DomainObject domainObject1 new DomainObject();domainObject1.setId(722);domainObject1.setName(admin);domainObject1.setAge(18);modelMap.put(admin,domainObject1); // 或者 modelMap.addAttribute(admin,domainObject1);return DomObjSharedData; }结果如图 总体来讲向 request 域对象中共享数据的各种方式区别不大自行根据个人喜好习惯选择使用即可。 使用 SesionAttributes 注解向 session 域对象中共享数据 使用 SesionAttributes 注解可以将数据共享到 session 域对象中同时也共享到 request 域对象中。 SesionAttributes 注解属性 value 通过键来指定共享的值types 通过类型来指定共享的值 //只能用于类级别 //书写格式 SessionAttributes(value xxx,types xxx.class) //或者 SessionAttributes(value {xxx,xxx...},types {xxx.class,xxx.class...})简单示例 使用 SesionAttributes 注解向 session 域对象中共享数据 package cn.edu.springmvcdemo.controller;import cn.edu.springmvcdemo.model.DomainObject; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.SessionAttributes;SessionAttributes(value {name,admin}) //或者 SessionAttributes(types {String.class,DomainObject.class}) Controller public class DomainObjectSDDemo_SA {RequestMapping(/saTest)public String saTest(ModelMap modelMap){//字符串注入modelMap.put(name,admin);//对象注入//实体类 DomainObject ModelAndView 的示例中已创建DomainObject domainObject1 new DomainObject();domainObject1.setId(722);domainObject1.setName(admin);domainObject1.setAge(18);modelMap.put(admin, domainObject1);return DomObjSharedData_SA;} }创建 DomObjSharedData_SA.jsp % page contentTypetext/html;charsetUTF-8 languagejava % html body获取 request 域中的 name : ${requestScope.name} br获取 session 域中的 name : ${sessionScope.name} bradmin ${admin} /body /html结果如图 使用 Servlet API 向 application 域对象中共享数据 使用 Servlet API 可以将数据共享到 application 域对象中。虽然不能同时共享到其他域对象中但也可以使用 Servlet API 将数据共享到 session 域对象与 request 域对象中。 简单示例 使用 Servlet API 向 application 域对象、session 域对象与 request 域对象中共享数据 package cn.edu.springmvcdemo.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession;Controller public class DomainObjectSDDemo_SAPI {RequestMapping(/sapiTest)public String sapiTest(HttpSession session, HttpServletRequest request){//1.向 application 域对象共享数据ServletContext application session.getServletContext();application.setAttribute(name, admin1);//2.向 session 域对象共享数据session.setAttribute(name,admin2);//3.向 request 域对象共享数据request.setAttribute(name,admin3);return DomObjSharedData_SAPI;} }创建 DomObjSharedData_SAPI.jsp % page contentTypetext/html;charsetUTF-8 languagejava % html body获取 application 域中的 name : ${applicationScope.name} br获取 session 域中的 name : ${sessionScope.name} br获取 request 域中的 name : ${requestScope.name} /body /html结果如图 附 ModelAttribute 注解介绍 1.使用 ModelAttribute 注解在方法上在执行目标方法前先从上到下逐一执行有 ModelAttribute 注解的方法 2.使用 ModelAttribute 注解在方法的参数上用于从 Model 、Form 表单或者 URL 请求参数中获取属性值 通常 ModelAttribute 注解应用在修改对象的某些属性值而其他属性值不允许修改或保持不变的场景中 简单示例 创建修改信息的页面 DomObjSharedData_UPDATE.jsp %-- 只修改年龄id 隐藏名字不允许修改 --% % page contentTypetext/html;charsetUTF-8 languagejava % html bodyh3信息修改/h3form action${pageContext.request.contextPath}/updateTest methodpostinput typehidden nameid value722 /年龄input typetext nameage /input typesubmit value修改//form /body /html创建成功修改跳转的页面 accessing.jsp % page contentTypetext/html;charsetUTF-8 languagejava % html bodyh2提交成功/h2 /body /html在没有使用 ModelAttribute 注解前不作修改的属性值可能为空或者使用传统的方法修改实现 在 Spring MVC 中提供的 ModelAttribute 注解也可以实现修改 package cn.edu.springmvcdemo.controller;import cn.edu.springmvcdemo.model.DomainObject; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam;Controller public class DomainObjectSDDemo_MA {//先执行有 ModelAttribute 注解的方法ModelAttribute//默认 id 参数赋值为 null RequestParam 注解知识点private void getUser(RequestParam(value id,required false) Integer id, ModelMap modelMap){System.out.println(id id);DomainObject domainObject new DomainObject();//通过传递的 id 值匹配数据库中对应的记录返回一个对应的对象//模拟获取 id 对应的对象属性值domainObject.setName(admin);domainObject.setAge(18);//注入 DomainObject对象attributeName 规定写为 domainObject modelMap.addAttribute(domainObject,domainObject); //注attributeName 与对应的对象名字保持一致首字母改为小写}//信息修改RequestMapping(/update)public String update(){return DomObjSharedData_UPDATE;}//修改成功RequestMapping(/updateTest)public String updateTest(DomainObject domainObject){System.out.println(修改后的信息 domainObject);//信息修改后为DomainObject{id722, namenull, age18}这里 name 为空直接全部覆盖会把数据库的 name 信息也修改为 null//或者/*** 传统做法* 第一步通过传递的id值匹配数据库中对应的记录返回一个对应的对象* 第二步将传递的对象要修改的属性覆盖到从数据库中查询出的对象的属性里* 第三步调用 service 层的修改方法实现修改* 这里思想就是单一对修改的字段进行覆盖。* **///使用 ModelAttribute 注解后可以直接全部覆盖省略了传统做法的第一二步直接调用 service 层的修改方法即可return accessing;} } 注在注入对象中关于 attributeName 的命名也可以使用第二种方式。如图 测试结果 1.填写要修改的年龄点击提交 2.修改成功同时没有修改的信息保持不变而不是为空值
http://www.zqtcl.cn/news/272381/

相关文章:

  • 郑州企业建站系统模板兰州需要做网站的公司有哪些
  • 怎样做网站卖东西 自己有货句容网络公司
  • 网站建设协议书 保密条款免费发布推广的网站
  • 网站首页外链上海网站建设联系方式
  • 陕西网站建设优化技术2023年1月热点新闻事件
  • 广东省建设银行招聘网站免费搭建个人网站
  • 知名商城网站建设公司wordpress主题 汉化
  • 网站上线做什么pc网站如何做移动适配
  • wap网站搭建北京北京网站建设
  • 放心的网站设计制作免费做logo设计的网站
  • 温州专业手机网站制作多少钱移动商城 网站建设方法方式
  • 周口网站开发wordpress
  • 如何查网站的备案号玉环在哪里做网站
  • 网站开发什么叫前端后端seo研究中心晴天
  • 邢台建筑类的建设网站代刷网站只做软件下载
  • 关于旅游的网站建设目的食品网站建设的目的
  • 开发php网站开发太湖网站建设推荐秒搜科技
  • 90设计网站怎么绑定手机号淘宝搜索排名
  • 无锡自助做网站哪些编程语言适合网站开发
  • 蒲城网站建设wzjseo北京专业推广公司
  • 阳春做网站外贸建站推广公司
  • 哪个网站的课件做的好源码之家关闭了
  • 各大网站热搜榜排名嵊州网站
  • 在哪找做网站的镇江网页设计工作室
  • 做网站的是干嘛的百度推广的几种方式
  • 临沧网站建设用eclipse做jsp网站
  • 做物流运输网站电话做网站看
  • 山东公司网站推广优化什么网站做宣传好
  • 企业网站模板设计外网vp(永久免费)加速器下载
  • 消费者联盟网站怎么做中山网站建设案例