第26讲 struts2国际化配置

国际化(Internationlization),通俗地讲,就是让软件实现对多种语言的支持;
复制项目:HeadFirstStruts2chapter05,改名:HeadFirstStruts2chapter06项目中,修改:web project settings ,删除所有的包和jsp文件,
26.struts2国际化配置
2struts.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="cruise"></constant>
</struts>
3新建cruise.properties文件, 和同名的cruise_zh_CN.properties 文件和cruise_en_US.properties文件,文件名cruise和struts.xml中的value值一致,后缀名的写法是固定的,这里中文要使用Unicode编码的方式(中文转Unicode),
cruise.properties文件,如下:
userName=\u7528\u6237\u540d
password=\u5bc6\u7801
login=\u767b\u5f55
cruise_zh_CN.properties 文件,如下:
userName=\u7528\u6237\u540d
password=\u5bc6\u7801
login=\u767b\u5f55
cruise_en_US.properties文件,如下:
userName=userName
password=password
login=login
3新建一个login.jsp文件, 引入struts标签,<s:text>标签
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table>
    <tr>
       <td><s:text name="userName"></s:text> </td>
       <td><input type="text"/></td>
    </tr>
    <tr>
       <td> <s:text name="password"></s:text> </td>
       <td><input type="password"/></td>
    </tr>
    <tr>
       <td><input type="button" value="<s:text name='login'></s:text>"/></td>
    </tr>
</table>
</body>
</html>
4查看浏览器的编码格式,确保是UTF-8,然后修改浏览器的语言,查看效果。
26.struts2国际化配置
26.struts2国际化配置
5在properties文件中,加入参数,
cruise.properties文件,如下:
userName=\u7528\u6237\u540d
password=\u5bc6\u7801
login=\u767b\u5f55
welcomeInfo=\u6b22\u8fce{0}
cruise_zh_CN.properties 文件,如下:
userName=\u7528\u6237\u540d
password=\u5bc6\u7801
login=\u767b\u5f55
welcomeInfo=\u6b22\u8fce{0}
cruise_en_US.properties文件,如下:
userName=userName
password=password
login=login
welcomeInfo=welcome{0}
6新建welcome.jsp ,引入struts标签,
<%@ 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>
<s:text name="welcomeInfo">
    <s:param>Ashley</s:param>
</s:text>
</table>
</body>
</html>
7测试

猜你喜欢

转载自blog.csdn.net/u010393325/article/details/83928768