SpringBoot教程 | 第十九篇: 驗證表單信息

這篇文篇主要簡述如何在springboot中驗證表單信息。在springmvc工程中,需要檢查表單信息狡门,表單信息驗證主要通過注解的形式瑟押。

構建工程

創(chuàng)建一個springboot工程搀捷,由于用到了 web 、thymeleaf多望、validator嫩舟、el,引入相應的起步依賴和依賴怀偷,代碼清單如下:

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

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-validator</artifactId>

</dependency>

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-el</artifactId>

</dependency>

</dependencies>


創(chuàng)建一個PresonForm的Object類

package com.forezp.entity;

import javax.validation.constraints.Min;

import javax.validation.constraints.NotNull;

import javax.validation.constraints.Size;

/**

* Created by fangzhipeng on 2017/4/19.

*/

public class PersonForm {

? ? @NotNull

? ? @Size(min=2, max=30)

? ? private String name;

? ? @NotNull

? ? @Min(18)

? ? private Integer age;

? ? public String getName() {

? ? ? ? return this.name;

? ? }

? ? public void setName(String name) {

? ? ? ? this.name = name;

? ? }

? ? public Integer getAge() {

? ? ? ? return age;

? ? }

? ? public void setAge(Integer age) {

? ? ? ? this.age = age;

? ? }

? ? public String toString() {

? ? ? ? return "Person(Name: " + this.name + ", Age: " + this.age + ")";

? ? }

}


這個實體類家厌,在2個屬性:name,age.它們各自有驗證的注解:

@Size(min=2, max=30) name的長度為2-30個字符

@NotNull 不為空

@Min(18)age不能小于18

創(chuàng)建 web Controller

@Controller

public class WebController extends WebMvcConfigurerAdapter {

? ? @Override

? ? public void addViewControllers(ViewControllerRegistry registry) {

? ? ? ? registry.addViewController("/results").setViewName("results");

? ? }

? ? @GetMapping("/")

? ? public String showForm(PersonForm personForm) {

? ? ? ? return "form";

? ? }

? ? @PostMapping("/")

? ? public String checkPersonInfo(@Valid PersonForm personForm, BindingResult bindingResult) {

? ? ? ? if (bindingResult.hasErrors()) {

? ? ? ? ? ? return "form";

? ? ? ? }

? ? ? ? return "redirect:/results";

? ? }

}


創(chuàng)建form表單

src/main/resources/templates/form.html:

<html>

? ? <body>

? ? ? ? <form action="#" th:action="@{/}" th:object="${personForm}" method="post">

? ? ? ? ? ? <table>

? ? ? ? ? ? ? ? <tr>

? ? ? ? ? ? ? ? ? ? <td>Name:</td>

? ? ? ? ? ? ? ? ? ? <td><input type="text" th:field="*{name}" /></td>

? ? ? ? ? ? ? ? ? ? <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>

? ? ? ? ? ? ? ? </tr>

? ? ? ? ? ? ? ? <tr>

? ? ? ? ? ? ? ? ? ? <td>Age:</td>

? ? ? ? ? ? ? ? ? ? <td><input type="text" th:field="*{age}" /></td>

? ? ? ? ? ? ? ? ? ? <td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age Error</td>

? ? ? ? ? ? ? ? </tr>

? ? ? ? ? ? ? ? <tr>

? ? ? ? ? ? ? ? ? ? <td><button type="submit">Submit</button></td>

? ? ? ? ? ? ? ? </tr>

? ? ? ? ? ? </table>

? ? ? ? </form>

? ? </body>

</html>


注冊成功的頁面

src/main/resources/templates/results.html:

<html>

<body>

Congratulations! You are old enough to sign up for this site.

</body>

</html>


演示

啟動工程,訪問http://localhost:8080/:


如果你輸入A和15椎工,點擊 submit:


如果name 輸入N, age為空:


如果輸入:forezp. 18


?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末饭于,一起剝皮案震驚了整個濱河市蜀踏,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌掰吕,老刑警劉巖果覆,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異殖熟,居然都是意外死亡局待,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門菱属,熙熙樓的掌柜王于貴愁眉苦臉地迎上來钳榨,“玉大人,你說我怎么就攤上這事纽门⊙Τ埽” “怎么了?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵赏陵,是天一觀的道長饼齿。 經(jīng)常有香客問我,道長瘟滨,這世上最難降的妖魔是什么候醒? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮杂瘸,結果婚禮上倒淫,老公的妹妹穿的比我還像新娘。我一直安慰自己败玉,他們只是感情好敌土,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著运翼,像睡著了一般返干。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上血淌,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天矩欠,我揣著相機與錄音,去河邊找鬼悠夯。 笑死癌淮,一個胖子當著我的面吹牛,可吹牛的內容都是我干的沦补。 我是一名探鬼主播乳蓄,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼夕膀!你這毒婦竟也來了虚倒?” 一聲冷哼從身側響起美侦,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎魂奥,沒想到半個月后菠剩,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡捧弃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年赠叼,在試婚紗的時候發(fā)現(xiàn)自己被綠了擦囊。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片违霞。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖瞬场,靈堂內的尸體忽然破棺而出买鸽,到底是詐尸還是另有隱情,我是刑警寧澤贯被,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布眼五,位于F島的核電站,受9級特大地震影響彤灶,放射性物質發(fā)生泄漏看幼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一幌陕、第九天 我趴在偏房一處隱蔽的房頂上張望诵姜。 院中可真熱鬧,春花似錦搏熄、人聲如沸棚唆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宵凌。三九已至,卻和暖如春止后,著一層夾襖步出監(jiān)牢的瞬間瞎惫,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工译株, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瓜喇,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓古戴,卻偏偏與公主長得像欠橘,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子现恼,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內容