Spring MVC 對locale和theme的支持


Locale 
Spring MVC缺省使用AcceptHeaderLocaleResolver來根據request header中的 Accept-Language 來確定訪客的local。對於前端jsp頁面上,spring提供了標簽<spring:message>來提供從resource文件中獲取的文字的動態加載功能。 
例如 
修改servlet context xml文件中的messageSource部分,增加對多國語言message的code resource的引入。 
Xml代碼    收藏代碼
  1. <bean id="messageSource"  
  2.     class="org.springframework.context.support.ReloadableResourceBundleMessageSource"  
  3.     p:fallbackToSystemLocale="true" p:useCodeAsDefaultMessage="false"  
  4.     p:defaultEncoding="UTF-8">  
  5.     <description>Base message source to handle internationalization  
  6.     </description>  
  7.     <property name="basenames">  
  8.         <list>  
  9.             <!-- main resources -->  
  10.             <value>classpath:valid/validation</value>  
  11.             [color=red]<value>classpath:local/message</value>[/color]  
  12.         </list>  
  13.     </property>  
  14. </bean>  


在 src/main/resources目錄下增加local目錄,並在其下增加messpage_zh_CN.properties文件,內容為 hello=\u6b22\u8fce,並增加message_en.properties文件內容為hello=welcome。 

修改hellworld.jsp,增加如下代碼 

Jsp代碼    收藏代碼
  1. <h1>[color=red]<spring:message code='hello'/>[/color]</h1>  


此時訪問http://localhost:8080/mvc,根據你的客戶端的不同,將分別顯示中文和英文的歡迎語。 

除缺省的AcceptHeaderLocaleResolver外,spring mvc還提供了CookieLocaleResolver和SessionLocaleResolver兩個localResolver來提供在運行時由客戶端強行指定local的功能。 
分別使用cookie和session中存儲的locale值來指定當前系統所使用的locale. 

以SessionLocaleResolver為例,在servlet context xml配置文件中增加如下配置 
Xml代碼    收藏代碼
  1. <bean id="localeResolver"  
  2.     class="org.springframework.web.servlet.i18n.SessionLocaleResolver">  
  3. </bean>     


新增一個controller,來提供更換locale功能。 
Java代碼    收藏代碼
  1. @Controller  
  2. public class LocalChange {  
  3.   
  4.     @Autowired  
  5.     private LocaleResolver localeResolver;  
  6.       
  7.     @RequestMapping("/changeLocale")  
  8.     public String changeLocal(String locale,  
  9.             HttpServletRequest request,  
  10.             HttpServletResponse response){  
  11.         Locale l = new Locale(locale);  
  12.         localeResolver.setLocale(request, response, l);  
  13.         return "redirect:helloworld";  
  14.     }   
  15. }  


可分別訪問http://localhost:8080/springmvc/changeLocale?locale=en 
http://localhost:8080/springmvc/changeLocale?locale=zh_CN 
來查看更換語言后的結果。 

除以以上方式來變更樣式外,spring mvc還提供了一個 LocaleChangeInterceptor攔截器來在request時根據request 參數中的locale參數的內容來實時變更Locale。 
示例代碼如下 
在servlet context xml配置文件中新增攔截器, 
Xml代碼    收藏代碼
  1. <mvc:interceptor>  
  2.   <mvc:mapping path="/*" />  
  3.   <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />  
  4. </mvc:interceptor>  


此時訪問 http://localhost:8080/springmvc/helloworld?locale=en ,可以查看動態更換locale后的效果。 



Theme 
Spring MVC中通過ThemeSource接口來提供對動態更換樣式的支持,並提供了ResourceBundleThemeSource這個具體實現類來提供通過properties配置文件對theme中的樣式的配置 
例如配置文件中 內容為 helloworld=theme/default/css/helloworld.css 
而jsp文件中使用 
<link rel="stylesheet" type="text/css" 
href=" <spring:theme code='helloworld'/> " /> 
來引用對helloworld這個樣式文件的引入。由此來實現樣式文件的動態引用,從而使spring mvc中可以實現換膚功能。 
如果說ThemeSource接口是提供了如何去取當前的theme的code與實際內容的mapping關系,那么spring mvc提供的另外一個interface ThemeResolver則是提供了如何去設置當前使用的theme的手段。 
  Spring MVC提供了三個ThemeReslover的實現類,分別是 
FixedThemeResolver :固定格式的theme,不能在系統運行時動態更改theme. 
SessionThemeResolver :theme name存放在session中key值為 org.springframework.web.servlet.theme.SessionThemeResolver.THEME 的session attribute中。可在運行中通過更改session中的相應的key值來動態調整theme的值。 
CookieThemeResolver :theme name存放在cookie中key值為 org.springframework.web.servlet.theme.CookieThemeResolver.THEME 中。可在運行中通過更改cookie中的相應的key值來動態調整theme的值。 
以上Themesource和ThemeResolver在servlet context xml中的配置示例如下 

Xml代碼    收藏代碼
  1. <bean  
  2.     class="org.springframework.ui.context.support.ResourceBundleThemeSource"  
  3.     id="themeSource">  
  4.     <property name="basenamePrefix" value="theme."></property>  
  5. </bean>  
  6.   
  7.     <bean id="themeResolver"  
  8.     class="org.springframework.web.servlet.theme.SessionThemeResolver">  
  9.     <property name="defaultThemeName" value="grey" />  
  10. </bean>  

從以上配置可以看出,我們使用了一個sessionThemeReslover(bean name 必須為themeReslover,因為這個值是hardcode在DispatcherServlet中的),缺省的themeName為grey。 
而ThemeSource中我們的配置的basenamePrefix為”theme.”,這里表示spring mvc將從classes/theme/目錄下對應的themename.properties中讀取配置,例如我們這里配置的是grey,則將從classes/theme/grey.properties中讀取theme code的配置。 
下面我們將新建3個theme,分別為default,red和blue,存放目錄如下。 
 

並修改helloworld.jsp,按前面所介紹的,增加對換膚功能的支持。 

Jsp代碼    收藏代碼
  1. <link rel="stylesheet" type="text/css"  
  2.     href="<spring:theme code='helloworld'/>" />  
  3. </head>  
  4. <body>  
  5.     <h1>Hello World!</h1>  
  6.     [color=red]<div id="divTheme"></div>[/color]  

這里新增一個div來展示換膚前后的效果 

新增一個controller,來提供換膚功能。 

Java代碼    收藏代碼
  1. @Controller  
  2. public class ThemeChange {  
  3.     private final Log logger = LogFactory.getLog(getClass());  
  4.       
  5.     @Autowired  
  6.     private ThemeResolver themeResolver;  
  7.   
  8.     @RequestMapping("/changeTheme")  
  9.     public void changeTheme(HttpServletRequest request,  
  10.             HttpServletResponse response, String themeName) {  
  11.         logger.info("current theme is " + themeResolver.resolveThemeName(request));  
  12.         themeResolver.setThemeName(request, response, themeName);  
  13.         logger.info("current theme change to " + themeResolver.resolveThemeName(request));  
  14.     }  
  15. }  


可訪問 http://localhost:8080/springmvc/ 看到一個缺省的灰色的div層, 
訪問 localhost:8080/springmvc/changeTheme?themeName=red 后,刷新頁面,div的樣式將發生變化 

除以以上方式來變更樣式外,spring mvc還提供了一個 ThemeChangeInterceptor 攔截器來在request時根據request 參數中的theme的內容來動態變更樣式。 
實例代碼如下 
在servlet context xml配置文件中新增攔截器, 
Xml代碼    收藏代碼
  1. <mvc:interceptor>  
  2.   <mvc:mapping path="/*" />  
  3.   <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />  
  4. </mvc:interceptor>  


此時訪問 http://localhost:8080/springmvc/?theme=blue ,可以查看動態換膚后的效果。 


免責聲明!

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



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