struts2实现国际化
2012-07-25 16:14
417 查看
案例:
在src目录下新建两个属性文件:
welcome_en_US.properties:
greeting=Welcome to china\!
welcome_zh_CN.properties:
greeting=欢迎来到中国
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="welcome"></constant> </struts>
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.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"> </head> <body> <s:text name="greeting"></s:text> </body> </html>
在action中访问资源文件:
User.java:
package blog.i18n; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class User extends ActionSupport { public String execute(){ ActionContext.getContext().put("wel", this.getText("greeting")); return "success"; } }
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="welcome"></constant> <package name="i18n" extends="struts-default" namespace="/person"> <action name="nation" class="blog.i18n.User" method="execute"> <result name="input">/index.jsp</result> <result name="success">/WEB-INF/message.jsp</result> </action> </package> </struts>
message.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.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"> </head> <body> <s:text name="greeting"></s:text> <br/> <%= request.getAttribute("wel") %> </body> </html>
相关文章推荐
- struts2的国际化实现网站整体中英文切换实例代码
- struts2表单验证及国际化实现
- Struts2 的国际化实现
- struts2实现国际化
- Struts2 的国际化实现
- Struts2 的国际化实现
- Struts2中国际化的JAVA实现
- struts2表单验证初步及国际化实现(学习总结)
- 实验_Struts2国际化 登录页面国际化的两种实现方法
- struts2表单验证初步及国际化实现(学习总结)
- Struts2(十三):超链接实现动态加载国际化资源文件
- struts2实现国际化,多语言切换
- struts2,struts1,jsp实现国际化的方法示例
- struts2实现国际化
- Struts2 的国际化实现
- struts2表单验证初步及国际化实现(学习总结)
- struts2 用户自定义实现国际化
- Struts2国际化--用户自行选择程序语言的实现
- struts2表单验证初步及国际化实现(学习总结)
- Struts2的国际化实现