配置ResourceBundleMessageSource的Bean,在AppConfig中添加messageSource方法:
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.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan("com")
@Import(SecurityConfig.class)
@EnableWebMvc
public class AppConfig {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/secure/");
resolver.setSuffix(".jsp");
return resolver;
}
<span style="color:#FF6666;"> @Bean
public ResourceBundleMessageSource messageSource(){
ResourceBundleMessageSource source=new ResourceBundleMessageSource();
source.setBasename("localization/messages");
source.setUseCodeAsDefaultMessage(true);
return source;
}</span>
}
localization/messages位于src/main/recources目录下:
message.properties:
login.failed_message=Login Error,try again!
在JSP文件中引用消息:
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %> <!--导入标签库-->
....
<spring:message code="login.failed_message"/>
...
代码:
https://github.com/angleBeibei/SpringSecurity