Struts2用户主动选择国际化 应用详解

本文介绍了一个使用Struts2框架实现多语言支持的例子。通过配置登录页面的资源文件及表单元素,实现了中文和英文两种语言环境的切换。文章还提供了一种通过Action设置语言环境的方法。

login.jsp:

<%@ page language="java" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- struts2标签库调用声明 -->
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
	<title><s:text name="loginPage"></s:text></title>
</head>
<body>
	<!-- form标签库定义,以及调用哪个Action声明 -->
	<s:form action="Login">
		<table width="60%" height="76" border="0">
				<!-- 各标签定义 -->
				<s:textfield name="username" key="username"/>
				<s:password name="password" key="password" />
				<s:submit key="loginSubmit"  align="center"/>				
		</table>
	</s:form>
	<!-- 指定URL为英文的语言设置 -->
	<s:url id="english" action="loginLanguage">
	   <!-- 参数request_locale设置英文 -->
	   <s:param name="request_locale">en_US</s:param>
	</s:url>
	<!-- 英文语言设置的链接定义 -->
	<s:a href="%{english}">English</s:a>
	
    <!-- 指定URL为中文的语言设置 -->
	<s:url id="chinese" action="loginLanguage">
	    <!-- 参数request_locale设置中文 -->
        <s:param name="request_locale">zh_CN</s:param>
    </s:url>
    <!-- 中文语言设置的链接定义 -->
    <s:a href="%{chinese}">中文</s:a>
</body>
</html>

解析:<s:text name="loginPage" />

标签text官网描述:

Description

Render a I18n text message.

The message must be in a resource bundle with the same name as the action that it is associated with. In practice this means that you should create a properties file in the same package as your Java class with the same name as your class, but with .properties extension.

If the named message is not found in a property file, then the body of the tag will be used as default message. If no body is used, then the stack will be searched, and if a value is returned, it will written to the output. If no value is found on the stack, the key of the message will be written out.

Parameters

Name
Required
Default
Evaluated
Type
Description
idfalse falseStringDeprecated. Use 'var' instead
nametrue falseStringName of resource property to fetch
searchValueStackfalsetruefalseBooleanSearch the stack if property is not found on resources
varfalse falseStringName used to reference the value pushed into the Value Stack

 

textfield 、password 、submit标签中的key属性官网描述:

keyfalse falseStringSet the key (name, value, label) for this particular component

原先可以使用label属性定义显示值,现在使用key属性,在不同语言环境下username的key得到的value值就不同。

标签url官网参数描述:

<url>标签嵌套的<param>的name属性为“request_locale”,其原理就是把Session中的request_locale参数值设置为该语言选项,这样用户也没有必要在操作系统中选择语言或者使用其他办法选择语言选项。点击“中文”链接时地址栏为http://localhost:8080/Convert/loginLanguage.action?request_locale=zh_CN ,即其后添加了"?request_local=zh_CN";同样,点击"English"链接时地址栏为http://localhost:8080/Convert/loginLanguage.action?request_locale=en_US 。

对应显示页面为:

          

messageResource_en_US.properties:

messageResource_zh_CN.properties:

当然还有其他的切换语言的方式,如:

先在jsp中添加两个链接:

<a href="changeLang.action?lang=1">中文</a>
 <a href="changeLang.action?lang=2">english</a>


然后再写一个action类。重写execute方法: 

public class ChangeLang extends ActionSupport {
	private String lang;
	Locale l=null;
	@Override
	public String execute() throws Exception {
		if(lang.equals("1")){
			//中文版
			l=Locale.CHINA;
		}else{
			//英文版
			l=Locale.US;
		}
		ActionContext.getContext().setLocale(l);//添加到locale对象中
		ServletActionContext.getRequest().getSession().setAttribute("WW_TRANS_I18N_LOCALE", l);//设置locale的属性
		return LOGIN;
	}}


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itzyjr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值