Struts2的国际化
2010-11-23 14:45
78 查看
Struts2的国际化:
1:类的国际化 :类级别的文件命名规则为:className_zh_CN.properties等,className为要国际化的类名
2:包的国际化: 包级别的文件命名规则为:package_zh_CN.properties等,所有的包级别的国际化资源文件都叫这个名字
3:全局国际化: 文件名_语言_地区.properties
演示国际化的步骤:
1:配置struts.xml文件
<struts>
<constant name="struts.i18n.encoding" value="gbk"></constant>
<constant name="struts.devMode" value="true"></constant>
<!--value是文件名-->
<constant name="struts.custom.i18n.resources" value="ActionMessage"></constant>
<package name="default" extends="struts-default">
<action name="login" class="com.ambow.struts2.action.UserAction">
<result name="success">/success.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>
2:拟写Entity代码
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3:拟写Action代码:
public class UserAction extends ActionSupport implements SessionAware{
public Map session;
public User user;
public void setSession(Map session) {
this.session = session;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Map getSession() {
return session;
}
public String execute(){
if("admin".equals(user.getName()) && "admin".equals(user.getPassword())){
session.put("user", user);
<!--这里可以得到资源文件的国际化的值,然后显示到界面 action里面访问资源文件-->
String mess = getText("hello");
session.put("mes", mess);
return "success";
}
return "error";
}
}
4:拟写jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="user.name" key="userName"></s:textfield><br/>
<s:password name="user.password" key="password"></s:password><br/>
<s:submit key="login"></s:submit></br>
</s:form>
</body>
</html>
5:拟写成功界面:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:text name="messagesuccess"></s:text><br/>
<!--jsp页面也可以访问资源文件-->
<s:property value="%{getText('messagesuccess')}"/></br>
<s:property value="#session.mes"/></br>
</body>
</html>
资源文件:
1:ActionMessage_en_US.properties类容
userName=userName
password=userPassword
login=loginUser
messagesuccess=struts internation
4000
hello=welcome allan
2:ActionMessage_zh_CN.properties
userName=/u7528/u6237/u540D
password=/u5BC6/u7801
login=/u767B/u9646
messagesuccess=/u6B22/u8FCE/u767B/u9646struts2/u56FD/u9645/u5316
hello=/u6B22/u8FCEallan/u767B/u9646
然后测试显示成功。
1:类的国际化 :类级别的文件命名规则为:className_zh_CN.properties等,className为要国际化的类名
2:包的国际化: 包级别的文件命名规则为:package_zh_CN.properties等,所有的包级别的国际化资源文件都叫这个名字
3:全局国际化: 文件名_语言_地区.properties
演示国际化的步骤:
1:配置struts.xml文件
<struts>
<constant name="struts.i18n.encoding" value="gbk"></constant>
<constant name="struts.devMode" value="true"></constant>
<!--value是文件名-->
<constant name="struts.custom.i18n.resources" value="ActionMessage"></constant>
<package name="default" extends="struts-default">
<action name="login" class="com.ambow.struts2.action.UserAction">
<result name="success">/success.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>
2:拟写Entity代码
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3:拟写Action代码:
public class UserAction extends ActionSupport implements SessionAware{
public Map session;
public User user;
public void setSession(Map session) {
this.session = session;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Map getSession() {
return session;
}
public String execute(){
if("admin".equals(user.getName()) && "admin".equals(user.getPassword())){
session.put("user", user);
<!--这里可以得到资源文件的国际化的值,然后显示到界面 action里面访问资源文件-->
String mess = getText("hello");
session.put("mes", mess);
return "success";
}
return "error";
}
}
4:拟写jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="user.name" key="userName"></s:textfield><br/>
<s:password name="user.password" key="password"></s:password><br/>
<s:submit key="login"></s:submit></br>
</s:form>
</body>
</html>
5:拟写成功界面:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:text name="messagesuccess"></s:text><br/>
<!--jsp页面也可以访问资源文件-->
<s:property value="%{getText('messagesuccess')}"/></br>
<s:property value="#session.mes"/></br>
</body>
</html>
资源文件:
1:ActionMessage_en_US.properties类容
userName=userName
password=userPassword
login=loginUser
messagesuccess=struts internation
4000
hello=welcome allan
2:ActionMessage_zh_CN.properties
userName=/u7528/u6237/u540D
password=/u5BC6/u7801
login=/u767B/u9646
messagesuccess=/u6B22/u8FCE/u767B/u9646struts2/u56FD/u9645/u5316
hello=/u6B22/u8FCEallan/u767B/u9646
然后测试显示成功。
相关文章推荐
- 实验_Struts2国际化 登录页面国际化的两种实现方法
- struts2,struts1,jsp实现国际化的方法示例
- Struts2之国际化
- struts2 的国际化
- Struts2中文国际化乱码问题
- struts2 国际化
- 9.Struts2的国际化
- struts2国际化的解决方案
- JAVA_WEB Struts2学习:使用验证框架进行校验JSP页面元素 反编译工具的配置 Strtuts2 国际化学习
- struts2国际化全例 错误解决
- struts2进阶【4】——国际化
- struts2的国际化学习笔记
- 坠落凡间的struts2(7)---struts2的国际化
- Struts2国际化整理资料
- 浅谈 Struts2国际化开发技巧
- struts2 国际化和令牌
- 深入理解Struts2国际化信息机制
- struts2 国际化
- struts2 国际化
- Struts2 的国际化实现