---
title: "Struts2day03Struts2.0国际化"
author: "lonemoon"
date: "2012-07-11T21:09:02Z"
tags: []
categories: ["编程语言"]
slug: "struts2day03struts2-0"
draft: false
lastmod: "2012-07-11T21:09:02Z"
id: 1585509
url: "https://lonemoon.iteye.com/blog/1585509"
---

Struts2.0国际化  
    B/S结构中为网页提供多国语言的支持  
       1.B/S架构为每一个语言提供一套页面  
          缺点:修改页面风格，每一套都必须修改  
       2.B/S国际化是Struts2.0的半成品  
          只要提供一套页面，不同国家访问时就能看到相应的文字  
      1.知道用户的所在国家  
         浏览器使用国别码来识别地区  
      2.所在国家文字的提供，在一个页面中可以显示中文或者英文  
         e.properties{username password}  
         c.properties{用户名  密码}  
          login.jsp  
      占位符占位&lt;input type="text" name="username"/&gt;

   Struts2.0国际化开发过程  
      1.提供文字的配置文件(资源文件)  
         1)位置:src目录下面  
     2)命名:xxx.properties  
        appRes\_zh\_CN.properties(中文的资源文件)  
          注意：在处理非拉丁语系的时候，要先转换成unicode编码  
           使用 native2ascii命令  
          native2ascii -encoding gbk 源文件名 目标文件名  
          native2ascii -encoding gbk appRes.properties appRes\_zh\_US.properties  
        appRes\_en\_US.properties(英文的资源文件)  
      2.在struts.xml文件中进行配置

        &lt;constant name="struts.custom.i18n.resources" value="appRes"&gt;&lt;/constant&gt;  
      3.在jsp页面中使用占位符站位  
        使用Struts2.0提供的&lt;s:text name="资源文件的key"/&gt;进行占位

 

1.提供文字的配置文件，位于src目录下

在appRes\_zh\_CN.properties配置文件中

userName=\\u7528\\u6237\\u540D  
passWord=\\u5BC6\\u7801

```
userName=\u7528\u6237\u540D
passWord=\u5BC6\u7801
```

 在appRes\_en\_US.properties配置文件中

```
userName=userName
passWord=passWord
```

 2.在struts.xml文件中

在package标签上面

```
<!--注册源文件-->
<constant name="struts.custom.i18n.resources" value="appRes"></constant>
```

 3.在login.jsp页面 ，使用占位符进行占位

```
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> </head>
  <body>
     <center>
   <form action="login.action" method="post">
   <s:text name="userName"/><input type="text" name="username"/> <br>
   <s:text name="passWord"/><input type="text" name="age"/> <br>
   <input type="submit" value="Submit"/>
   </form>
   </center>
  </body>
</html>
```