通過springboot 去創建和提交一個表單(七)


創建工程

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

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>

  

創建實體

代碼清單如下:

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.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!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>
src/main/resources/templates/result.html
 
<!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>Result</h1>
     <p th:text= "'id: ' + ${greeting.id}"  />
     <p th:text= "'content: ' + ${greeting.content}"  />
     <a href= "/greeting" >Submit another message</a>
</body>
</html>

  

啟動工程,訪問ttp://localhost:8080/greeting:

點擊submit:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM