(六)Struts2 国际化

所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)

第一节:国际化简介

国际化(Internationlization),通俗地讲,就是让软件实现对多种语言的支持。

让美国人看到的是英语,让中国人看到的汉语

第二节:Struts2 国际化设置

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

<s:text name=""></s:text> 访问国际化资源

struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     
 8   <constant name="struts.custom.i18n.resources" value="wishwzp"></constant>
 9  
10 </struts>

wishwzp.properties文件内容:(这个是默认的)

  userName=u7528u6237u540d
  password=u5bc6u7801
  login=u767bu5f55
  welcomeInfo=u6b22u8fce{0}

wishwzp_zh_CN.properties文件内容:(这个是中文的)

  userName=u7528u6237u540d
  password=u5bc6u7801
  login=u767bu5f55
  welcomeInfo=u6b22u8fce{0}

wishwzp_en_US.properties文件内容:(这个是英文的)

  userName=userName
  password=password
  login=login
  welcomeInfo=welcome{0}

上面的u7528u6237u540d(“用户名”中文转Unicode为“u7528u6237u540d”)

    u5bc6u7801(“密码”中文转Unicode为“u5bc6u7801”)

    u767bu5f55(“登录”中文转Unicode为“\u767bu5f55”)

    u6b22u8fce(“欢迎”中文转Unicode为“u6b22u8fce”)

都是采用unicode的

Unicode转换工具百度Unicode编码转换 - 站长工具

login.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <table>
12     <tr>
13         <td><s:text name="userName"></s:text></td>
14         <td>
15             <input type="text"/>
16         </td>
17     </tr>
18     <tr>
19         <td><s:text name="password"></s:text></td>
20         <td>
21             <input type="text"/>
22         </td>
23     </tr>
24     <tr>
25         <td>
26             <input type="button" value="<s:text name='login'></s:text>"/>
27         </td>
28     </tr>
29 </table>
30 </body>
31 </html>

welcome.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@taglib prefix="s" uri="/struts-tags" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <s:text name="welcomeInfo">
12     <s:param>Jack</s:param>
13 </s:text>
14 </body>
15 </html>
原文地址:https://www.cnblogs.com/wishwzp/p/5472434.html