这篇文章将为大家详细讲解有关Struts2的国际化信息机制是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
国际化信息机制 (三种 Action范围、 Package范围、 全局)
1. 全局国际化配置信息文件
全局国际化文件,对所有Action 生效,任何程序都可以访问到,需要在struts.xml 配置常量 struts.custom.i18n.resources指定信息文件
页面product.jsp
<s:fielderror/>
<form action="${pageContext.request.contextPath }/product_add.action" method="post">
商品名:<input type="text" name="name"/><br/>
价格:<input type="password" name="price"/><br/>
<input type="submit" value="登录"/>
</form>
编写ProductAction
public class ProductAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String name;
private double price;
public String add(){
System.out.println(name+"---------"+price);
return SUCCESS;
/*
get(),set()方法略去.................
*/
}
}
添加校验信息:(对Action的方法进行校验 ProductAction-product_add-validation.xml
)
ProductAction-product_add-validation.xml
其中product_add
是Struts.xml中action标签中的name的值
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<!-- 校验商品 -->
<field name="name">
<field-validator type="requiredstring">
<message key="wc"/>
</field-validator>
</field>
</validators>
新建国际化信息文件 src
下 messages.properties
(默认的国际化文件)
注意:
1. 其中<message key="wc"/>中的Key必须是messages.properties 的Key值
2.messages.properties 的value值必须装换成Unicode码, 使用myeclipse开发工具,内置properties editor 自动将中文转换 Unicode码
2. Action范围国际化文件
在Action类 所在包 创建 Action类名.properties
(无需在struts.xml 配置 )
3. package范围国际化文件
在package下面 建立 package.properties
(无需在struts.xml )
4. 在JSP页面获取
在国际化 messages.properties 添加一个信息
JSP页面代码:
<h2><s:i18n name="messages">
<s:text name="cn.wc"></s:text>
</s:i18n></h2>
5. 在Action代码获取
在messages.properties 添加国际化信息
Action转发的页面JSP
<s:text name="welcome">
<s:param>lxp</s:param>
</s:text>
Action代码:
public class Product2Action extends ActionSupport {
private static final long serialVersionUID = 1L;
public String add(){
System.out.println(this.getText("welcome",new String[]{"Action"}));
return SUCCESS;
}
}
关于Struts2的国际化信息机制是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。