- 在struts.xml中添加代码
<!-- 通过“struts.custom.i18n.resources”属性指定国际化资源文件的基名为“message”,位于src目录下 --> <constant name = "struts.custom.i18n.resources" value="message"/> <constant name = "struts.i18n.encoding" value = "utf-8"/>
注意:这里value的值是文件的基名
-
在src目录下
加入message_zh_CN.properties
titleone=登陆 ttile2=欢迎页面 head1=登陆 head2=欢迎 welcome=登陆成功 form.name=用户名 form.password=密码 form.submit=提交
加入 message_en_US.properties
titleone=LoginForm ttile2=Login success head1=Login head2=welcome to login welcome=login success form.name=name form.password=password form.submit=submit
-
在页面前加
<!-- 加载struts2标签库 --> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
将需要使用<s:text name=../>的地方包裹在<s:i18n name="message"></s:i18n>中 ,这步非常重要,之前一直无法找到资源文件,后来看了书才知道是因为<s:text name=..>会加载<s:i18n name="message"></s:i18n>里指定的国际化资源文件,message是基名
-
在需要国际化的地方加上<s:text name=../>,记得写结束,比如loginForm.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!-- 加载struts2标签库 --> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <s:i18n name="message"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><s:text name="titleone" /></title> </head> <body> <center> <span style="color: red; font-weight: bold"> <% //若错误信息不为空,则打印错误信息 if (request.getAttribute("error") != null) { out.println(request.getAttribute("error") + "<br/>"); } %> </span> <h1> <s:text name="head1" /> </h1> <form action="login" method="post"> <s:text name="form.name" /> :<input type="text" name="username"><br> <br> <s:text name="form.password" /> :<input type="password" name="password"><br> <br> <input type="submit" value="<s:text name="form.submit"/>"> </form> </center> </body> </html> </s:i18n>
struts2国际化
最新推荐文章于 2020-10-05 10:38:58 发布