zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Spring MVC数据转换

2023-09-14 08:57:15 时间
public void setAsText(String text) throws IllegalArgumentException { System.out.println("setAsText"); User user = new User(); if(text != null){ String[] items = text.split(":"); user.setUsername(items[0]); user.setPassword(items[1]); setValue(user); }
* 第一个参数user是一个模型数据,接收页面的username用password * 第二个参数converterUser通过@RequestParam注解,把页面的other参数交由UserEditor转成一个User对象 @RequestMapping("create") public ModelAndView createUser(User user,@RequestParam("other")User converterUser){ System.out.println(user.getUsername()+"--"+user.getPassword()); System.out.println(converterUser.getUsername()+"--"+converterUser.getPassword()); ModelAndView view = new ModelAndView(); view.setViewName("/success"); return view; }
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.support.WebBindingInitializer; import org.springframework.web.context.request.WebRequest; import cn.framelife.mvc.entity.User; public class MyBindingInitializer implements WebBindingInitializer { public void initBinder(WebDataBinder binder, WebRequest request) { //注册自定义的属性编辑器。这里可以注册多个属性编辑器 binder.registerCustomEditor(User.class, new UserEditor()); }

配置WebBindingInitializer实现类:


* 第一个参数user是一个模型数据,接收页面的username用password * 第二个参数converterUser通过@RequestParam注解,把页面的other参数交由UserEditor转成一个User对象 @RequestMapping("create") public ModelAndView createUser(User user,@RequestParam("other")User converterUser){ System.out.println(user.getUsername()+"--"+user.getPassword()); System.out.println(converterUser.getUsername()+"--"+converterUser.getPassword()); ModelAndView view = new ModelAndView(); view.setViewName("/success"); return view; }
!-- 装配自定义的转换器 -- mvc:annotation-driven conversion-service="conversionService"/
* 第一个参数user是一个模型数据,接收页面的username用password * 第二个参数converterUser通过@RequestParam注解,把页面的other参数交由转换器StringTouserConverter转成一个User对象 @RequestMapping("create") public ModelAndView createUser(User user,@RequestParam("other")User converterUser){ System.out.println(user.getUsername()+"--"+user.getPassword()); System.out.println(converterUser.getUsername()+"--"+converterUser.getPassword()); ModelAndView view = new ModelAndView(); view.setViewName("/success"); return view; }

如果Controller范围的属性编辑器、全局范围的属性编辑器、转换器同时存在,那么Spring MVC将按以下的优先顺序查找对应类型的编辑器来处理:
查询Controller范围的属性编辑器
查询转换器
查询全局范围的属性编辑器

4、数据格式化

A、启动注解驱动格式化功能
之前我们配置自定义转换器的时候,使用的是BeanConversionServiceFactoryBean。

org.springframework.context.support.ConversionServiceFactoryBean

改成


FormattingConversionServiceFactoryBean即可以注册自定义的转换器,还可以注册自定义的注解驱动的格式转换器,使项目支持注解驱动格式化功能。

 bean id="conversionService" 

 property name="converters" 

 list 

 !-- 这是之前配置自定义的转换器 -- 

 bean /bean 

 /list 

 /property 

 /bean 

B、页面

 form action="user/create.abc" method="post" 

 用户名: input type="text" name="username" br/ 

 密 码: input type="text" name="password" br/ 

 生日: input type="text" name="birthday" br/ 

 工资: input type="text" name="salary" br/ 

 其它: input type="text" name="other" br/ 

 input type="submit" 

 /form 

C、实体类中使用格式化注解

public class User implements java.io.Serializable {

 private Integer id;

 private String username;

 private String password;

 // 将如1999-09-09这样的字符串转换成Date对象

 @DateTimeFormat(pattern = "yyyy-MM-dd")

 private Date birthday;

 // 把如5,500.00这个的字符串转换成long类型的数据

 @NumberFormat(pattern = "#,###.##")

 private long salary;

 public long getSalary() {

 return salary;

 public void setSalary(long salary) {

 this.salary = salary;

 public Date getBirthday() {

 return birthday;

 public void setBirthday(Date birthday) {

 this.birthday = birthday;

 public Integer getId() {

 return id;

 public void setId(Integer id) {

 this.id = id;

 public String getUsername() {

 return username;

 public void setUsername(String username) {

 this.username = username;

 public String getPassword() {

 return password;

 public void setPassword(String password) {

 this.password = password;

}

D、Controler中处理

 @RequestMapping("create")

 public ModelAndView createUser(User user){

 System.out.println(user.getBirthday()+"=="+user.getSalary());

 ModelAndView view = new ModelAndView();

 view.setViewName("/success");

 return view;

 }

Spring Boot MVC请求参数通用校验及国际化支持 一、Validation及国际化配置 1、添加依赖 2、校验失败提示消息国际化配置 3、application.properties 4、国际化资源文件 二、代码演示 1、全局异常处理 2、MessageUtils工具类 3、响应VO 2、测试Controller和请求DTO 3、多语言属性文件 4、测试用例 (1)简单对象UserReqDTO测试 (2)包含List集合对象的ChargeRuleReqDTO测试
Spring MVC中文件上传和下载 文件上传需将表格的提交方式设为 POST ,并且将enctype设为 multipart/form-data ,以二进制的方式提交数据。 spring mvc中可通过MultipartResolver监听每个请求,如有上传的文件,则把请求封装为MultipartHttpServletRequest,通过封装的请求可以获取上传的文件信息和上传的文件。 实际使用可直接将MultipartFile作为控制器中请求处理方法的参数,MultipartFile是一个接口,其实现类为CommonsMultipartFile,通过MultipartFile封装的方法也可获取文件相关信息。
java面试题(十八)spring MVC 3.1 什么是MVC? MVC是一种设计模式,在这种模式下软件被分为三层,即Model(模型)、View(视图)、Controller(控制器)。Model代表的是数据,View代表的是用户界面,Controller代表的是数据的处理逻辑,它是Model和View这两层的桥梁。将软件分层的好处是,可以将对象之间的耦合度降低,便于代码的维护。 3.2 DAO层是做什么的? DAO是Data Access Object的缩写,即数据访问对象,在项目中它通常作为独立的一层,专门用于访问数据库。这一层的具体实现技术有很多,常用的有Spring JDBC、Hibernate、JPA、
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载