samedi 18 avril 2015

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://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:p="http://ift.tt/1jdM0fE"
xmlns:context="http://ift.tt/GArMu7"
xmlns:mvc="http://ift.tt/1bHqwjR"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/1cnl1uo
http://ift.tt/GArMu7
http://ift.tt/1ldEMZY
http://ift.tt/1bHqwjR
http://ift.tt/1kF4x7W">


<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://ift.tt/QfKAz6" prefix="c" %>
<%@ taglib prefix="spring" uri="http://ift.tt/18bwTB1"%>
<%@taglib uri="http://ift.tt/IED0jK" 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="http://ift.tt/19wK3Lg"></script>

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

</body>

</html>

Aucun commentaire:

Enregistrer un commentaire