Struts2之国际化
2015-02-01 18:59
309 查看
1:在Src下建立两个属性文件(注:两个文件的key值要相同)
其中内容如下
RES_en_US.properties
username=username
password=password
welcome={0} welcome to J***A world{1}
submit=submit
RES_zh_CN.properties(里面放的是中文对应的unicode编码)
username=\u7528\u6237\u540D
password=\u5BC6\u7801
welcome={0} \u6B22\u8FCE\u6765\u5230J***A\u4E16\u754C{1}
submit=\u63D0\u4EA4
2 index.jsp的核心代码
<%@ taglib uri="/struts-tags" prefix="s"%>
<body>
<a href="lang?request_locale=zh_CN">中文</a>
<a href="lang?request_locale=en_US">English</a>
<s:form action="show" method="post">
${message}
<s:textfield name="username" key="username" ></s:textfield>//username是属性文件中的key值
//JSP为占位符传参(后续加上去的运行截图显示时没有这段代码)
<s:text name="welcome">
<s:param>晓波哥</s:param>
<s:param>学习</s:param>
</s:text>
<s:submit key="submit"></s:submit>
</s:form>
</body>
3action类
(1)中英文切换对应的action
package org.action;
import com.opensymphony.xwork2.ActionSupport;
public class LangAction extends ActionSupport {
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
(2)表单提交的action
package org.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ResAction extends ActionSupport {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String execute() throws Exception {
// action中为占位符传参
ActionContext.getContext().put("message", this.getText("welcome",new String[]{username,"学习"})) ;//页面中有个${message}获取该值,welcome是属性文件中的key值
return SUCCESS;
}
}
3struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--下面这个全局常量说明使用属性文件进行国际化-->
<constant name="struts.custom.i18n.resources" value="RES"/>
<package name="default" extends="struts-default">
<action name="show" class="org.action.ResAction" >
<result name="success">/index.jsp</result>
</action>
<action name="lang" class="org.action.LangAction" >
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
4运行截图:
(1)测试中英文切换
(2)输入用户名并且在文本框上方显示出来(中文下的就不截图了
)
其中内容如下
RES_en_US.properties
username=username
password=password
welcome={0} welcome to J***A world{1}
submit=submit
RES_zh_CN.properties(里面放的是中文对应的unicode编码)
username=\u7528\u6237\u540D
password=\u5BC6\u7801
welcome={0} \u6B22\u8FCE\u6765\u5230J***A\u4E16\u754C{1}
submit=\u63D0\u4EA4
2 index.jsp的核心代码
<%@ taglib uri="/struts-tags" prefix="s"%>
<body>
<a href="lang?request_locale=zh_CN">中文</a>
<a href="lang?request_locale=en_US">English</a>
<s:form action="show" method="post">
${message}
<s:textfield name="username" key="username" ></s:textfield>//username是属性文件中的key值
//JSP为占位符传参(后续加上去的运行截图显示时没有这段代码)
<s:text name="welcome">
<s:param>晓波哥</s:param>
<s:param>学习</s:param>
</s:text>
<s:submit key="submit"></s:submit>
</s:form>
</body>
3action类
(1)中英文切换对应的action
package org.action;
import com.opensymphony.xwork2.ActionSupport;
public class LangAction extends ActionSupport {
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
(2)表单提交的action
package org.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ResAction extends ActionSupport {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String execute() throws Exception {
// action中为占位符传参
ActionContext.getContext().put("message", this.getText("welcome",new String[]{username,"学习"})) ;//页面中有个${message}获取该值,welcome是属性文件中的key值
return SUCCESS;
}
}
3struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--下面这个全局常量说明使用属性文件进行国际化-->
<constant name="struts.custom.i18n.resources" value="RES"/>
<package name="default" extends="struts-default">
<action name="show" class="org.action.ResAction" >
<result name="success">/index.jsp</result>
</action>
<action name="lang" class="org.action.LangAction" >
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
4运行截图:
(1)测试中英文切换
(2)输入用户名并且在文本框上方显示出来(中文下的就不截图了
)
相关文章推荐
- struts2笔记(12)-----Struts2的国际化
- Struts2系列:(26) 国际化(四)使用UI标签的key属性获取国际化资源
- struts2国际化详解
- struts2 国际化 cookie保存语言, 下次访问时显示当前设置的语言
- struts2 国际化
- 关于displaytag中支持struts2国际化问题解答方法
- struts2 的国际化支持
- struts2国际化
- struts2 国际化资源文件自定义的路径解决方法
- struts2 国际化
- struts2国际化问题
- Struts2中文国际化乱码问题
- Struts2国际化
- 6.struts2国际化
- struts2的国际化 i18n 总结
- struts2配置包范围国际化资源文件--包范围
- Struts2 输出带有占位符的国际化信息
- 用javascript结合struts2实现国际化的一种比较戳的方法
- 3.5 Struts2国际化应用
- Struts2_08_Struts2国际化实例