TextTagAction.java
package com.tw511.common.action; import com.opensymphony.xwork2.ActionSupport; public class TextTagAction extends ActionSupport{ public String execute() throws Exception { return SUCCESS; } }
TextTagAction.properies
name.msg = "This is a message from properties file" name.msg.param = "This is a message from properties file - param : {0}"
text.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head><title>www.tw511.com</title> </head> <body> <h1>Struts2 text標籤範例</h1> <h2>1.<s:text name="name.msg" /></h2> Output : <s:text name="name.msg" /> <h2>2. <s:text name="name.msg.unknow">message doesn't exists</s:text></h2> Output : <s:text name="name.msg.unknow">message doesn't exists</s:text> <h2>3. <s:text name="name.msg.unknow" /></h2> Output : <s:text name="name.msg.unknow" /> <h2>4. <s:text name="name.msg.param" ><s:param >yiibai</s:param> </s:text></h2> Output : <s:text name="name.msg.param" > <s:param >yiibai</s:param> </s:text> </body> </html>
它是如何工作的?
1. <s:text name=」name.msg」 />
從資源包獲取並顯示訊息(TextTagAction.properies)關聯當前動作類 (TextTagAction.action).
"This is a message from properties file"
2. <s:text name=」name.msg.unknow」>message doesn’t exists</s:text>
message doesn't exists
3. <s:text name=」name.msg.unknow」 />
name.msg.unknow
4. <s:text name=」name.msg.param」 ><s:param >yiibai</s:param></s:text>
"This is a message from properties file - param : yiibai"
<?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="textTagAction" class="com.tw511.common.action.TextTagAction" > <result name="success">/pages/text.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2texttag/textTagAction.action
在瀏覽器中開啟上面的網址,顯示結果如下所示: