企业级 SpringBoot 教程 (二十)处理表单提交

这篇文件主要介绍经过springboot 去建立和提交一个表单。html

建立工程

涉及了 web,加上spring-boot-starter-web和spring-boot-starter-thymeleaf的起步依赖。web

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

  

建立实体

代码清单以下:spring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Greeting {
private long id;
private String content;
public long getId() {
return id;
}
public void setId( long id) {
this .id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this .content = content;
}
}

  

建立Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Controller
public class GreetingController {
@GetMapping ( "/greeting" )
public String greetingForm(Model model) {
model.addAttribute( "greeting" , new Greeting());
return "greeting" ;
}
@PostMapping ( "/greeting" )
public String greetingSubmit( @ModelAttribute Greeting greeting) {
return "result" ;
}
}

  

页面展现层

src/main/resources/templates/greeting.htmlspringboot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE HTML>
<html xmlns:th= "http://www.thymeleaf.org" >
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action= "#" th:action= "@{/greeting}" th:object= "${greeting}" method= "post" >
<p>Id: <input type= "text" th:field= "*{id}" /></p>
<p>Message: <input type= "text" th:field= "*{content}" /></p>
<p><input type= "submit" value= "Submit" /> <input type= "reset" value= "Reset" /></p>
</form>
</body>
</html>

  

架构代码以下 :

"分布式b2b <wbr

资料和源码来源地址架构

Spring Cloud大型企业分布式微服务云架构源码请加企鹅求求:一七九一七四三三八零
app

相关文章
相关标签/搜索