Java springcloud B2B2C o2o多用戶商城 springcloud架構(十九) 驗證表單信息

這篇文篇主要簡述如何在springboot中驗證表單信息。在springmvc工程中,須要檢查表單信息,表單信息驗證主要經過註解的形式。html

構建工程

建立一個springboot工程,因爲用到了 web 、thymeleaf、validator、el,引入相應的起步依賴和依賴,代碼清單以下:java

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

建立一個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.它們各自有驗證的註解:web

  • @Size(min=2, max=30) name的長度爲2-30個字符
  • @NotNull 不爲空
  • @Min(18)age不能小於18

建立 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";
    }
}

建立form表單

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

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

架構代碼以下 :

"分佈式b2b <wbr

 

須要JAVASpring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼請加企鵝求求:一零三八七七四六二六apache

相關文章
相關標籤/搜索