十九 SpringBoot服务端表单数据校验-实现添加用户功能

本文通过七个步骤详细介绍了如何在SpringBoot项目中进行服务端表单数据校验,包括创建Maven项目,配置pom.xml,设计用户添加页面addUser.html和ok.html,定义Users实体类,编写Controller以及启动类,并最终进行测试,实现了用户添加功能。
摘要由CSDN通过智能技术生成

1 创建maven项目

 

2 修改pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>
	<groupId>com.bjsxt</groupId>
	<artifactId>13-spring-boot-validate</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<java.version>1.8</java.version>
	</properties>
	
	<dependencies>
		<!-- springboot的web启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- springboot的thymeleaf启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	</dependencies>
</project>

3 添加用户页面

3.1 addUser.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
	<form th:action="@{/saveUser}" method="post">
		用户姓名:<input type="text" name="name"/><br/><br/>
		用户密码:<input type="password" name="password"/><br/><br/>
		用户年龄:<input type="text" name="age"/><br/><br/>
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

3.2 ok.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作提示页面</title>
</head>
<body>
	操作成功
</body>
</html>

4 创建用户实体类Users.java

package com.bjsxt.pojo;

public class Users {
	private String name;
	private String password;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Users(String name, String password, Integer age) {
		super();
		this.name = name;
		this.password = password;
		this.age = age;
	}
	public Users() {
		super();
	}
	@Override
	public String toString() {
		return "Users [name=" + name + ", password=" + password + ", age=" + age + "]";
	}
}

5 编写controller

@Controller
public class UsersController {
	@RequestMapping("/addUser")
	public String addUser() {
		return "addUser";
	}
	
	@RequestMapping("/saveUser")
	public String saveUser(Users users) {
		return "ok";
	}
}

6 编写启动类

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

7 启动测试

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Smart-Socket中,AioQuickClient是一个异步的TCP客户端,可以通过以下方式获取服务端发送的数据: 1.实现AioQuickClient的AioListener接口,并重写onAfterReceivedBytes方法,在该方法中获取服务端发送的数据。 例如: ```java public class MyAioListener implements AioListener<byte[]> { @Override public void onAfterConnected(ChannelContext<byte[]> channelContext, boolean isConnected, boolean isReconnect) throws Exception { // 连接成功后的操作 } @Override public void onAfterSent(ChannelContext<byte[]> channelContext, IoPacket packet, boolean isSentSuccess) throws Exception { // 数据发送完成后的操作 } @Override public void onAfterReceivedBytes(ChannelContext<byte[]> channelContext, byte[] bytes, int length) throws Exception { // 获取服务端发送的数据 String receivedData = new String(bytes, 0, length, "UTF-8"); System.out.println("接收到服务端发送的数据:" + receivedData); } @Override public void onAfterClose(ChannelContext<byte[]> channelContext, Throwable throwable, String remark, boolean isRemove) throws Exception { // 关闭连接后的操作 } } ``` 2.在创建AioQuickClient时,将实现了AioListener接口的传入构造器中。 例如: ```java public static void main(String[] args) throws Exception { // 创建AioQuickClient AioQuickClient aioQuickClient = new AioQuickClient("127.0.0.1", 8888, new MyAioListener()); aioQuickClient.start(); // 发送数据 aioQuickClient.send("Hello, server!"); } ``` 以上代码中,创建了一个AioQuickClient,并将实现了AioListener接口的MyAioListener传入构造器中。在MyAioListener的onAfterReceivedBytes方法中获取服务端发送的数据。 注意:Smart-Socket中的AioQuickClient是一个异步的TCP客户端,因此在获取服务端发送的数据时,需要使用回调函数的方式,而不是阻塞式的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值