struts的国际化支持参看空项目进行配置
原理:它就是通过Http协议将浏览器里面设置的Locale信息发送给服务端
1、struts国际化的配置
* 在struts-config.xml文件中加入
<message-resources parameter="MessageResources" />
2、提供国际化资源文件
* MessageResources.properties
* MessageResources_zh_CN.properties
* MessageResources_en_US.properties
中文需要转换成unicode编码
3、在jsp中采用<bean:message>读取国际化消息文本
4、利用struts默认将Locale放到Session中的特性,我们可以完成语言的自动切换,看博文struts流程分析
* ChangeLanguageAction.java
5、动态消息国际化主要分为三个步骤:
* 创建国际化消息文本
ActionMessage am=new ActionMessage("login.usernotfound",new Object[]{username});
ams.add("error_1",am);
* 传递国际化消息文本
this.saveMessages(request, ams);
* 显示国际化消息文本
如何创建国际化消息文本?
理解ActionMessage和ActionMessages两个对象的区别
ActionMessage:此对象对应着国际化的一条消息,相当于把国际化资源文件中的信息读到这个对象里.
如何传递国际化消息文本?
调用saveMessages()传递提示消息,调用saveErrors()传递错误消息
如何显示国际化消息文本?
通过<html:messages>显示提示消息和错误消息
通过<html:errors>显示错误消息(只能显示错误消息)
在jsp中采用<bean:message>读取国际化消息文本和<html:messages>,<html:errors>读取消息的区别是什么?
<bean:message>只要配置了国际化消息他就能通过key来读取国际化资源文件中的信息.他理解为直接去读国际化资源文件的内容
<html:messages>它的ID的理解,其实他就是读ActionMessage消息的,id,<html:messages id="msg" >他默认就是读ErrorKey上的东西的
用<html:errors>时可以用struts本身提供的东西去修饰如:
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
你可以自己去加,如:
errors.header=<UL>
errors.prefix=<font color="red"><LI>
errors.suffix=</LI></font>
errors.footer=</UL>
jstl对国际化的支持:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<fmt:setLocale value="${header['accept-language']}"/>
<fmt:setBundle basename="resources.MessageResources"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="login.do" method="post">
<fmt:message key="login.form.field.username"/>:<input type="text" name="username"><br>
<fmt:message key="login.form.field.password"/>:<input type="password" name="password"></br>
<input type="submit" value="<fmt:message key="login.form.button.login"/>">
</form>
</body>
</html>