SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-003- SPRING的GENERAL TAG LIBRARY简介及用<s:message>和ReloadableResourceBundleMessageSource实现国际化

一、 SPRING支持的GENERAL TAG LIBRARY

1.

二、用<s:message>和ReloadableResourceBundleMessageSource实现国际化

1.配置ReloadableResourceBundleMessageSource,它能it has the ability to reload message properties without recompiling or restarting the application.

 1 package spittr.web;
 2 
 3 import org.springframework.context.MessageSource;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.ComponentScan;
 6 import org.springframework.context.annotation.Configuration;
 7 import org.springframework.context.support.ReloadableResourceBundleMessageSource;
 8 import org.springframework.web.servlet.ViewResolver;
 9 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
10 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
11 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
12 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
13 import org.springframework.web.servlet.view.InternalResourceViewResolver;
14 
15 @Configuration
16 @EnableWebMvc
17 @ComponentScan("spittr.web")
18 public class WebConfig extends WebMvcConfigurerAdapter {
19 
20   @Bean
21   public ViewResolver viewResolver() {
22     InternalResourceViewResolver resolver = new InternalResourceViewResolver();
23     resolver.setPrefix("/WEB-INF/views/");
24     resolver.setSuffix(".jsp");
25     return resolver;
26   }
27   
28   @Override
29   public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
30     configurer.enable();
31   }
32   
33   @Override
34   public void addResourceHandlers(ResourceHandlerRegistry registry) {
35     // TODO Auto-generated method stub
36     super.addResourceHandlers(registry);
37   }
38   
39   @Bean
40   public MessageSource messageSource() {
41     ReloadableResourceBundleMessageSource messageSource = 
42         new ReloadableResourceBundleMessageSource();
43     //messageSource.setBasename("file:///Users/habuma/messages");
44     //messageSource.setBasename("messages");
45     messageSource.setBasename("classpath:messages");
46     messageSource.setCacheSeconds(10);
47     return messageSource;
48   }
49  
50 }

(1)路径有3种形式

application路径:没有前缀

classpath:"classpath:xxx"

系统路径:"file:///Users/habuma/messages"

(2)还有另一种MessageResource,要重载资源文件要重启应用

1 @Bean
2 public MessageSource messageSource() {
3     ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
4     messageSource.setBasename("messages");
5     return messageSource;
6 }

2.编写资源文件,放到合适的路径

(1)默认文件messages.properties

1 spitter.welcome=Welcome to Spitter!

(2)客户端语言是中文时messages_zh_CN.properties

spitter.welcome=u6B22u8FCE

3.view层显示

 1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 2 <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
 3 <%@ page session="false" %>
 4 <%@ page contentType="text/html;charset=utf-8" %>
 5 <html>
 6   <head>
 7     <title>Spitter</title>
 8     <link rel="stylesheet" 
 9           type="text/css" 
10           href="<c:url value="/resources/style.css" />" >
11   </head>
12   <body>
13     <s:message code="spitter.welcome" text="Welcome" />
14   </body>
15 </html>

4.

原文地址:https://www.cnblogs.com/shamgod/p/5243678.html