Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1445932
  • 博文数量: 254
  • 博客积分: 8696
  • 博客等级: 中将
  • 技术积分: 2961
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-03 16:46
文章分类

全部博文(254)

文章存档

2015年(4)

2014年(18)

2013年(16)

2012年(8)

2011年(25)

2010年(2)

2009年(74)

2008年(107)

分类: 系统运维

2009-03-01 13:52:19

一、国际化(I18N)
默认资源文件:messages.properties
美国英语:messages_en_US.properties
ApplicationContext装配javabean时,检测ID为messageSource的javabean(MessageSource接口的实现)。MessageSource接口定义:
package org.springframework.context;
import java.util.Locale;
public interface MessageSource
{
    String getMessage(String code, Object[] args, String defaultMessage,
                      Locale locale);
    String getMessage(String code, Object[] args, Locale locale);
    String getMessage(MessageSourceResolvable resolvable, Locale locale);
}
getMessage()方法用于获得消息资源。ApplicationContext实现了MessageSource接口,可以直接调用容器实现的接口方法。

二、实例
资源文件:
messages.properties简体中文
LoginFrame.loginName=\u7528\u6237\u540D\uFF1A
LoginFrame.loginPass=\u5BC6\u7801\uFF1A
LoginFrame.login=\u767B\u9646
LoginFrame.exit=\u9000\u51FA

messages_en_US.properties美国英语
LoginFrame.loginName=User Name
LoginFrame.loginPass=Password
LoginFrame.login=Login
LoginFrame.exit=Exit

Spring配置文件applicationContext.xml:



            class="org.springframework.context.support.ResourceBundleMessageSource">
       
            messages
       

   



JSP页面:
<%@ page language="java" contentType="text/html" pageEncoding="GBK" %>




   
        后台管理
       
       
   
   
    <%
        ApplicationContext context =
            new ClassPathXmlApplicationContext("applicationContext.xml");
        Locale locale = Locale.US;
     %>
     






     

     
     
        
     
     
        
     
     
 

        
        
            
        
        
            
        
        
            
        
        
 
             <%=context.getMessage("LoginFrame.loginName", null, locale)%>
            
            
   
             <%=context.getMessage("LoginFrame.loginPass", null, locale)%>
            
            

             "> 
             "> 
            

        

     

   
   

参考资料:《JSP技术开发大全》
阅读(1521) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~