springMVC 国际化配置

1.配置DispatcherServlet-servlet.xml

<?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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
	
	
	<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.hetaoo.controller"></context:component-scan>
	
	<!-- 配置国际化资源文件 -->
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<!-- 上面标红地方导致我出错 -->

		<property name="basename" value="i18n"></property>
	</bean>
		
	<!-- 配置视图解析器 :如何把handler方法返回值解析为实际的物理视图-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 配置国际化资源文件 
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="i18n"></property>	
	</bean>-->
	
</beans>

 2.写i18n.properties配置文件

i18n.username=Username
i18n.password=Password

    i18n_zh_CN.properties文件

i18n.username=\u7528\u6237\u540D
i18n.password=\u5BC6\u7801

  i18n_en_US.properties

i18n.username=Username
i18n.password=Password

 3.编写jsp页面

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!--没有该标签是没有jstl jar包 -->    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<fmt:message key="i18n.username"></fmt:message>
	<br><br>
	
	<fmt:message key="i18n.password"></fmt:message>
	<br><br>
</body>
</html>

 4.随便编写一个controller返回到上述jsp页面即可

猜你喜欢

转载自hetaoo.iteye.com/blog/2290254