Spring如何處理表單提交
今天我們來講一個(gè)最簡單的表單提交處理的例子,通過提交一個(gè)表單給朋友打一聲招呼!
看這邊文章之前,你至少應(yīng)該了解基于Spring的Web開發(fā)的基礎(chǔ)知識,當(dāng)然,你還是應(yīng)該準(zhǔn)備好開發(fā)環(huán)境:
- IDE+Java環(huán)境(JDK 1.7或以上版本)
- Maven 3.0+(Eclipse和Idea IntelliJ內(nèi)置,如果使用IDE并且不使用命令行工具可以不安裝)
準(zhǔn)備POM文件
POM.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.tianmaying</groupId> <artifactId>springboot-form-submission-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-form-submission-demo</name> <description>Springboot form submission demo</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.5.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
創(chuàng)建Controller
我們已經(jīng)知道可以通過Controller來進(jìn)行URL路由,Spring WebMvc框架會將Servlet容器里收到的HTTP請求根據(jù)路徑分發(fā)給對應(yīng)的@Controller
類進(jìn)行處理、而 @RequestMapping
注解表明該方法處理那些URL對應(yīng)的HTTP請求。
我們的SayHelloController
的代碼如下:
package com.tianmaying.springboot.formsubmission; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class SayHelloController { @RequestMapping(value="/sayhello", method=RequestMethod.GET) public String sayHelloForm(Model model) { model.addAttribute("helloMessage", new HelloMessage()); return "sayhello"; } @RequestMapping(value="/sayhello", method=RequestMethod.POST) public String sayHello(@ModelAttribute HelloMessage helloMessage, Model model) { model.addAttribute("helloMessage", helloMessage); return "message"; } }
- 針對
/sayhello
的GET
請求,我們返回提交表單的頁面,即sayHello.html
- 針對
/sayhello
的POST
請求,我們進(jìn)行表單的處理,然后將打招呼的信息渲染到message.html
頁面返回。
表單處理也無外乎這兩件事情:顯示表單,處理表單提交。
顯示表單
/sayhello
的GET
請求里,在渲染頁面之前,我們通過model.addAttribute("helloMessage", new HelloMessage());
告訴頁面綁定到一個(gè)空的HelloMessage
對象,這樣sayHello.html
頁面初始時(shí)就會顯示一個(gè)空白的表單。
HelloMessage
package com.tianmaying.springboot.formsubmission; public class HelloMessage { private String name; private String message; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
僅僅扔一個(gè)空白對象給表單還不夠,你還得告訴表單的各個(gè)輸入如何綁定到對象的各個(gè)屬性上。這個(gè)時(shí)候我們要用上Themeleaf了。
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>腳本之家: Spring表單提交處理</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>表單處理演示</h1> <form action="#" th:action="@{/sayhello}" th:object="${helloMessage}" method="post"> <p>friend: <input type="text" th:field="*{name}" /></p> <p>message: <input type="text" th:field="*{message}" /></p> <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> </form> </body> </html>
th:action="@{/sayhello}"
表示將表單提交的POST
請求交給/sayhello
這個(gè)URL來處理th:object="${helloMessage}"
表示用來搜集的表單數(shù)據(jù)的對象時(shí)helloMessage
,即用戶輸入信息將存儲于這個(gè)對象中- 兩個(gè)表單域分別增加了屬性
th:field="*{name}"
和th:field="*{message}"
,這就是將一個(gè)表單域綁定到特定的對象屬性
處理表單
把處理表單的Controller代碼再單獨(dú)拿出來:
@RequestMapping(value="/sayhello", method=RequestMethod.POST) public String greetingSubmit(@ModelAttribute HelloMessage helloMessage, Model model) { model.addAttribute("helloMessage", helloMessage); return "message"; }
處理表單就非常簡單了,通過@ModelAttribute
,我們可以直接通過helloMessage
對象來處理用戶提交的信息了。
從最早JSP和Servlet時(shí)代過來的人,對從request中根據(jù)參數(shù)名稱逐個(gè)獲取信息,然后自己去設(shè)置對應(yīng)對象屬性的場景一定會歷歷在目,那叫慘絕人寰哪?,F(xiàn)在我們只需專注于Model的業(yè)務(wù)邏輯處理了,Spring MVC和Thymeleaf這對黃金組合幫我們搞定了表單和對象綁定這樣繁瑣的事情。
Run起來
這應(yīng)該是你很熟悉的代碼了:
package com.tianmaying.springboot.formsubmission; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
SpringBootApplication標(biāo)注做的事情參考這里,mvn spring-boot:run
或在IDE中運(yùn)行main()
方法就可以看到效果了!不用裝Web服務(wù)器不用部署就能直接Run Web應(yīng)用的感覺確實(shí)很酸爽!
當(dāng)然,一個(gè)成熟的應(yīng)用,通常還需要做表單的驗(yàn)證操作,即確保用戶提交上來的數(shù)據(jù)是合法而且有效的!且待下回分解!
以上就是Spring如何處理表單提交的詳細(xì)內(nèi)容,更多關(guān)于Spring處理表單提交的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot基于SpringSecurity表單登錄和權(quán)限驗(yàn)證的示例
- Spring MVC接受表單自動封裝特性實(shí)例解析
- SpringSecurity 自定義表單登錄的實(shí)現(xiàn)
- SpringSecurity 默認(rèn)表單登錄頁展示流程源碼
- Spring Boot 2 Thymeleaf服務(wù)器端表單驗(yàn)證實(shí)現(xiàn)詳解
- 詳解SpringBoot構(gòu)建的Web項(xiàng)目如何在服務(wù)端校驗(yàn)表單輸入
- springmvc后臺基于@ModelAttribute獲取表單提交的數(shù)據(jù)
- layui 圖片上傳+表單提交+ Spring MVC的實(shí)例
- Spring Security 表單登錄功能的實(shí)現(xiàn)方法
- Spring Security在標(biāo)準(zhǔn)登錄表單中添加一個(gè)額外的字段
相關(guān)文章
關(guān)于@CacheEvict無法解決分頁緩存清除的解決思路
這篇文章主要介紹了關(guān)于@CacheEvict無法解決分頁緩存清除的解決思路,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12零基礎(chǔ)寫Java知乎爬蟲之將抓取的內(nèi)容存儲到本地
上一回我們說到了如何把知乎的某些內(nèi)容爬取出來,那么這一回我們就說說怎么把這些內(nèi)容存儲到本地吧。2014-11-11詳解Spring Boot 使用slf4j+logback記錄日志配置
本篇文章主要介紹了Spring Boot 使用slf4j+logback記錄日志配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Spring Security其它權(quán)限校驗(yàn)方式&自定義權(quán)限校驗(yàn)方式
這篇文章主要介紹了Spring Security其它權(quán)限校驗(yàn)方式&自定義權(quán)限校驗(yàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Spring?Security如何實(shí)現(xiàn)升級密碼加密方式詳解
這篇文章主要為大家介紹了Spring?Security實(shí)現(xiàn)升級密碼加密方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01java synchronized同步靜態(tài)方法和同步非靜態(tài)方法的異同
這篇文章主要介紹了java synchronized同步靜態(tài)方法和同步非靜態(tài)方法的異同的相關(guān)資料,需要的朋友可以參考下2017-01-01