---
title: "Struts2中的国际化"
author: "tom_seed"
date: "2010-08-17T20:46:44Z"
tags: ["HTML", "JSP", "Struts", "XML", "浏览器"]
categories: ["企业架构"]
slug: "struts2"
draft: false
lastmod: "2012-06-24T14:48:49Z"
id: 739573
url: "https://tom-seed.iteye.com/blog/739573"
---

**Struts2的国际化**

**1.JSP页面(locale.jsp)**

**2.(中、英)两个属性文件(global\_en\_US.properties和global\_zh\_CN.properties)**

**3.struts.properties文件中的键值对**

**1.JSP页面(locale.jsp)**

```
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>国际化练习</title>
    </head>
    <body>
            <s:textfield name="username" label="%{getText('test.field')}"
                value="%{getText('test.field')}"></s:textfield>
        </s:form>
    </body>
</html>
```

 

 

**2.(中、英)两个属性文件(global\_en\_US.properties和global\_zh\_CN.properties)**

**这两个文件与struts.properties文件一起放在项目的src目录下(即classes的根目录中)**

**创建global\_en\_US.properties属性文件，内容为：**

```
test.field=English_global
```

 

 

**创建global\_zh\_CN.properties属性文件，内容为：**

 

```
test.field=Chinese_global
```

 

 

**3.在struts.properties文件中添加键值对**

```
struts.custom.i18n.resources=global
```

 

 

**设置当前当地语言环境,相当于设置浏览器的默认语言**

```
struts.locale=en_US
```

 

 

 

**如果不希望在属性文件中配置国际化也可以在struts.xml文件中进行国际化配置**

```
<constant name="struts.custom.i18n.resources" value="global" />
```

 

 

**国际化总结：**

**1.Struts会优先寻找浏览器或在配置文件中设置的当地语言环境所使用的默认语言，若未找到，则会寻找以struts.custom.i18n.resources对应的值为名称的properties属性文件(例global.properties)**

**2.struts.locale=en\_US,当没有找到该属性文件时,struts会先去查找浏览器默认语言对应的属性(global\_zh\_CN.properties)文件,若还未找到,则会寻找以struts.custom.i18n.resources对应的值为名称的properties属性文件(例global.properties)**

**3.当优先找到的属性(properties)文件中没有对应的属性项时,struts会寻找以struts.custom.i18n.resources对应的值为名称的properties属性文件(例global.properties)中对应的属性项,若有则显示。**