Im using thymeleaf and spring, my messages.properties files need to have the same name as the template in order for them to work. I have tried to create a custom path using webConfigurer.Java but it's not working.
WebConfigurer.java source:
/*
* Copyright 2015 Information Retrieval programs (IRP).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://ift.tt/jtTJvY
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package en.irp.project.config;
import java.util.Locale;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
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.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
/**
* Specify session factory, hibernate properties, data source, hibernate
* transaction, view resolver.
*
* The ComponentScan({ "com.app.skeleton.*" }) annotation means exactly the same
* as xml -> context:component-scan base-package="com.*" in
* dispatcher-servlet.xml
*
* @author Michael Beers <michael@michaelbeers.nl>
* @todo Fix the languages resolver.
*/
@EnableWebMvc
@EnableTransactionManagement
@Configuration
@ComponentScan({"en.irp.project.*"})
@PropertySource("classpath:/application.properties")
@Import({SecurityConfigurer.class})
public class WebConfigurer extends WebMvcConfigurerAdapter {
/**
* Loads the environment to load properties.
*/
@Autowired
Environment env;
/**
* Generates the JDBC data source for the application.
*
* @return
*/
@Bean(name = "dataSource")
public BasicDataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(env.getProperty("database.driver"));
ds.setUrl(env.getProperty("database.url"));
ds.setUsername(env.getProperty("database.user"));
ds.setPassword(env.getProperty("database.password"));
return ds;
}
/**
* Generates the Hibernate session factory.
*
* @return
*/
@Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
builder.scanPackages("en.irp.project.model").addProperties(getHibernateProperties());
//builder.scanPackages("com.app.skeleton.model").addProperties(getHibernateProperties());
return builder.buildSessionFactory();
}
/**
* Returns all the hibernate properties from the application.properties
* file.
*
* @return
*/
private Properties getHibernateProperties() {
Properties prop = new Properties();
prop.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
prop.put("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
prop.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
prop.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
return prop;
}
/**
* Generates the Hibernate transaction manager.
*
* @return
*/
@Bean
public HibernateTransactionManager txManager() {
return new HibernateTransactionManager(sessionFactory());
}
/**
* Generates the i18n language loader.
*
* @return
*/
@Bean(name="messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
resource.setBasename("WEB-INF/languages/messages");
resource.setDefaultEncoding("UTF-8");
return resource;
}
/**
* Generates the i18n language changer parameter.
*
* @return
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
/**
* Genereates the default i18n language.
*
* @return
*/
@Bean(name = "localeResolver")
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("en"));
return localeResolver;
}
/**
* Generates the template resolver for thymeleaf.
*
* @return
*/
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setCacheable(false);
return resolver;
}
/**
* Generates the template engine for thymeleaf.
*
* @return
*/
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
engine.addDialect(new SpringSecurityDialect());
return engine;
}
/**
* Generates the view resolver from Spring MVC with thymeleaf intergrated.
*
* @return
*/
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(1);
viewResolver.setViewNames(new String[]{"*"});
viewResolver.setCache(false);
return viewResolver;
}
/**
* Add resources.
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("/WEB-INF/assets/");
}
/**
* Add interceptors.
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
Aucun commentaire:
Enregistrer un commentaire