本文主要是介绍struts2的国际化i18n,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文主要介绍struts2的国际化,首先在struts.xml文件中配置 常量值
<constant name="struts.custom.i18n.resources" value="app"/>(app为properties部分文件名,全名为app_en_US.properties或app_zh_CN.properties)
app_en_US.propertie内容
city=NEW YORK
street=51 AVENUE
reg.error.name.null=username cannot null
reg.error.name.length=username's length should be 5-10
country=CHINA
welcome.msg=welcome,{0}
submit=SUBMIT
app_zh_CN.properties内容
city=中国
street=山海
reg.error.name.null=用户名不能为空
reg.error.name.length=用户名长度应该在5-10之间
country=中国
welcome.msg=欢迎你,{0}
submit=注册
一.java中获取properties文件中的信息(与struts2无关,properties需直接放在src目录下)
public class Testi18n{public static void main(String [] args){ResourceBundle rb=ResourceBundle.getBundle("app",Locale.US);String city=rb.getString("city");System.out.println(city);}}
输出为
NEW YORK
二.在jsp页面中获取properties文件中的信息
i18n.jsp文件:
<%@page import="org.omg.CORBA.Request"%>
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<span style="white-space:pre"> </span><hr>
<span style="white-space:pre"> </span><%
<span style="white-space:pre"> </span>request.setAttribute("name", "SIEGE");
<span style="white-space:pre"> </span>%>
<span style="white-space:pre"> </span><!-- 使用s:text 标签输出国际化消息-->
<span style="white-space:pre"> </span><s:property value="getText('street')"/>
<span style="white-space:pre"> </span> <!--使用s:param为国际化信息的占位符传入参数-->
<span style="white-space:pre"> </span><s:text name="welcome.msg">
<span style="white-space:pre"> </span><s:param><s:property value="#request.name"/></s:param>
<span style="white-space:pre"> </span></s:text>
<span style="white-space:pre"> </span> <!-- 在表单元素中使用key来指定国际化消息的key-->
<span style="white-space:pre"> </span><s:form>
<span style="white-space:pre"> </span> <s:submit name="submit" key="submit" />
<span style="white-space:pre"> </span><!--通过ognl的%符号来获取值-->
<span style="white-space:pre"> </span> <s:submit value="%{getText('submit')}"></s:submit>
<span style="white-space:pre"> </span></s:form>
</body>
</html>
输出结果为:
这篇关于struts2的国际化i18n的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!