struts2 国际化

一、国际化概念(了解)

      1、什么是国际化

         软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的、符合来访者阅读习惯的页面或数据。

      2、什么需要国际化

      程序:需要国际化。

      数据:是什么样的就是什么样的。

      比如:

        用户注册的表单,有用户名,密码这5个汉字,在zh_CN语言环境,显示的就是用户名和密码。但是在en_US语言环境,显示的就应该是username和password。这就是程序。

        用户名输入的是【张三】,密码输入的是【test】,那无论在什么语言环境都应该是是【张三】和【test】。这就是数据。

      3、固定文本的国际化

                   例如:消息提示,错误提示和菜单,导航栏等等固定文本。

              步骤:

           3.1、创建一个消息资源包

                            一个资源包由多个文件组成,这些文件名都有命名规范:主要文件名_语言代码_国家代码.properties。  语言代码:由iso规定的。国家代码:有iso规定的

                            当文件只有主要文件名.properties时,表明它是默认资源包。浏览器会根据不同的语言环境找对应语言环境的资源包,当没有时,找默认的。

                            每个资源包的内容都由相同的key和对应语言环境的value组成。

                            比如:

                                   message_zh_CN.properties        message_zh_HK.properties    message_en_US.properties

           3.2、读取资源包中的内容

                  

 1 /**
 2  * 国际化的小测试
 3  * @author zhy
 4  *
 5  */
 6 public class I18NDemo {
 7     //取其它
 8     @Test
 9     public void test1(){
10         //使用ResourceBundle的getBundle方法获取一个对象,参数使用消息资源包文件路径+名称和所处的语言环境
11         //注意在给定消息资源包路径时,不需要指定语言代码和国家代码
12         ResourceBundle bundle = ResourceBundle.getBundle("com.itheima.resource.message",Locale.US);
13         String key = bundle.getString("key");
14         System.out.println(key);
15     }
16 
17 //取本地
18 @Test
19     public void test1(){
20         //使用ResourceBundle的getBundle方法获取一个对象,参数使用消息资源包文件路径+名称和所处的语言环境
21         //注意在给定消息资源包路径时,不需要指定语言代码和国家代码
22         ResourceBundle bundle = ResourceBundle.getBundle("com.itheima.resource.message");
23         String key = bundle.getString("key");
24         System.out.println(key);
25     }    
26 }
引用测试

                      jsp中使用国际化:

                      

1 key=u5168u5C40u7684u4E2Du6587u7684u6D88u606Fu8D44u6E90u5305
2 
3 
4 jsp.login.title=u56FDu9645u5316
5 jsp.login.username=u7528u6237u540D
6 jsp.login.password=u5BC6u7801
7 jsp.login.submit=u767Bu5F55
message_zh_CN.properties
1 key=english message resource
2 
3 
4 jsp.login.title=i18n
5 jsp.login.username=Username
6 jsp.login.password=Password
7 jsp.login.submit=Submit
message_en_US.properties
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 Locale locale = request.getLocale();
 4 ResourceBundle bundle = ResourceBundle.getBundle("com.itheima.resource.message",locale);
 5 %>
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>
 9     <title><%=bundle.getString("jsp.login.title") %></title>
10   </head> 
11   <body>
12         <form action="">
13             <%=bundle.getString("jsp.login.username") %><input type="text" name="username"/><br/>
14             <%=bundle.getString("jsp.login.password") %><input type="password" name="password"/><br/>
15            <input type="submit" value="<%=bundle.getString("jsp.login.submit") %>" />
16         </form> 
17   </body>
18 </html>
jsp文件

      但是在jsp2.0之后不允许jsp代码和java代码嵌套, 所以使用 :

                     使用jstlfmt标签:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
 4 <%--
 5 Locale locale = request.getLocale();
 6 ResourceBundle bundle = ResourceBundle.getBundle("com.ittest.resource.message",locale);
 7 --%>  <!-- 这种代码和jsp标签结合的方式在jsp2.0之后就不能再使用,所以在这里行不通 -->
 8 <fmt:setLocale value="${pageContext.request.contextPath }" />
 9 <fmt:setBundle basename="com.ittest.resource.message" var="bundle" />
10 <html>
11 <head>
12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
13 
14 
15 <title><fmt:message key="jsp.login.title" bundle="${bundle }" /></title>
16 </head>
17 <body>
18 
19         <form action="login" method="post">
20             <fmt:message key="jsp.login.username" bundle="${bundle }" /><input type="text" name="username"/>
21             <fmt:message key="jsp.login.password" bundle="${bundle }" /><input type="password" name="password"/>
22                 <input type="submit" value="<fmt:message key="jsp.login.submit" bundle="${bundle }" />" />
23         </form>
24 </body>
25 </html>
jsp文件2

二、Struts2中的国际化(了解)

      1、Struts2中使用国际化的前提

                            首先,我们要知道,在Struts2中,所有的消息提示都是基于国际化的。

                     其次,要想在Struts2中使用国际化,动作类必须继承ActionSupport类。

      2、Struts2中使用国际化

           2.1、配置资源包

                                    a、配置全局资源包                            

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <constant name="struts.devMode" value="true" />
 7     <!-- 配置struts2的默认消息资源包,全局的 -->
 8     <constant name="struts.custom.i18n.resources" value="com.itheima.resource.message" />
 9     <package name="p1" extends="struts-default">
10         <action name="action1" class="com.itheima.web.action.Demo1Action">
11             <result>/success.jsp</result>
12         </action>
13     </package>
14 </struts>
struts。xml

                                   b、配置包范围的资源包

                                          资源包名称命名规范:package_语言代码_国家代码.properties(固定的)。以此种命名方式的资源包能被该包及其子包中的动作类访问。

                                          优先级:高于全局消息资源包

                                   c、局部消息资源包(只为动作类来使用的)

                                          资源包名称命名规范:动作类名称_语言代码_国家代码.properties。以此种命名方式的资源包,只为动作类服务。

                                          优先级最高(就近原则)。

                                      Struts2中资源包的搜索顺序:

                        actionClass.properties

                        package.properties

                        global resource properties

                                  

           2.2、读取资源包的内容

                                   a、动作类中的读取方式(实际开发中几乎从来不用)        

 1 package com.itheima.web.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class Demo1Action extends ActionSupport {
 6 
 7     public String execute(){
 8         String value = getText("key");
 9         System.out.println(value);
10         return SUCCESS;
11     }
12 }
View Code

                                   b、在页面中读取资源包内容

                                          直接访问jsp

                                          

                                          通过动作类访问jsp

                                    

                                   c、自由指定读取资源包

                                  

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="/struts-tags" prefix="s" %>
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6     <title>sturs2中的国际</title>
 7   </head>
 8   <body>
 9    sturs2中的国际,在jsp页面中访问消息资源包,必须使用struts2的标签
10    <%--使用s:text获取消息资源包的值 :
11        当直接访问jsp时,因为没有经过动作类,只会去查找全局消息资源包.
12        如果经过了动作类,则先去找动作类的。
13    --%>
14    <s:text name="key" /><hr/>
15    <%--当在消息资源包中都找不到key值时,直接把name属性的值输出到页面上 --%>
16    <s:text name="abc" />
17    <hr/>
18    <%--s:i18n标签,读取指定消息资源包 --%>
19    <s:i18n name="com.itheima.resource.message">
20     <s:text name="key" />
21    </s:i18n>
22    <hr/>
23    <%--当自由指定读取消息资源包不存在时, 是按照资源包的搜索顺序去查找
24       搜索顺序的定义,详见官方文档或笔记
25    --%>
26    <s:i18n name="cn.itcast.resource.message">
27            <s:text name="key" />
28    </s:i18n>
29   </body>
30 </html>
b c

      

 

原文地址:https://www.cnblogs.com/soficircle/p/7062033.html