Struts2 使用 Struts2 实现国际化

图示:

 

首先写三份不同国家语言的 properties 文档

message_en_GB.properties 文档

title=Welcome You Access
headline=Struts demo 1


 

message_zh_CN.properties 文档

title=\u6B22\u8FCE\u4F60\u7684\u767B\u9646
headline=\u8FD9\u662FStruts2\u6F14\u793A\u6848\u4F8B


 

message_zh_HK.properties 文档

title=\u6B61\u8FCE\u4F60\u7684\u767B\u9678
headline=\u9019\u662FStruts2\u6F14\u793A\u6848\u4F8B


 

SetLocalAction Action类

package action;
import java.util.Locale;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class SetLocalAction extends ActionSupport {
	private String language;
	private String location;
	public String getLanguage() {
		return language;
	}
	public void setLanguage(String language) {
		this.language = language;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	@Override
	public String execute() throws Exception {
		if(null!=language&&null!=location){
			ActionContext.getContext().setLocale(new Locale(language, location));
			}else{
			ActionContext.getContext().setLocale(this.getLocale());

			}
			return SUCCESS;
	} 
}

 

配置 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"struts-2.3.dtd">
<struts>
	<!--配置国际化文件 -->
	<constant name="struts.custom.i18n.resources" value="message"></constant>
	<package name="default" namespace="/" extends="struts-default">
		<action name="setLocal" class="action.SetLocalAction">
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>

 

index.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<html>
<head>
<title>使用国际化</title>
</head>
<body>
	<a href="setLocal.action">恢复默认</a>
	<br />    
	<a href="setLocal.action?language=zh&location=CN">设置大陆中文</a>
	<br />    
	<a href="setLocal.action?language=en&location=GB">设置美国英文</a>
	<br />    
	<a href="setLocal.action?language=zh&location=HK">设置香港繁体</a>
	<br />
	<h4><s:text name="title" /></h4>
	<p><s:text name="headline" /></p>
</body>
</html>

 

效果图:

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值