Spring-Tiles annotation based configuration - spring

Hi I am a newbie in Spring framework, In my application I need to integrate Spring with Tiles. How can I add viewResolver for Tiles as well as internalViewResolver for my login page, or how can I make it as one.I am using annotation based configuration. My login page location is
My config class is
public class AppConfig extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
#Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
........//
}
where should I place my Tiles JSPs. I hope my question is clear, Please comment for additional information. Thanks!

You should be using UrlBasedViewResolver view resolver and not the InternalResourceViewResolver since the suffix, view name and prefix would be specified in the Tiles Definition XML files.
#Bean
public UrlBasedViewResolver viewResolver() {
UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
viewResolver.setViewClass(TilesView.class);
return viewResolver;
}
#Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
tilesConfigurer.setDefinitions(new String[]{
"/WEB-INF/defs/general.xml",
"/WEB-INF/defs/tiles.xml" //another definition
});
tilesConfigurer.setCheckRefresh(true);
return tilesConfigurer;
}
And add in your Tiles Def XML file for each views like this:
For url path http://localhost:8080/register:
#RequestMapping("/register")
Definition:
<definition name="page" template="/WEB-INF/layout/page.jsp">
<put-list-attribute name="stylesheets">
<add-attribute value="/resources/css/AdminLTE.min.css" />
</put-list-attribute>
<put-list-attribute name="javascripts">
<add-attribute value="/resources/js/main.js" />
</put-list-attribute>
</definition>
<definition name="register" extends="page">
<put-attribute name="title" value="User Registeration" />
<put-attribute name="body" value="/WEB-INF/jsp/register.jsp" />
<put-attribute name="current" value="register" />
</definition>
I have an example on Github for Spring Tiles but it is using XML Configuration, you can use above #Bean Configuration to make it use JavaConfig.

Related

Why application.properties is not executing properties after applying spring security in spring boot application?

I have created an spring-boot application. It was working fine all the css and js were mapping perfectly with my jsp pages and application was able to map to my jsp pages as well. By appication.properties file in resources folder.
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp
spring.mvc.static-path-pattern=/resources/**
server.port=8181
But since I have enabled Spring security I am not able to do that I needed to initialise #bean class
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
It is weird. Can anybody help me with it?
Thank you in advance,
Priyal shah.
Ensure that your class is extending WebMvcConfigurerAdapter class like below sample.
#Configuration
#ComponentScan(basePackages="com.package")
#EnableWebMvc
public class MvcConfigs extends WebMvcConfigurerAdapter{
#Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}

Spring boot: InternalResourceViewResolver not working

I spent several hours trying to use InternalResourceViewResolver in order to append prefix and suffix to html views.
My views located under static/pages/ and by Spring docs, folder static is considered to be one of defaults for static content. So, I could access profile page by pages/profile.html. But what I really want to have is profile instead of pages/profile.html.
I've tried several answers, but that does not work, like:
#Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("pages/");
resolver.setSuffix(".html");
return resolver;
}
and adding
#Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
Still does not work properly. By adding any suffixes or prefixes I could not found page on any path. I am starting to get 404 on pages/profile.html, but it also does not appear on other urls.
Just need add your own custom configuration like this
#Configuration
public class WebMvcConfig {
#Bean
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/jsp");
resolver.setSuffix(".jsp");
return resolver;
}
}
Then you can inspect all your beans via "http://localhost:8080/beans"
And you can verfity is it using the custom configured bean:
{
"bean": "defaultViewResolver",
"scope": "singleton",
"type": "org.springframework.web.servlet.view.InternalResourceViewResolver",
"resource": "class path resource [io/cloudhuang/web/WebMvcConfig.class]",
"dependencies": [ ]
}
But the eaiest way should be config it in the application.properties
spring.mvc.view.prefix=
spring.mvc.view.suffix=
For application.yaml
spring:
mvc:
view:
prefix: templates/
suffix: .jsp
Using Spring Boot you actually don't need to declare your own InternalResourceViewResolver. Boot declares it for you, and you can just add a couple of properties to your application.properties file. E.g. in your case these would be:
spring.mvc.view.prefix=/jsp
spring.mvc.view.suffix=.jsp

setting up spring view resolver using spring-java-config

I am trying to setup view-resolver for spring using java-config. I have tried 3 different method with different config found in 3 different sites. All of them working fine. My question is that Is there any specific standard / method / signature / interface method to defining the view-resolver ?
#Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver ();
resolver.setPrefix ("/WEB-INF/pages/");
resolver.setSuffix (".jsp");
resolver.setViewClass (JstlView.class);
return resolver;
}
ref-link http://habrahabr.ru/post/226663/
#Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/WEB-INF/pages/");
bean.setSuffix(".jsp");
return bean;
}
ref-link https://samerabdelkafi.wordpress.com/2014/08/03/spring-mvc-full-java-based-config/
#Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
List< ViewResolver > resolvers = new ArrayList< ViewResolver >();
InternalResourceViewResolver r1 = new InternalResourceViewResolver();
r1.setPrefix("/WEB-INF/pages/");
r1.setSuffix(".jsp");
r1.setViewClass(JstlView.class);
resolvers.add(r1);
JsonViewResolver r2 = new JsonViewResolver();
resolvers.add(r2);
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setViewResolvers(resolvers);
resolver.setContentNegotiationManager(manager);
return resolver;
}
ref-link http://fruzenshtein.com/spring-java-configurations/
The answer is it depends on your requirements.
setupViewResolver and jspViewResolver do the same as long as JSTL is available on the classpath:
public InternalResourceViewResolver() {
Class<?> viewClass = requiredViewClass();
if (viewClass.equals(InternalResourceView.class) && jstlPresent) {
viewClass = JstlView.class;
}
setViewClass(viewClass);
}
Whereas contentNegotiatingViewResolver shows a more complex example where you want to render the response depending on the request content type.
If you need to register just a specific view resolver you can shorten registration to a simple one liner in Spring 4.1:
#EnableWebMvc
#Configuration
public class HystrixMvcConfiguration extends WebMvcConfigurerAdapter {
#Override
public void configureViewResolvers(final ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
}
ViewResolverRegistry supports builder methods for Velocity, Freemarker, Groovy templates and Tiles out of the box.

Root Context path in spring application

I am running application using tomcat server, when server start i will get url
http://localhost:8080/TestApp/
and displaying index.jsp file but when i click link in index file it is displaying url like
http://localhost:8080/testsuccess
but it should display like
http://localhost:8080/TestApp/testsuccess
can any please help me to solve this.
SpringConfiguration.java
#Configuration
#EnableWebMvc
#ComponentScan("com.testapp")
#EnableTransactionManagement
public class SpringConfiguration {
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
SpringWebAppInitializer.java
public class SpringWebAppInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(SpringConfiguration.class);
ServletRegistration.Dynamic dispatcher = container.addServlet(
"SpringDispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
MyController.java
#Controller
public class MyFirstController
{
#RequestMapping(value = "/" , method = RequestMethod.GET)
public String testApp() throws Exception{
return "index";
}
#RequestMapping(value = "/testsuccess", method = RequestMethod.GET)
public String testAppSuccess() {
Map<String, Object> model = new HashMap<String, Object>();
return "success";
}
}
got it i should use
Next
it will give the context path.
I think your problem comes from the link inside your index.jsp. They might look like ... You should use either jstl or spring tag lib to handle links / urls in your pages. They both have the ability to prepend the deployment / context path of your application.
jstl example:
with taglib xmlns:c="http://java.sun.com/jsp/jstl/core" included you can create an anchor like ..
spring example:
with taglib xmlns:spring="http://www.springframework.org/tags" your link will be created in two steps:
<spring:url value="/testsuccess" var="myurl" htmlEscape="true"/>
...
Update: Wrong function name in jstl taglib version.

ResourceBundle not found for MessageSource when placed inside a folder

I am trying to use resource bundles with Spring's Message Source. Here is the way I am doing it:
#Component
public class MessageResolver implements MessageSourceAware {
#Autowired
private MessageSource messageSource;
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
public String getMessage(){
return messageSource.getMessage("user.welcome", new Object[]{"Rama"} , Locale.US);
}
}
And here is my folder structure:
messages_en_US.properties contains just one line:
user.welcome=Welcome {0}
Here is the xml configuration used:
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>resourcebundles/messages</value>
</property>
</bean>
Here is the error I am getting:
WARNING: ResourceBundle [resourcebundles/messages] not found for MessageSource: Can't find bundle for base name resourcebundles/messages, locale en_US
Exception in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'user.welcome' for locale 'en_US'.
But if I move my resource bundle to directly under the resources folder, it is working fine. In this case, here is the xml configuration I am using:
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
Is is that if I have to use ResourceBundleMessageSource, I should put my resource bundles directly under the resources? If i have to keep it in specified folder only, is there any other way to get this one work?
Thanks!
boy, maybe you can change the xml configuration as follows:
use
org.springframework.context.support.ReloadableResourceBundleMessageSource
instead of
org.springframework.context.support.ResourceBundleMessageSource
all configuration like this:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:resourcebundles/messages" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
Change your configuration to the following for messageSource bean in your xml file.
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>classpath*:resourcebundles/messages</value>
</property>
</bean>
Since all your properties files are in classpath of java you need to define the path with prefix classpath*: otherwise it will look into the web directory of your application.
Hope this helps you. Cheers.
It's nearly 2015 now and I'm using Spring 4.1.2.RELEASE and there's definitely a problem with the way the messageSource bean needs to be configured so it picks up the target resource bundle.
1) If the messageSource bean is of type ReloadableResourceBundleMessageSource it won't work:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
#Configuration
#ComponentScan(basePackages = { "com.intertech.service" })
//#ImportResource({"classpath:spring/applicationContext-i18n.xml"})
public class AppConfig {
#Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource getMessageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("config/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
// #Bean(name = "messageSource")
// public ResourceBundleMessageSource getMessageSource() {
// ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
// messageSource.setBasename("config/messages");
// messageSource.setDefaultEncoding("UTF-8");
// messageSource.setUseCodeAsDefaultMessage(true);
// return messageSource;
// }
}
2) If the messageSource bean is of type ResourceBundleMessageSource it will work:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
#Configuration
#ComponentScan(basePackages = { "com.intertech.service" })
//#ImportResource({"classpath:spring/applicationContext-i18n.xml"})
public class AppConfig {
// #Bean(name = "messageSource")
// public ReloadableResourceBundleMessageSource getMessageSource() {
// ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
// messageSource.setBasename("config/messages");
// messageSource.setDefaultEncoding("UTF-8");
// messageSource.setUseCodeAsDefaultMessage(true);
// return messageSource;
// }
#Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("config/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
}
3) If you're using an XML configuration file combined with a configuration class - it will work (notice how the base bundle is configured in a class like qualification manner i.e. 'config.messages' not 'config/messages'): (applicationContext-i18n.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="config.messages"
p:useCodeAsDefaultMessage="true"/>
<!-- This will not work -->
<!--
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="config/messages"
p:useCodeAsDefaultMessage="true"/>
-->
</beans>
and:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
#Configuration
#ComponentScan(basePackages = { "com.intertech.service" })
#ImportResource({"classpath:spring/applicationContext-i18n.xml"})
public class AppConfig {
// #Bean(name = "messageSource")
// public ReloadableResourceBundleMessageSource getMessageSource() {
// ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
// messageSource.setBasename("config/messages");
// messageSource.setDefaultEncoding("UTF-8");
// messageSource.setUseCodeAsDefaultMessage(true);
// return messageSource;
// }
// #Bean(name = "messageSource")
// public ResourceBundleMessageSource getMessageSource() {
// ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
// messageSource.setBasename("config/messages");
// messageSource.setDefaultEncoding("UTF-8");
// messageSource.setUseCodeAsDefaultMessage(true);
// return messageSource;
// }
}
4) Most importantly... if you're using a WebApplicationInitializer (no web.xml), you've got to register the configuration class that defines the 'messageSource' bean in the root context, not in the dispatcher servlet's context:
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebAppInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
dispatcherServlet.register(MvcConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(
dispatcherServlet));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.htm");
}
}
In my case, using Spring 4.3.2.RELEASE and java config and a ReloadableResourceBundleMessageSource, I had to define my template engine as a bean otherwise my messages were not getting resolved.
Here's a sample of a working configuration.
AppConfig.java
import java.util.concurrent.TimeUnit;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
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.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;
#Configuration
#EnableWebMvc
#ComponentScan("myapp")
public class AppConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
private static final boolean CACHE_THYMELEAF_TEMPLATES = false;
private final String UTF8_ENCODING = "UTF-8";
#Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
#Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding(UTF8_ENCODING);
resolver.setCache(CACHE_THYMELEAF_TEMPLATES);
return resolver;
}
#Bean
public TemplateEngine templateEngine() {
//this method must be defined as a bean otherwise i18n messages are not found
//if method defined as private TemplateEngine templateEngine() messages are not found
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.addTemplateResolver(templateResolver());
return engine;
}
private ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/thymeleaf/");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setSuffix(".html");
resolver.setCacheable(CACHE_THYMELEAF_TEMPLATES);
resolver.setCharacterEncoding(UTF8_ENCODING);
return resolver;
}
#Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("WEB-INF/i18n/messages");
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding(UTF8_ENCODING);
messageSource.setFallbackToSystemLocale(false);
messageSource.setCacheSeconds((int)TimeUnit.HOURS.toSeconds(1));
return messageSource;
}
}
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="resourcebundles/messages" />
</bean>
You have to configure your messages path as shown above.
Also, check class name.
Is is that if I have to use ResourceBundleMessageSource, I should put
my resource bundles directly under the resources? If i have to keep it
in specified folder only, is there any other way to get this one work?
You can define your messages in your own package, it does not need to be located in the resources folder.
Using Spring 5.2.2.RELEASE versioned components, this is the way I managed to make it work:
The qualified name of the file would be:
/tutproject/src/com/tutproject/app/messages/messages.properties
The bean is defined like this in the my Spring Bean Configuration File (XML):
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"
value="/com/tutproject/app/messages/messages">
</property>
</bean>
The Java build path includes tutproject/src , this is the part of the location omitted in the XML definition.
Some additional useful information from the ResourceBundleMessageSource class:
The basenames follow {#link java.util.ResourceBundle}
conventions: essentially, * a fully-qualified classpath location. If
it doesn't contain a package qualifier * (such as {#code
org.mypackage}), it will be resolved from the classpath root. * Note
that the JDK's standard ResourceBundle treats dots as package
separators: * This means that "test.theme" is effectively equivalent
to "test/theme".
I have used following configuration and it is working fine
<beans:bean id="messageSource class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basename" value="classpath:resourcebundles/ScreenLabelResources" />
</beans:bean>
What worked for me was something really simple.
It was
<property name="basename">
<value>locale\messages</value>
</property>
I changed it to
<property name="basename">
<value>locale/messages</value>
</property>
Just a \ to / change fixed it for me. I am using a MAC.
I have not tried *classpath, that may not have worked for me.
I have used following configuration and it is working fine in my project.
My messages.properties is in below path:
..\WebContent\WEB-INF\resources
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:WEB-INF/resources/messages" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
I have used following configuration and it is working fine in my project.
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:configurations/messages" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
location: src\main\resources\configurations\messages_en.properties
YAML version for this
spring:
messages:
basename: i18n/validation, i18n/message # for multiple properties just use comma separated values
encoding: UTF-8
You can refer to documentation to see full description.
Also I should mention that the default MessageSource bean is a ResourceBundleMessageSource which is already reading form a classpath so there is no need to use nonation like classpath:i18n/validation.
Directory structure
In Spring Boot 2.2.5 things have slightly changed. Classpath is not needed anymore.
#Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
I strongly suggest to keep property files out side of project so that we don't need to compile code for every property change.
Below configuration we are using in live project. setting property.location value in application.properties file
#Configuration
public class LocalizationConfiguration {
private static final Logger logger = LoggerFactory.getLogger(LocalizationConfiguration.class);
#Value("${property.location}")
private String propertyLocation;
#Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.ENGLISH); // change this
return localeResolver;
}
#Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
String messageFolderPath = propertyLocation + "/" + "i18n";
resource.setBasename("file:"+messageFolderPath+"/messages");
resource.setDefaultEncoding("UTF-8");
resource.setCacheSeconds(10);
return resource;
}
#Bean
public LocalValidatorFactoryBean validatorFactoryBean() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource());
return bean;
}
}

Resources