Description
Hans Desmet opened SPR-6552 and commented
The validation with DataBinder.setRequiredFiels doesn't work anymore.
Next example validated with Spring 2.5.6, not with 3.0 RC3
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.example.web"/>
</beans>
SearchController.java
package org.example.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class SearchController {
private final static String VIEW= "/WEB-INF/search.jsp";
@RequestMapping(value = "/search", method = RequestMethod.GET)
public String searchForm(Model model) {
model.addAttribute(new SearchForm());
return VIEW;
}
@RequestMapping(value = "/search", method=RequestMethod.POST)
public String searchForm(@ModelAttribute SearchForm searcForm, BindingResult bindingResult, Model model) {
model.addAttribute("hasErrors", bindingResult.hasErrors());
return VIEW;
}
private final String[] requiredFields = {"word"};
@InitBinder("searchForm")
public void initBinderVanTotPostcode(DataBinder dataBinder) {
dataBinder.setRequiredFields(requiredFields);
}
}
SearchForm.java
package org.example.web;
public class SearchForm {
private String word;
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
}
search.jsp
<%@page
contentType="text/html" pageEncoding="UTF-8" session="false"%>
<%@taglib
prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Search</title>
</head>
<body>
<form:form commandName="searchForm">
<div>
<form:label path="word">Word:</form:label>
<form:input path="word"/>
<form:errors path="word"/>
<input type="submit" value="Search"/>
</div>
<div>${hasErrors}</div>
</form:form>
</body>
</html>
Affects: 3.0 RC3