现在的位置: 首页 > 综合 > 正文

Spring MVC3.0国际化

2018年08月19日 ⁄ 综合 ⁄ 共 4160字 ⁄ 字号 评论关闭

Spring MVC3.0国际化
< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />
说明:在这里简单实现Spring MVC国际化。{这里用到注解,请参考Spring MVC注解Demo}
步骤:
① 创建一个demo的web工程,加入相应的jar包,配置web.xml文件,同样可以参照Spring MVC注解Demo;web.xml文件的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-val>/WEB-INF/config/spring/*.xml</param-val>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
说明:/WEB-INF/config/spring/*.xml表示在此路径下的所有已.xml的Spring配置文件都会在初始化时加载。
② 在/WEB-INF/config/spring路径下创建一个Spring-common.xml文件,配置Spring相关信息,代码如下:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/beans/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 控制器所在的包{添加注解的Controller} -->
<context:component-scan base-package="com.teana.controller" />
<!-- 国际化支持 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<val>messages</val>
</property>
</bean>

<!-- jsp文件的前缀和后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" val="/WEB-INF/jsp/" />
<property name="s?ix" val=".jsp" />
</bean>
</beans>
③ 在src目录下创建Controller,名称为TestController.java,包结构为com.teana.controller,其代码如下:
package com.teana.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ReqstMapping;
import org.springframework.web.bind.annotation.ReqstMethod;

@Controller
@ReqstMapping("/teana")
p lic class TestController
{
@ReqstMapping("/start/{name}/{age}")
p lic String start(@PathVariable("name") String name,
@PathVariable("age") int age)
{
System.out.println("姓名:" + name + ", 年龄:" + age);
return "start";
}
}
④ 在工程的src目录下创建国际化资源文件分别为messages.properties、messages_en_US.properties、messages_cn_ZH.properties文件,代码如下:
messages.properties的代码:
title=Spring MVC Demo{i18n}
welcome=Welcome to prod? professional DEMO S?SS are annotated MVC\!
messages_en_US.properties的代码:
title=(US)Spring MVC Demo{i18n}
welcome=(US)Welcome to prod? professional DEMO S?SS are annotated MVC\!
messages_cn_ZH.properties的代码:
title=Spring MVC Demo{\国\际\化}
welcome=\欢\迎\来\到SPRING MVC \注\MO S?SS\?01

⑤ 根据Spring-common.xml配置文件和TestController.java文件配置在WEB-INF/jsp/路径下配置start.jsp文件代码如下:
<%@ page lang ge="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 使用Spring标签 -->
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<!DOCTYPE HTML P LIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><spring:message code="title" /></title>
</head>
<body>
<center>
<br/><br/>
<h2 style="color:red;"><spring:message code="welcome" /></h2>
</center>
</body>
</html>
最后我们在浏览器输入:http://localhost:8080/demo/teana/start/linda/22.html 在不同的local环境下即可看到效果。
总结:
用Spring做国际化时经常会报:
org.springframework.context.NoS hMessageException: No message found under code 'title' for locale 'zh_CN'.
这样的错误。请注意以下几点:
① 新建资源文件时,尽量右击项目新建文件,来增加.
② 属性文件名的写法:
messages_zh_CN.properties (中文)
messages_en_US.properties (英文)
配置messageSource这个bean(注意:一定是messageSource不是messageResource ,这是Spring规定的)例如:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<val>messages</val>
</property>

</bean>

参考:http://www.zuidaima.com/share/1634244049275904.htm

转载于:http://mmz06.blog.163.com/blog/static/12141696201042953840924/

抱歉!评论已关闭.