SpringBoot 服务端表单数据校验

  • POM 依赖
<parent>
    <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.10.RELEASE</version>
</parent>

<properties>
	<java.version>1.8</java.version>
	<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
	<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- springBoot 的启动器 -->
	<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
<!-- thymeleaf 的启动器 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
</dependencies>

  • SpringBoot 中使用了 Hibernate-validate
  1. 在实体类中添加校验规则
    @NotBlank: 判断字符串是否为 null 或者是空串(去掉首尾空格)。
    @NotEmpty: 判断字符串是否 null 或者是空串。
    @Length: 判断字符的长度(最大或者最小)
    @Min: 判断数值最小值
    @Max: 判断数值最大值
    @Email: 判断邮箱是否合法
public class Users {
	@NotBlank(message="用户名不能为空") //非空校验
	@Length(min=2,max=6,message="最小长度为2位,最大长度为6位")
	private String name;
	@NotEmpty
	private String password;
	@Min(value=15)
	private Integer age;
	@Email
	private String email;
}
  1. 在Controller 中开启校验
/**
* 完成用户添加
*@Valid 开启对 Users 对象的数据校验
*BindingResult:封装了校验的结果
*/
@RequestMapping("/save")
public String saveUser(@Valid Users users,BindingResult result){
	if(result.hasErrors()){
		return "add";
	}
	System.out.println(users);
	return "ok";
}
  1. 在页面中获取提示信息
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>添加用户</title>
	</head>
<body>
	<form th:action="@{/save}" method="post">
		用户姓名:<input type="text" name="name"/><font color="red" th:errors="${users.name}"></font><br/>
		用户密码:<input type="password" name="password"/><font color="red" th:errors="${users.password}"></font><br/>
		用户年龄:<input type="text" name="age" /><font color="red"th:errors="${users.age}"></font><br/>
		<input type="submit" value="OK"/>
	</form>
</body>
</html>

  • 遇到异常,解决数据校验时的异常问题
    在这里插入图片描述
  1. 解决异常的方法,在跳转页面的方法中注入一个对象,来解决问题。要求参数对象的变量名必须是对象的类名的全称首字母小写。
/**
* 解决异常的方式。可以在跳转页面的方法中注入一个 Uesrs 对象。
* 注意:由于 springmvc 会将该对象放入到 Model 中传递。key 的名称会使用
该对象的驼峰式的命名规则来作为 key。
* 参数的变量名需要与对象的名称相同。将首字母小写。
*
* @param users
* @return
*/
@RequestMapping("/addUser")
public String showPage( Users users){
		return "add";
}
/**
* 完成用户添加
*@Valid 开启对 Users 对象的数据校验
*BindingResult:封装了校验的结果
*/
@RequestMapping("/save")
	public String saveUser( @Valid Users users,BindingResult result){
	if(result.hasErrors()){
		return "add";
	}
	System.out.println(users);
	return "ok";
}

//===============================================
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>添加用户</title>
	</head>
<body>
	<form th:action="@{/save}" method="post">
	用户姓名:<input type="text" name="name"/><font color="red" th:errors="${users.name}"></font><br/>
	用户密码:<input type="password" name="password" /><font color="red" th:errors="${users.password}"></font><br/>
	用户年龄:<input type="text" name="age" /><font color="red" th:errors="${users.age}"></font><br/>
	<input type="submit" value="OK"/>
	</form>
</body>
</html>

  1. 如果参数的名称需要做改变
/**
*
* 如果想为传递的对象更改名称,可以使用@ModelAttribute("aa")这表示当
前传递的对象的 key 为 aa。
* 那么我们在页面中获取该对象的 key 也需要修改为 aa
* @param users
* @return
*/
@RequestMapping("/addUser")
public String showPage(@ModelAttribute("aa") Users users){
		return "add";
}
/**
* 完成用户添加
*@Valid 开启对 Users 对象的数据校验
*BindingResult:封装了校验的结果
*/
@RequestMapping("/save")
public String saveUser(@ModelAttribute("aa") @Valid Users users,BindingResult result){
		if(result.hasErrors()){
		return "add";
		}
		System.out.println(users);
		return "ok";
}


========================================

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>添加用户</title>
	</head>
<body>
	<form th:action="@{/save}" method="post">
	用户姓名:<input type="text" name="name"/><font color="red" th:errors="${aa.name}"></font><br/>
	用户密码:<input type="password" name="password"/><font color="red" th:errors="${aa.password}"></font><br/>
	用户年龄:<input type="text" name="age" /><font color="red" th:errors="${aa.age}"></font><br/>
	<input type="submit" value="OK"/>
	</form>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Spring Boot NTRIP 服务端示例,用于将数据 1234 发送回客户端: ```java @RestController public class NTRIPServerController { @GetMapping("/ntrip") public void ntripServer(HttpServletResponse response) throws IOException { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"data.bin\""); try (OutputStream outputStream = response.getOutputStream()) { outputStream.write(new byte[] { 0x12, 0x34 }); } } } ``` 此控制器处理 `/ntrip` GET 请求,设置响应类型为二进制流,并将数据写入响应输出流中。 以下是一个简单的 NTRIP 客户端示例,用于从服务端获取数据: ```java public class NTRIPClientDemo { private static final String NTRIP_SERVER_URL = "http://localhost:8080/ntrip"; public static void main(String[] args) throws IOException { URL url = new URL(NTRIP_SERVER_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); InputStream inputStream = connection.getInputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } byte[] data = outputStream.toByteArray(); System.out.println(Arrays.toString(data)); inputStream.close(); } } ``` 该客户端发送 GET 请求到服务端,并将响应数据读取到字节数组中。 请注意,此示例仅适用于演示和学习目的,实际应用中需要进行更多的错误处理和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值