做网站开发平台,北京广告公司有哪些,黄浦区网站建设公司,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.修改成功同时没有修改的信息保持不变而不是为空值