Struts 2“ i18n ”标签用于从任何已声明的资源束中获取消息,而不仅仅是与当前操作关联的资源束。 请参见下面的完整“ i18n ”标签示例:
1.行动
动作类转发请求。
I18nTagAction.java
package com.mkyong.common.action;
import com.opensymphony.xwork2.ActionSupport;
public class I18nTagAction extends ActionSupport{
public String execute() throws Exception {
return SUCCESS;
}
}
2.属性文件
演示的两个属性文件。
I18nTagAction.properties
i18n.msg = "This is a message from I18nTagAction.properties"
Custom.properties
i18n.msg = "This is a message from Custom.properties"
3. i18n标签示例
它显示了“ i18n ”标签的使用。
i18n.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>Struts 2 i18n tag example</h1>
<h2>1.Get message from I18nTagAction.properties</h2>
Output :
<s:text name="i18n.msg" />
<h2>2.Get message from Custom.properties</h2>
Output :
<s:i18n name="com/mkyong/common/action/Custom">
<s:text name="i18n.msg" />
</s:i18n>
</body>
</html>
它是如何工作的?
1.在示例1中,它将从与当前操作类( I18nTagAction.java )相关联的资源包( I18nTagAction.properties )中获取消息。
2.在示例2中,它将从com / mkyong / common / action /文件夹中的“ Custom.properties ”属性文件中获取消息。
不要放入.properties后缀
在i18n标记中,一个非常常见的错误是,如果使用.properties后缀声明了属性文件,Struts 2将无法从声明的资源束中获取消息。
错误方式 :<s:i18n name="com/mkyong/common/action/Custom.properties"> <s:text name="i18n.msg" /> </s:i18n>
正确的方法 :
声明了不带.properties后缀的属性文件。<s:i18n name="com/mkyong/common/action/Custom"> <s:text name="i18n.msg" /> </s:i18n>
4. struts.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.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="i18nTagAction"
class="com.mkyong.common.action.I18nTagAction" >
<result name="success">pages/i18n.jsp</result>
</action>
</package>
</struts>
5.演示
http:// localhost:8080 / Struts2Example / i18nTagAction.action
输出量
参考
标签: struts2