Struts 2 i18n标签示例

下载它– Struts2-I18n-Tag-Example.zip

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

输出量

Struts 2 i18n tag example

参考

  1. Struts 2 i18n标签文档

翻译自: https://mkyong.com/struts2/struts-2-i18n-tag-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值