Spring 消息本地化实现(使用注释实现)/使用自定义消息

来源:互联网 发布:日本阶级固化 知乎 编辑:程序博客网 时间:2024/06/08 05:01

配置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)@EnableWebMvcpublic 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

0 0
原创粉丝点击