Hibernate validator nie działa

0

Witam, problem jest w tym gdyż używam Spring MVC + Hibernate validator do walidacji formularzy, tylko nie działa. BindingResult zawszę zwraca false, niezależnie od tego czy dane zostaną wprowadzone poprawnie czy też nie. Nie wiem w czym jest problem. Proszę o rzucenie okiem na kod i wskazanie gdzie robię błąd. Dopiero uczę się Spring'a.

Klasa Customer

package com.mzuchnik;


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


public class Customer {

    @NotBlank
    private String firstName;

    @Size(min = 1, message = "is required")
    private String lastName;


    public Customer() {
    }

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

Klasa CustomerController

package com.mzuchnik;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @RequestMapping(value = "/showForm")
    public String showForm(Model model)
    {
        Customer customer = new Customer();
        model.addAttribute("customer", customer);
        return "customer-form";
    }

    @RequestMapping(value = "/processForm")
    public String processForm(
            @Valid @ModelAttribute("customer") Customer customer,
            BindingResult theBindingResult)
    {
        if(theBindingResult.hasErrors()) { // <--- zawsze zwraca false :c
            return "customer-form";
        }else {
            return "customer-confirm";
        }
    }

}

customer-form.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Customer Form</title>
    <style>
        .error{ color:red }
    </style>
</head>
<body>

<form:form action="processForm" modelAttribute="customer">

    Podaj imię: <form:input path="firstName" />
    <form:errors path="firstName" cssClass="error"/>
    <br>
    Podaj nazwisko <form:input path="lastName" />
    <form:errors path="lastName" cssClass="error"/>
    <br>
    <input type="submit" value="Akceptuj"/>
</form:form>

</body>
</html>

customer-confirm.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Customer Confirm</title>
</head>
<body>
Twoję imię i nazwisko to: ${customer.firstName} ${customer.lastName}

</body>
</html>

Plik konfiguracyjny web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

   <!-- <mvc:annotation-driven />-->

    <context:component-scan base-package="com.mzuchnik"/>
    <context:annotation-config />


    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
0

Ja rozwiązuję to tak, że robię sobie pole private Validator validator w serwisie, a w metodzie odpowiadającej za dodanie czegoś do bazy wrzucam taki kod:

Set<ConstraintViolation<YourObject>> validationErrors = validator.validate(yourObject);
if(!validationErrors.isEmpty()){
        jakiś kod w przypadku niespełnienia warunków walidacji
}
dalej jazda z kodem w przypadku pomyślnej walidacji

1 użytkowników online, w tym zalogowanych: 0, gości: 1