struts2 配置国际化全局资源文件

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>
	<constant name="struts.action.extension" value="do,action"></constant>
	<!-- 定义国际化常量 -->
	<constant name="struts.custom.i18n.resources" value="cool"></constant>
	<!-- 国际化 -->
	<package name="person" namespace="/person" extends="struts-default">
		<action name="manage" class="com.cool.global.GlobalAction" >
			<result name="message">/WEB-INF/page/message.jsp</result> 
		</action>
	</package> 
</struts>
 

在项目的src目录下创建cool_en_US.properties,cool_zh_CN.properties两个文件

cool_en_US.properties  存放英文国际化信息

文件里只有一行代码,如下:

 

welcome={0},welcome to cool,{1}


cool_zh_CN.properties 存放中文国际化信息

 

welcome ={0},欢迎来到我的网站,{1}

录入中文的时候,以MyEclipse6.6为例会自动转换成下面的格式:

 

welcome ={0}\uFF0C\u6B22\u8FCE\u6765\u5230\u6211\u7684\u7F51\u7AD9\uFF0C{1}

 

创建com.cool.global.GlobalAction

 

GlobalAction.java

package com.cool.global;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GlobalAction extends ActionSupport{

	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		ActionContext.getContext().put("message", this.getText("welcome",new String[]{"customer","play"}));
		return "message";
	}
	
}

 

WebRoot目录下创建JSP文件Global.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>
    <title>国际化测试</title>    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  <body>
   	<s:text name="welcome">
   		<s:param>customer</s:param>
   		<s:param>study</s:param>
   	</s:text>
  </body>
</html>
 

WebRoot/WEB-INF/page目录下创建message.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Message Page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  <body>
    ${message }
  </body>
</html>

 

Action返回的值,访问地址

http://localhost:8080/struts2/person/manaage.action

 

JSP界面赋值,访问地址

http://localhost:8080/struts2/Global.jsp

 

这样所能够看到的都是中文的提示消息,当然想看到英文的提示消息,需要将浏览器的语言更改为英文。

在IE的,工具->Internet选项->语言,在弹出的对话框中将中文替换为英文即可。

 

测试完成后别忘了切换回中文。

 

以上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值