这是我的项目结构:
这是Spring root config:
<?php
$a = 10;
$b = 'String';
echo '$a is '.$a;
echo '....';
echo '$b is '.$b;
parse_str("a=$b&b=$a");
echo '....After Using Parse Str....';
echo '$a is '.$a;
echo '....';
echo '$b is '.$b;
?>
但是当使用@EnableWebMvc
@Configuration
@ComponentScan({ "com.rgh.*" })
@EnableTransactionManagement
@Import({ SpringSecurityConfig.class })
public class SpringWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
}
// Start localization configs
@Bean
public MessageSource messageSource() {
final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setDefaultLocale(new Locale("fa"));
return localeResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
// End localization configs
@Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
builder.scanPackages("com.rgh.*.model").addProperties(getHibernateProperties());
return builder.buildSessionFactory();
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.format.sql", "false");
properties.put("hibernate.show.sql", "true");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.put("hibernate.hbm2ddl.auto", "update");
return properties;
}
@Bean(name = "dataSource")
public BasicDataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/framework");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
@Bean
public HibernateTransactionManager txManager() {
return new HibernateTransactionManager(sessionFactory());
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
启动应用程序时,spring无法识别设置为localhost:8080
的默认语言环境并使用localeResolver.setDefaultLocale(new Locale("fa"));
语言环境加载页面,并且当我使用{调用页面时{1}}它再次加载en_US
区域设置的网页。
感谢您的任何想法。
更新
localhost:8080/?lang=de
UPDATE2
这些是我的新配置,默认默认区域设置为en_US
且public class SpringWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringWebConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
文件未加载,我应该打开登录页面,输入无效的用户名和密码然后按提交,然后将加载en_US
并将区域设置更改为messages
!!!
messsages_fa.properties
答案 0 :(得分:2)
问题是您在根上下文中加载了所有内容,并且没有检测到某些bean。接下来,您应该真正拆分您的配置。创建Web,安全性和持久性配置,并在调度程序servlet的根配置和Web中加载安全性和持久性。
@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.rgh"}, excludeFilters = {
@Filter(org.springframework.stereotype.Controller.class)
})
public class PersistenceConfiguration {
// Hibernate, transaction manager, datasource and messageSource go here
}
然后是网络配置
@EnableWebMvc
@Configuration
@ComponentScan({ "com.rgh"}, includeFilters = {
@Filter(org.springframework.stereotype.Controller.class)
}, useDefaultFilters=false )
public class SpringWebConfig extends WebMvcConfigurerAdapter {
/// web related things like view resolvers, controller, interceptors go here
}
然后在你的bootstrap类中加载正确的配置类。
public class SpringWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringWebSecurity.class, PersistenceConfiguration.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { SpringWebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
现在您已拆分配置,所有内容都由正确的加载。组件扫描中的includeFilter
和excludeFilter
以及useDefaultFilters
属性在此非常重要,可避免重复bean。如果你不是所有的bean都会被root一次加载一次,而一次由dispatcherservlet加载。