Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbstractMessageSource does not properly interact with DelegatingMessageSource parent [SPR-16047] #20596

Closed
spring-issuemaster opened this issue Oct 5, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Oct 5, 2017

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:

  1. comment the MessageSource bean in ControllersConfig
  2. comment the line
    return new Class<?>[] { TestConfig.class }; in Initialzer and uncomment the line return null;
  3. 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:

  • #20099 Introduce null-safety of Spring Framework API
  • #20675 AbstractMessageSource does not support null as default message anymore

Referenced from: commits c3378fd

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 18, 2017

Juergen Hoeller commented

This comes down to our nullability revision (#20099) in AbstractMessageSource: getMessageFromParent does not properly interact with a DelegatingMessageSource parent anymore. We'll fix this for 5.0.1.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 18, 2017

Juergen Hoeller commented

This will be available in the upcoming 5.0.1.BUILD-SNAPSHOT. Feel free to give it a try...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.