SPRING IN ACTION 第4版筆記-第六章RENDERING WEB VIEWS-003- SPRING的GENERAL TAG LIBRARY簡介及用 和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=\u6B22\u8FCE

 

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.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM