欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot 2 Thymeleaf服務(wù)器端表單驗(yàn)證實(shí)現(xiàn)詳解

 更新時(shí)間:2019年11月19日 08:51:51   作者:gdjlc  
這篇文章主要介紹了Spring Boot 2 Thymeleaf服務(wù)器端表單驗(yàn)證實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Spring Boot 2 Thymeleaf服務(wù)器端表單驗(yàn)證實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

表單驗(yàn)證分為前端驗(yàn)證和服務(wù)器端驗(yàn)證。

服務(wù)器端驗(yàn)證方面,Java提供了主要用于數(shù)據(jù)驗(yàn)證的JSR 303規(guī)范,而Hibernate Validator實(shí)現(xiàn)了JSR 303規(guī)范。

項(xiàng)目依賴加入spring-boot-starter-thymeleaf時(shí),默認(rèn)就會加入Hibernate Validator的依賴。

開發(fā)環(huán)境:IntelliJ IDEA 2019.2.2

Spring Boot版本:2.1.8

新建一個(gè)名稱為demo的Spring Boot項(xiàng)目。

1、pom.xml

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
    </dependency>

2、src/main/java/com/example/demo/User.java

package com.example.demo;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

public class User {
  @NotBlank(message = "用戶名不能為空")
  String name;
  @Length(min = 11, max = 11, message = "手機(jī)號長度必須11位")
  String phone;
  @Size(min = 6, max = 20, message = "密碼長度6-20位")
  String password;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getPhone() {
    return phone;
  }
  public void setPhone(String phone) {
    this.phone = phone;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
}

3、src/main/java/com/example/demo/FormController.java

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;

@Controller
public class FormController {
  @RequestMapping("/{form}")
  public String form(@PathVariable String form, @ModelAttribute User user){
    return form;
  }

  @PostMapping("/submit")
  public String submit(@Valid User user, BindingResult result){
    if (result.hasErrors()) {
      List<ObjectError> list = result.getAllErrors();
      for (ObjectError error : list) {
        System.out.println(error.getDefaultMessage());
      }
      return "form";
    }
    //業(yè)務(wù)邏輯處理
    return "form";
  }
}

4、src/main/resources/templates/form.html

前端通過#fields對象輸出錯(cuò)誤信息有2種方式,1種是在每個(gè)字段后面輸出,另1種是全部在一起輸出。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>表單的提交處理</title>
  <style>
    .fieldError{color: red}
  </style>
</head>
<body>
  <form method="post" th:action="@{/submit}" th:object="${user}">
    <table>
      <tr>
        <td>用戶名:</td>
        <td><input type="text" th:field="*{name}" />
          <span class="fieldError" th:if="${#fields.hasErrors('*{name}')}" th:errors="*{name}"></span>
        </td>
      </tr>
      <tr>
        <td>手機(jī)號:</td>
        <td><input type="text" th:field="*{phone}" />
          <span class="fieldError" th:if="${#fields.hasErrors('*{phone}')}" th:errors="*{phone}"></span>
        </td>
      </tr>
      <tr>
        <td>密碼:</td>
        <td><input type="text" th:field="*{password}" />
          <span class="fieldError" th:if="${#fields.hasErrors('*{password}')}" th:errors="*{password}"></span>
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <input type="submit" value="提交" />
          <div th:each="err : ${#fields.errors('*')}">
            <span th:text="${err}" class="fieldError"></span>
          </div>
        </td>
      </tr>
    </table>
  </form>
</body>
</html>

啟動服務(wù)后,瀏覽器訪問http://localhost:8080/form,點(diǎn)擊提交按鈕,結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Boot整合tk.mybatis代碼實(shí)例

    Spring Boot整合tk.mybatis代碼實(shí)例

    這篇文章主要介紹了Spring Boot整合tk.mybatis代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java開發(fā)必備的三大修飾符

    Java開發(fā)必備的三大修飾符

    JAVA的三個(gè)修飾:static,final,abstract,在JAVA語言里無處不在,但是它們都能修飾什么組件,修飾組件的含義又有什么限制,總是混淆.所以來總結(jié)一下,需要的朋友可以參考下
    2021-06-06
  • 詳解MyBatis逆向工程

    詳解MyBatis逆向工程

    本篇文章主要介紹了詳解MyBatis逆向工程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • IntelliJ IDEA2019實(shí)現(xiàn)Web項(xiàng)目創(chuàng)建示例

    IntelliJ IDEA2019實(shí)現(xiàn)Web項(xiàng)目創(chuàng)建示例

    這篇文章主要介紹了IntelliJ IDEA2019實(shí)現(xiàn)Web項(xiàng)目創(chuàng)建示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 詳解Spring中Lookup注解的使用

    詳解Spring中Lookup注解的使用

    我們知道在spring容器中單獨(dú)的一個(gè)抽象類是不能成為一個(gè)bean的,那么有沒有辦法呢?這個(gè)時(shí)候我們可以使用Lookup注解,下面跟隨小編看下Spring中Lookup注解的使用
    2021-10-10
  • Nacos單機(jī)版安裝啟動的全流程

    Nacos單機(jī)版安裝啟動的全流程

    這篇文章主要介紹了Nacos單機(jī)版安裝啟動的全流程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 深入解析Java多態(tài)進(jìn)階學(xué)習(xí)

    深入解析Java多態(tài)進(jìn)階學(xué)習(xí)

    java的動態(tài)綁定機(jī)制非常重要。這篇文章將帶大家更深入的學(xué)習(xí)一下Java的多態(tài),文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下
    2022-07-07
  • 淺談一下Servlet的定義以及運(yùn)行原理

    淺談一下Servlet的定義以及運(yùn)行原理

    相信有很多剛?cè)胄械呐笥褧苫骃ervlet到底是個(gè)什么意思,那么這篇文章就來淺談一下到底什么是Servlet,以及Servlet的原理與如何寫一個(gè)Servlet,,需要的朋友可以參考下
    2023-03-03
  • java中BCryptPasswordEncoder密碼的加密與驗(yàn)證方式

    java中BCryptPasswordEncoder密碼的加密與驗(yàn)證方式

    這篇文章主要介紹了java中BCryptPasswordEncoder密碼的加密與驗(yàn)證方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn)

    Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn)

    本文主要介紹了Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評論