您的位置:首页 > 编程语言 > Java开发

struts2 配置带占位符国际化资源(全局)

2011-03-08 20:03 417 查看
1.在src 目录 下添加语言配置文件:

a. orca_en_US.properties

内容:

username=用户姓名

welcome={0}欢迎您到JAVA世界{1}

b. orca_zh_CN.properties

内容:

username=UserName

welcome={0}welcome to JAVA world{1}

2.在struts.xml 中配置(语言文件可以全局访问)

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<!--

configure properties

value="orca" :值为properties文件的前缀

-->

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

<package name="taink" namespace="/control/manager" extends="struts-default">

<action name="employee_*" method="{1}" class="org.taink.struts.action.EmployeeAction">

<result name="input">/index.jsp</result>

<result name="success">/WEB-INF/page/message.jsp</result>

</action>

</package>

</struts>

3.在jsp 页面访问到语言文件, 做以下设置

<%@ taglib uri="/struts-tags" prefix="s"%>

这样就可以通过标签访问了,如下:

信息:

<s:text name="welcome">

<s:param>sany</s:param>

<s:param>study</s:param>

</s:text>


4.在action 中需要访问到语言文件中的信息,如下:

package org.taink.struts.action;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport {

private static final long serialVersionUID = 6892944822771610653L;

public String doAdd() {

ActionContext.getContext().put("message", this.getText("welcome",new String[]{"sany","study"}) );

return "success";

}

public String doUpdate() {

ActionContext.getContext().put("message", "更新成功");

return "success";

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: