Struts2学习笔记二 国际化

准备资源文件,资源文件的命名格式如下:

package_language_country.properties
package_language.properties
package.properties
其中package是资源文件的基本名,我们可以自定义,但language和country必须是java支持的语言和国家。如:
中国大陆: package_zh_CN.properties 或者 package_zh.properties

美国: package_en_US.properties  或者 package_en.properties


代码包路径如下:


struts-constant.xml中增加以下配置(struts-constant.xml的配置方法请看我的struts学习笔记一,里面有详细介绍)

 <constant name="struts.custom.i18n.resources" value="package"></constant> <!--value的值要和资源文件名第一个的“下划线”前的值一样,否则无法加载资源文件-->

注:当在test包里添加TestAction_zh.properties 后些配置可以不加


Struts2搜索资源文件时是按以下顺序进行的:

当前action所在包下以action名为前辍的文件->当前action的包路径->父包路径->classPath  例:

我的action的路径如下:/struts/src/main/java/com/test/TestAction.java

struts2查找国附化资源文件的路径是:test包里的TestAction_zh.properties->test包里的package_zh.properties->com包里的package_zh.properties->classpath 里的 package_zh.properties


代码实例


新建 TestAction.java 要实现国际化需要继承ActionSupport类

package com.test;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public String demo(){
        return "list";
    }
}

在WebContent下增加 list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ page isELIgnored="false"%>
<!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=ISO-8859-1">
<title>测试</title>
</head>
<body>

<s:property value="getText('login')"/><!-- getText方法在ActionSupport中 -->



</body>
</html>


struts.xml配置

  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
    "http://struts.apache.org/dtds/struts-2.3.dtd">  
 
 
    <struts>
     <package name="default" namespace="/test" extends="struts-default">  
            <action name="login" class="HelloWorld" >  
                <result name="login">/index.jsp</result>  
                 <result name="list">/list.jsp</result>  
            </action>
             <action name="demo" class="com.DemoAction" >  
                 <result name="list">/list.jsp</result>  
            </action>
              <action name="demotest" class="com.test.TestAction" >  
                 <result name="list">/list.jsp</result>  
            </action>

<!--       <action name="hello_login" class="HelloWorld" method="login" >   -->
<!--                 <result name="login">/index.jsp</result>   -->
<!--        </action> -->
<!--        <action name="hello_list" class="HelloWorld" method="list" >   -->
<!--                 <result name="list">/index.jsp</result>   -->
<!--        </action> -->
<!--             <action name="user" class="UserAction"> -->
<!--               <result name="list">/list.jsp</result> -->
<!--             </action> -->
     </package>  
    </struts>


访问URL为:

http://localhost:8080/struts//test/demotest!demo.action

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值