Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JSR-303 bean validation not working, empty bindingresult

Hi StackOverflow

I have tried to add beanvalidation to my small webapplication. Until now it doesn't work because my bindingresult stays empty. I have done the following:

  • Add all dependencies (Classmate, Javax.el, Jboss-logging, Validation-api)
  • Added annotations
  • Updated controller with @valid and BindingResult
  • Updated my jsp page
  • Maked sure I have <mvc:annotation-driven/> in my dispatcher-servlet.xml

I have looked at similar questions here on stackoverflow, yet I have not been able to find the answer I need. I'm using Apache Tomcat.

My controller:

@RequestMapping(value = {"/cardForm"}, method = POST, params = {"dispatcher=createCard"})
    public String addCard(@Valid @ModelAttribute("card") Card card, BindingResult result) {
        System.out.println(result.getErrorCount());
        if (result.hasErrors()) {
            return "cardForm";
        } else {
            try {
                service.addCard(card);
            } catch (MapperException ex) {
                Logger.getLogger(CardController.class.getName()).log(Level.SEVERE, null, ex);
            }
            return "redirect:/";
        }
    }

My class to be validated:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package domain;
import javax.persistence.Entity;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**
 *
 * @author niels
 */
@Entity
public class Card extends DomainObjectBase {
    @NotNull(message="{error.name.null}")
    private String name;
    @NotNull(message="{error.type.null}")
    private String type;
    @NotNull(message="{error.text.null}")
    private String text;
    @NotNull(message="{error.playerClass.null}")
    private String playerClass;
    @Min(value=0,message="{error.attack.min}")
    private int attack;
    @Min(value=0,message="{error.health.min}")
    private int health;
    @Min(value=0,message="{error.cost.min}")
    private int cost;

    public Card() {}

    public Card(String name, String type, String text, String playerClass, int attack, int health, int cost) throws DomainException {
        setName(name);
        setType(type);
        setText(text);
        setPlayerClass(playerClass);
        setAttack(attack);
        setHealth(health);
        setCost(cost);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getPlayerClass() {
        return playerClass;
    }

    public void setPlayerClass(String playerClass) {
        this.playerClass = playerClass;
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) throws DomainException {
        this.attack = attack;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) throws DomainException {
        this.health = health;
    }

    public int getCost() {
        return cost;
    }

    public void setCost(int cost) throws DomainException {
        this.cost = cost;
    }

}

My dispatcher-servlet.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <mvc:annotation-driven/>
    <context:component-scan base-package="controller"/>
    <!--
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages"></property>
    </bean>
    -->
    <mvc:interceptors>    
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang" />
    </mvc:interceptors>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en_GB"/>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <bean class="org.springframework.context.support.ResourceBundleMessageSource"
        id="messageSource">
        <property name="basename" value="messages" />
    </bean>


</beans>

My cardForm.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html lang="en">

    <head>

        <title>
            Hearthstone | 
            <spring:message code="title.addCard">
            </spring:message>
        </title>
        <jsp:include page="title.jsp" />


    </head>

    <body>

        <div id="wrapper">
            <jsp:include page="menu.jsp" />
            <!-- Page Content -->
            <div id="page-content-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-6 col-lg-offset-3">


                            <form:form action="${pageContext.request.contextPath}/cardForm.htm" method="post" modelAttribute="card">
                                <form:errors path="*" cssClass="errorblock" element="div" />
                                <c:choose>
                                    <c:when test="${card.id > 0}">
                                        <h3><spring:message code="card.update">
                                        </spring:message></h3>
                                        <form:hidden path="id" value="${card.id}"/>
                                        <input type="hidden" name="dispatcher" value="updateCard"/>
                                    </c:when>
                                    <c:otherwise>
                                        <h3><spring:message code="card.add">
                                        </spring:message></h3>
                                        <input type="hidden" name="dispatcher" value="createCard"/>
                                    </c:otherwise>
                                </c:choose>


                                <div class="form-group">
                                    <form:label path="name"><spring:message code="card.name">
                                        </spring:message>: </form:label>
                                    <form:input path="name" class="form-control"></form:input>
                                    </div>
                                    <div class="form-group">
                                    <form:label path="type">Type: </form:label>

                                    <form:select class="form-control" path="type">
                                        <form:option value="Minion">Minion</form:option>
                                        <form:option value="Spell">Spell</form:option>
                                        <form:option value="Weapon">Weapon</form:option>
                                    </form:select>
                                </div>
                                <div class="form-group">
                                    <form:label path="text"><spring:message code="card.text">
                                        </spring:message>: </form:label>
                                    <form:input path="text" class="form-control"></form:input>
                                    </div>
                                    <div class="form-group">
                                    <form:label path="playerClass"><spring:message code="card.playerClass">
                                        </spring:message>: </form:label>
                                    <form:select class="form-control" path="playerClass">
                                        <form:option value="All classes">All classes</form:option>
                                        <form:option value="Priest">Priest</form:option>
                                        <form:option value="Shaman">Shaman</form:option>
                                        <form:option value="Mage">Mage</form:option>
                                        <form:option value="Warrior">Warrior</form:option>
                                        <form:option value="Warlock">Warlock</form:option>
                                        <form:option value="Paladin">Paladin</form:option>
                                        <form:option value="Rogue">Rogue</form:option>
                                        <form:option value="Druid">Druid</form:option>
                                        <form:option value="Hunter">Hunter</form:option>
                                    </form:select>
                                </div>

                                <div class="form-group">
                                    <form:label path="attack"><spring:message code="card.attack">
                                        </spring:message>: </form:label>
                                    <form:input path="attack" class="form-control"></form:input>
                                    </div>
                                    <div class="form-group">
                                    <form:label path="health"><spring:message code="card.health">
                                        </spring:message>: </form:label>
                                    <form:input path="health" class="form-control"></form:input>
                                    </div>
                                    <div class="form-group">
                                    <form:label path="cost"><spring:message code="card.cost">
                                        </spring:message>: </form:label>
                                    <form:input path="cost" class="form-control"></form:input>
                                    </div>
                                    <div class="form-group"><form:button type="submit" class="btn btn-primary"><spring:message code="form.send">
                                        </spring:message></form:button></div>
                            </form:form>
                        </div>

                    </div>
                </div>

            </div>
            <!-- /#page-content-wrapper -->

        </div>
        <!-- /#wrapper -->

        <!-- Bootstrap Core JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

        <!-- Menu Toggle Script -->
        <script>
            $("#menu-toggle").click(function (e) {
                e.preventDefault();
                $("#wrapper").toggleClass("toggled");
            });
        </script>

    </body>

</html>
like image 418
Niels Vermeiren Avatar asked Sep 28 '22 06:09

Niels Vermeiren


1 Answers

Ok I got that figured out for you and I must say that everything does work, but you are using wrong javax.validation annotation :)

You use @NotNull, but the fact is that fields in instance of Card received by controller.CardController#addCard(domain.Card, org.springframework.validation.BindingResult) are never null, they are set to empty strings - this is why you don't see any validation errors in BindingResult.

Just add (org.hibernate.validator.constraints.)@NotEmpty annotations instead of your current @NotNull annotations and you will see validation errors reported in your BindingResult.

like image 161
Rafal G. Avatar answered Nov 15 '22 03:11

Rafal G.