Closed
Description
Hans Desmet opened SPR-16047 and commented
Given following Initializer:
package be.vdab.web;
import javax.servlet.Filter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import be.vdab.test.TestConfig;
public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { TestConfig.class };
//return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { ControllersConfig.class };
}
@Override
protected Filter[] getServletFilters() {
return new Filter[] { new CharacterEncodingFilter("UTF-8") };
}
}
and following Controllers config:
package be.vdab.web;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@ComponentScan
public class ControllersConfig extends WebMvcConfigurerAdapter {
@Bean
MessageSource messageSource() {
ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
source.setBasename("classpath:messages");
source.setFallbackToSystemLocale(false);
return source;
}
}
and following Java config
package be.vdab.test;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfig {
}
and following controller:
package be.vdab.web;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/")
class IndexController {
private static final String VIEW = "/WEB-INF/JSP/index.jsp";
@GetMapping
ModelAndView findByPostcodeReeks() {
CommandObject reeks = new CommandObject();
return new ModelAndView(VIEW).addObject(reeks);
}
@GetMapping(params = { "value" })
ModelAndView findByPostcodeReeks(CommandObject reeks, BindingResult bindingResult) {
ModelAndView modelAndView = new ModelAndView(VIEW);
return modelAndView;
}
@InitBinder("commandObject")
void initBinderPostcodeReeks(DataBinder dataBinder) {
dataBinder.setRequiredFields("value");
}
}
and following Command object class
package be.vdab.web;
public class CommandObject {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
and following index.jsp
<%@page contentType='text/html' pageEncoding='UTF-8' session='false'%>
<%@taglib prefix='form' uri='http://www.springframework.org/tags/form'%>
<!doctype html>
<html lang='nl'>
<head>
<title>
Test
</title>
</head>
<body>
<h1>Test</h1>
<form:form action='' modelAttribute='commandObject' method='get'>
<form:input path='value' autofocus='autofocus'/>
<form:errors path='value'/>
<input type='submit' value='Zoeken'>
</form:form>
</body>
</html>
when you run the webapp, leave the field empty and submit the form, you don't see the error message.
The problem is solved in three different ways:
- comment the MessageSource bean in ControllersConfig
- comment the line
return new Class<?>[] { TestConfig.class }; in Initialzer and uncomment the line return null; - Downgrade the webapp to Spring 4.3.11
A project to show the problem is at https://github.com/desmethans/formerrorstag.git
Affects: 5.0 GA
Reference URL: https://github.com/desmethans/formerrorstag.git
Issue Links:
- Introduce null-safety of Spring Framework API [SPR-15540] #20099 Introduce null-safety of Spring Framework API
- AbstractMessageSource does not support null as default message anymore [SPR-16127] #20675 AbstractMessageSource does not support null as default message anymore
Referenced from: commits c3378fd