form表单

来源:互联网 发布:非关系型数据库例子 编辑:程序博客网 时间:2024/04/28 08:33

    首先,建立一个描述message的XML文件,名为messages.xml
    
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!-- 资源国际化测试 -->
    <bean id="messageSource"        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">        <property name="basenames">
            <list>
                <value>org/rjstudio/spring/properties/messages</value>
            </list>
        </property>
    </bean>
</beans>
    
    这个Bean的id是定死的,只能为“messageSource”。这里的Class需要填入MessageSource接口的实现。其中,在我看的书中只提及了两个类,一个是:ResourceBundleMessageSource,另一个则是ReloadableResourceBundleMessageSource。其中,后者提供了无需重启就可重新加载新配置的特性。
    
    list节点的value子节点中的body值“org/rjstudio/spring/properties/messages”,是指org.rjstudio.spring.proerties包下的以messages为主要名称的properties文件。比如说,以Locale为zh_CN为例,Spring会自动在类路径中在org.rjstudio.spring.properties包下按照如下顺序搜寻配置文件并进行加载:
    
    
    
    接下来,让我们在org.rjstudio.spring.properties下,建立两个messages的属性文件。一个名为messages_zh_CN.properties,另一个为messages_en_US.properties,分别对应国际化中的中国和美国。
    
    在这两个属性文件中分别建立一个userinfo属性。
        中国为:userinfo=当前登陆用户[{0}] 登陆时间[{1}]
        美国为:userinfo=current login user:[{0}] login time:[{1}]
    
    好了,一切就绪,接下来可以写段代码来测试了。。建个类,写个测试Main方法。
    
    
    
    
    public class MessageTest {
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("messages.xml");
            Object[] arg = new Object[] { "Erica", Calendar.getInstance().getTime() };
            String msg = ctx.getMessage("userinfo", arg,Locale.CHINA);
            System.out.println("Message is ===> " + msg);
        }
    }
    
    最后输出的结果是:Message is ===> 当前登录用户:[Erica] 登录时间:[07-6-8 上午10:20]
    
    ctx.getMessage("userinfo", arg,Locale.getDefault());这个方法,传入的三个参数,第一个是properties文件中对应的名。arg为一个对象数组,我们在properties里面放置了两个变量,[{0}]和[{1}],Spring会为我们给它们赋值。而最后则需要传入一个Local。这里用 Locale.CHINA代表中国。如果我们用Locale.US,则输出会变为:
    
    Message is ===> current login user:[Erica] login time:[6/8/07 10:59 AM]
    
    OK,到这里,就到这里。
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
Spring的国际化(原创)

1:在MyEclipse下面创建一个test的Web  Project,然后添加Spring相关的文件,在src根目录下创建applicationContext.xml文件。

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
 
       <bean id="messageSource"  class="org.springframework.context.support.ResourceBundleMessageSource">
 
      <property name="basename" value="messages"/>

      </bean>
 
       <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
   
</beans>
 


2:在src根目录下面创建4个资源文件:分别是

messages_zh.properties
main.title=你好

messages_en.properties
main.title=Hello World!

messages_ja.properties
main.title=こんにちは

messages_ko.properties
main.title=안녕하십니까

3:在WebRoot根目录下面创建test.jsp

test.jsp

<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="WEB-INF/lib/spring.tld"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Spring国际化</title>
  </head>
  <body>
 
    <spring:message code="main.title" /><br>

    <input type="button" value="<spring:message code="main.title" />"/><br>

  </body>

</html>

4:修改WEB-INF下面的web.xml

在web.xml加入
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath*:/applicationContext*,classpath*:META-INF/applicationContext*.xml
  </param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

这样用Spring国际化的Test.jsp页面就做好了:),此种方法是自动默认当前用户的语言,比如客户端是日语系统,就自动寻找messages_ja.properties资源文件,是英语系统,就自动寻找messages_en.properties资源文件。

 

===================================

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.     <description>Spring Quick Start</description>  
  6.   
  7.     <bean id="messageSource"  
  8.         class="org.springframework.context.support.ResourceBundleMessageSource">  
  9.         <property name="basenames">  
  10.             <list>  
  11.                 <value>messages</value>  
  12.             </list>  
  13.         </property>  
  14.     </bean>  
  15.   
  16. </beans>  

使用示例: 
ApplicationContext appContext = new FileSystemXmlApplicationContext("bean.xml"); 
Object[] args = new Object[]{"xiaocao000", Calendar.getInstance().getTime()}; 
String i18n = appContext.getMessage("welcomeinfo",args,Locale.getDefault()); 

 

原创粉丝点击