<param name="fruit">Banana</param> {Case 1} <param name="fruit" value="Banana"/> {Case 2}
<s:bean name="com.tw511.common.Person" var="personBean"> <s:param name="nickName">yiibai</s:param> </s:bean>
<s:bean name="com.tw511.common.Person" var="personBean"> <s:param name="nickName" value="yiibai"></s:param> </s:bean>
<s:bean name="com.tw511.common.Fruit" var="fruitBean"> <s:param name="fruitName">Banana</s:param> </s:bean> <s:bean name="com.tw511.common.Person" var="personBean"> <s:param name="favorFruit" value="#fruitBean"></s:param> </s:bean>
<s:bean name="com.tw511.common.Person" var="personBean"> <s:param name="age" value="99"></s:param> </s:bean> // or <s:bean name="com.tw511.common.Person" var="personBean"> <s:param name="age">99</s:param> </s:bean>
ParamTagAction.java
package com.tw511.common.action; import com.opensymphony.xwork2.ActionSupport; public class ParamTagAction extends ActionSupport{ public String execute() { return SUCCESS; } }
Person.java
package com.tw511.common; public class Person{ private String nickName; private int age; private Fruit favorFruit; public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Fruit getFavorFruit() { return favorFruit; } public void setFavorFruit(Fruit favorFruit) { this.favorFruit = favorFruit; } public String getFruitName(){ return this.favorFruit.getFruitName(); } }
Fruit.java
package com.tw511.common; public class Fruit{ private String fruitName; public String getFruitName() { return fruitName; } public void setFruitName(String fruitName) { this.fruitName = fruitName; } }
param.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 param tag example</h1> <s:bean name="com.tw511.common.Fruit" var="fruitBean"> <s:param name="fruitName">Banana</s:param> </s:bean> <s:bean name="com.tw511.common.Person" var="personBean"> <s:param name="nickName">ah pig ah dog</s:param> <s:param name="age">99</s:param> <s:param name="favorFruit" value="#fruitBean"></s:param> </s:bean> <h2>PersonBean</h2> <ol> <li>NickName property : <s:property value="#personBean.nickName" /></li> <li>Age property : <s:property value="#personBean.age" /></li> <li>Fruit property : <s:property value="#personBean.fruitName" /></li> </ol> </body> </html>
<?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="paramTagAction" class="com.tw511.common.action.ParamTagAction" > <result name="success">pages/param.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2paramtag/paramTagAction.action
在瀏覽中打上面的網址,存取後顯示結果如下: