简体   繁体   中英

Server-side validation example?/Java

I am trying to create a registration form that performs both client side and server side validation. I am using Spring MVC and Hibernate to perform this. I already have the client side figured out, but I'm confused on how to do server side validation? Is this even possible with what I'm trying to achieve?

I apologize if I don't make sense, I am a fairly new programmer. If more info is needed then please let me know.

Hibernate Validator can meet your requirements validating fields on the server-side.

import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.CreditCardNumber;
import org.hibernate.validator.constraints.Email;

public class User {

    @NotNull(message = "username cannot be null")
    @Size(min = 2, max = 15)
    private String username;

    @Max(100)
    private int age;

    @Email
    private String email;

    @CreditCardNumber
    private String creditCardNumber;

    // constructors, getters, setters
}

Maven dependencies:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId>
   <version>5.4.1.Final</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM