一、数据格式化
1、配置文件 springmvc.xml
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>
<mvc:annotation-driven></mvc:annotation-driven>
2、实体类
在需要格式化的属性上添加相应注解
@NumberFormat(pattern = “###,##”):对
@DateTimeFormat(pattern = “yyyy-MM-dd”)
@Data
public class Student {
@NumberFormat(pattern = "#,##")
private String id;
private String name;
//设置接受前端日期的格式: yyyy-MM-dd
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date date;
}
3、控制器类
前端将数据传入Student,由于设置了date的日期格式,如果日期的格式不符合要求,则将错误信息放入 BindingResult ;Map负责将信息传回前端
/**
* Servlet implementation class Test
*/
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
/**
* 数据格式化
* @return
*/
@RequestMapping(value={"/requestDateTimeFormat"})
public String requestDateTimeFormat(Student student,BindingResult result,Map<String,Object> map) {
System.out.println(student);
//如果有错误信息则打印错误信息,类似try..catch
if(result.getErrorCount()> 0) {
for (FieldError error : result.getFieldErrors()) {
System.out.println(error.getDefaultMessage());
}
map.put("errors", result.getFieldErrors());
}
return "welcome";
}
}
4、跳转页面 welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach items="${requestScope.errors}" var="error">
${error.getDefaultMessage() }</br>
</c:forEach>
</body>
</html>
二、数据校验
1、添加依赖
<!-- hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Final</version>
</dependency>
<!-- jakarta.validation-api -->
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
<!-- jboss-logging -->
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.2.Final</version>
</dependency>
<!--classmate -->
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.3.4</version>
</dependency>
2、实体类
在需校验的属性上添加注解,如 @Past 等
public class Student {
@NumberFormat(pattern = "#,##")
private String id;
private String name;
@Past
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date date;
}
3、控制器类
对于校验的属性,在对应的参数前用 @Valid 修饰
/**
* Servlet implementation class Test
*/
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
/**
* 数据校验
* @return
*/
@RequestMapping(value={"/requestValid"})
public String requestValid(@Valid Student student,BindingResult result,Map<String,Object> map) {
System.out.println(student);
if(result.getErrorCount()> 0) {
for (FieldError error : result.getFieldErrors()) {
System.out.println(error.getDefaultMessage());
}
map.put("errors", result.getFieldErrors());
}
return "welcome";
}
}
4、JSR 303 注解简介
5、hibernate validator 注解简介
hibernate validator 为JSR 303 的拓展
三、处理 ajax 请求数据
1、添加依赖
添加处理 json 格式数据的依赖
<!-- jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.3</version>
</dependency>
<!-- jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.3</version>
</dependency>
<!-- jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
2、index,jsp
前端通过ajax发送请求,并输出返回值
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#requestJson").click(function(){
$.post(
"index/requestJson",
{"id":12,"name":"test","date":"2022-09-6"},
function(result){
for(var i= 0;i<result.length;i++){
alert(result[i].id+","+result[i].name+","+result[i].date);
}
}
);
});
});
</script>
</head>
<body>
<input type="button" value="json" id="requestJson"/>
</body>
</html>
3、控制器类
-
对 Student 中的 date 属性进行校验,若校验不通过,则将错误信息放入 BindingResult 并打印输出,然后继续返回数据到前台
-
注意添加 @ResponseBody :由于此处时 ajax 请求,需要返回的是数据而不是一个页面,所以需要添加注解 @ResponseBody;表示此时返回的是一笔数据
/**
* Servlet implementation class Test
*/
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
/**
* 处理json数据
* @return
*/
@ResponseBody
@RequestMapping(value={"/requestJson"})
public ArrayList<Student> requestJson(@Valid Student student,BindingResult result) {
System.out.println(student);
//存在错误信息则打印
if(result.getErrorCount()> 0) {
for (FieldError error : result.getFieldErrors()) {
System.out.println(error.getDefaultMessage());
}
}
Student student2 =new Student("2","zs");
Student student3 =new Student("3","ww");
Student student4 =new Student("4","ls");
ArrayList<Student> list=new ArrayList();
list.add(student2);
list.add(student3);
list.add(student4);
return list;
}
}