I18nTagAction.java
package com.tw511.common.action; import com.opensymphony.xwork2.ActionSupport; public class I18nTagAction extends ActionSupport{ public String execute() throws Exception { return SUCCESS; } }
I18nTagAction.properties
i18n.msg = "This is a message from I18nTagAction.properties"
Custom.properties
i18n.msg = "This is a message from Custom.properties"
下面顯示<s:i18n>標籤的使用。
i18n.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts2 <s:i18n>標籤範例 - www.tw511.com</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/yiibai/common/action/Custom"> <s:text name="i18n.msg" /> </s:i18n> </body> </html>
它是如何工作的?
1.在範例1中,它會得到來自資源包的訊息(I18nTagAction.properties)這是關想當前的操作類 (I18nTagAction.java)
2.在例2中,它會從「Custom.properties」屬性的檔案得到訊息,這個檔案放在 com/yiibai/common/action/ 檔案
<s:i18n name="com/yiibai/common/action/Custom.properties"> <s:text name="i18n.msg" /> </s:i18n>
正確的方式 :
<s:i18n name="com/yiibai/common/action/Custom"> <s:text name="i18n.msg" /> </s:i18n>
<?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.tw511.common.action.I18nTagAction" > <result name="success">pages/i18n.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2i18ntag/i18nTagAction.action
在瀏覽器中開啟上面的URL,顯示結果如下: