zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    hdu 2527:Safe Or Unsafe(数据结构,哈夫曼树,求WPL)
    hdu 2019:数列有序!(数据结构,直接插入排序+折半插入排序)
    hdu 3791:二叉搜索树(数据结构,二叉搜索树 BST)
    hdu 3336:Count the string(数据结构,串,KMP算法)
    hdu 1022:Train Problem I(数据结构,栈,递归,dfs)
    hdu 2141:Can you find it?(数据结构,二分查找)
    hdu 1232:畅通工程(数据结构,树,并查集)
    hdu 2025:查找最大元素(水题,顺序查找)
    hdu 2857:Mirror and Light(计算几何,点关于直线的对称点,求两线段交点坐标)
    hdu 1174:爆头(计算几何,三维叉积求点到线的距离)
  • 原文地址:https://www.cnblogs.com/shamgod/p/5243678.html
Copyright © 2011-2022 走看看