
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 |
|---|---|---|---|---|---|
| id | false | false | String | Deprecated. Use 'var' instead | |
| name | true | false | String | Name of resource property to fetch | |
| searchValueStack | false | true | false | Boolean | Search the stack if property is not found on resources |
| var | false | false | String | Name used to reference the value pushed into the Value Stack |
textfield 、password 、submit标签中的key属性官网描述:
| key | false | false | String | Set 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;
}}
本文介绍了一个使用Struts2框架实现多语言支持的例子。通过配置登录页面的资源文件及表单元素,实现了中文和英文两种语言环境的切换。文章还提供了一种通过Action设置语言环境的方法。
778

被折叠的 条评论
为什么被折叠?



