form表单

    技术2024-07-03  66

        首先,建立一个描述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.propertiesmain.title=你好messages_en.propertiesmain.title=Hello World!messages_ja.propertiesmain.title=こんにちはmessages_ko.propertiesmain.title=안녕하십니까3:在WebRoot根目录下面创建test.jsptest.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资源文件。

     

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

     

    <?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">      <description>Spring Quick Start</description>        <bean id="messageSource"          class="org.springframework.context.support.ResourceBundleMessageSource">          <property name="basenames">              <list>                  <value>messages</value>              </list>          </property>      </bean>    </beans>   使用示例:  ApplicationContext appContext = new FileSystemXmlApplicationContext("bean.xml");  Object[] args = new Object[]{"xiaocao000", Calendar.getInstance().getTime()};  String i18n = appContext.getMessage("welcomeinfo",args,Locale.getDefault()); 

     

    最新回复(0)