Spring温习(8)--国际化的支持
我今天介绍的国际化呢,平时基本上都用不上,我的项目中也从来没用过,就记得当初学的时候有这么一部分,就回顾比较浅显的一部分了,以备今后使用
首先呢,必须要在Spring配置文件中配置这么一段
Xml代码
<span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- Spring 通过ApplicationContext 容器的MessageSource 接口实现Sping 对国际化的支持。 这里的Bean id 必须是“messageSource”,因为Spring 在装配系统Bean 时会根据这个名字进行查找。 这样,我们便可以在程序中通过ApplicationContext 对象调用getMessage()方法获取资源文件的信息。 --> <bean id="messageSource" abstract="false" lazy-init="default" autowire="default" dependency-check="default"> <property name="basenames"> <list> <value>config/properties</value> </list> </property> </bean> </beans></span>

<span style="font-size: medium;">package com.javacrazyer.test; import java.util.Date; import java.util.Locale; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class CommonTest { @Test public void test1() { String path = "WebRoot/WEB-INF/applicationContext.xml"; ApplicationContext context = new FileSystemXmlApplicationContext(path); Object[] objs = new Object[] { "javacrazyer", new Date().toLocaleString() }; // www.eimhe.com为资源文件的key值,objs为资源文件value值所需要的参数,Local.CHINA为国际化为什么语言 String str = context.getMessage("www.iteye.com", objs, Locale.CHINA); System.out.println(str); } } </span> <span style="font-size: medium;"><bean id="localeResolver" name="code"><span style="font-size: medium;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="spring" uri="WEB-INF/lib/spring.tld"%> <html> <head> <title>Spring国际化</title> </head> <body> <spring:message code="www.iteye.com" /><br> <input type="button" value="<spring:message code="www.iteye.com" />"/><br> </body> </html> </span>

<span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Spring ApplicationContext Definition --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> </span>