---
title: "Spring4MVC之国际化"
author: "ihuning"
date: "2015-09-15T21:18:23Z"
tags: []
categories: ["企业架构"]
slug: "spring4mvc"
draft: false
lastmod: "2015-09-15T21:18:23Z"
id: 2243731
url: "https://ihuning.iteye.com/blog/2243731"
---

Spring4MVC之国际化

 

1\. 添加jstljar包:standard.jar,jstl.jar

2\. 编写国际化资源文件:i18n.properties

 

 

```
i18n.username=USERNAME:
i18n.password=PASSWORD:
i18n.submit=SUMBIT
```

 

 

 

```
i18n.username=\u7528\u6237\u540D\uFF1A
i18n.password=\u5BC6\u7801\uFF1A
i18n.submit=\u767B\u5F55
```

 

 

3\. 在springmvc.xml文件中配置国际化资源文件

 

 

```
    <!--配置国际化资源文件-->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>    	
    </bean>
```

 

 

4\. 编写控制器类

 

 

```
package org.rabbitx.web.spring4mvc.i18n;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/i18n")
public class I18nController {

	@RequestMapping("/default-page")
	public String defaultPage()
	{
		return "i18n-default";
	}
	
}
```

 

 

5\. 编写jsp页面

    1) 引入fmt标签库:&lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%&gt;

    2) 使用&lt;fmt:message key="i18n.username"&gt;&lt;/fmt:message&gt;标签添加国际化支持；

 

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!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>LOGIN-I18N</title>
</head>
<body>
    <hr/>
    <h1>LOGIN-I18N</h1>
    
    <form action="#" method="POST">
        <table>
            <tr>
                <td><fmt:message key="i18n.username"></fmt:message></td>
                <td><input type="text" name="no" value=""></td>
            </tr>
            <tr>
                <td><fmt:message key="i18n.password"></fmt:message></td>
                <td><input type="text" name="name" value=""></td>
            </tr>
            <tr align="right">
                <td colspan="2"><button><fmt:message key="i18n.submit"></fmt:message></button></td>
            </tr>
        </table>
    </form>
       
    <hr/>
    <a href="/org.rabbitx.web.spring4mvc/index.jsp">返回首页</a>
</body>
</html>
```

 

6\. 测试地址：http://localhost:8080/org.rabbitx.web.spring4mvc/i18n/default-page