0

I studying Spring MVC and came to validation.

Here's my build.gradle file.

plugins {
    id 'java'
    id 'war'
}

group 'org.example'
version '1.0-SNAPSHOT'

def hibernateVersion = '7.0.4.Final'
def junitVersion = '5.8.2'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:' + junitVersion
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:' + junitVersion

    implementation 'org.springframework:spring-webmvc:5.3.16'

    // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
    compileOnly group: 'javax.servlet',
            name: 'javax.servlet-api',
            version: '4.0.1'

    implementation 'javax.servlet:jstl:1.2'

    implementation group: 'org.hibernate',
            name: 'hibernate-validator',
            version: hibernateVersion

    implementation group: 'org.hibernate',
            name: 'hibernate-validator-cdi',
            version: hibernateVersion

    testImplementation group: 'org.glassfish',
            name: 'jakarta.el',
            version: '4.0.1'

    implementation group: 'javax.validation',
            name: 'validation-api',
            version: '2.0.1.Final'

    implementation group: 'jakarta.el',
            name: 'jakarta.el-api',
            version: '4.0.0'


    implementation group: 'javax.el',
            name: 'javax.el-api',
            version: '3.0.0'
}

test {
    useJUnitPlatform()
}

Here's my controller and in custemerConfirmation I want to validate Customer model but nothing happens.

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @GetMapping("/form")
    public String customerForm(Model model) {
        model.addAttribute("customer", new Customer());
        return "customer/form";
    }

    @GetMapping("/confirmation")
    public String customerConfirmation(
            @ModelAttribute("customer") @Valid Customer customer,
            BindingResult bindingResult
    ) {
        System.out.printf("\u001B[34m===Binding result: %s===\u001B[37m\n", bindingResult);

        if (bindingResult.hasErrors()) {
            return "customer/form";
        }


        customer.setFirstName(customer.getFirstName().toUpperCase());
        customer.setLastName(customer.getLastName().toUpperCase());

        return "customer/confirmation";
    }
}

And my Customer model

public class Customer {
    private String firstName;
    @NotNull(message = "is required")
    @NotEmpty(message = "is required")
    @Length(min = 2, message = "Min length is 2 characters")
    private String lastName;

    public Customer() {
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

The validated form

<form:form method="get" action="/customer/confirmation" modelAttribute="customer">
    First name: <form:input path="firstName"/>
    <br><br>
    Last name (*): <form:input path="lastName"/> <form:errors path="lastName" cssClass="field-error"/>
    <br><br>
    <input type="submit" value="Submit"/>
</form:form>
toiepp
  • 85
  • 6

0 Answers0