PushTagAction.java
package com.tw511.common.action; import com.opensymphony.xwork2.ActionSupport; public class PushTagAction extends ActionSupport{ public String execute() throws Exception { return SUCCESS; } }
Person.java
package com.tw511.common; public class Person{ private String firstName = "This is firstName"; private String lastName = "This is lastName"; public String getFirstName() { return firstName; } public String getLastName() { return lastName; } }
push.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 push tag example</h1> <h2>1. Normal way</h2> <s:bean name="com.tw511.common.Person" var="personBean" /> First name : <s:property value="#personBean.firstName" /><br/> Last name: <s:property value="#personBean.lastName" /><br/> <h2>2. Push way</h2> <s:push value="#personBean" > First name : <s:property value="firstName" /><br/> Last name: <s:property value="lastName" /><br/> </s:push> </body> </html>
它是如何工作的?
通常情況下,如果你想獲得bean的屬性,可以參考它,如 <s:property value=」#personBean.firstName」 />。使用 <s:push>標籤, 可以推「#personBean」到堆疊的頂部,並直接存取屬性 <s:property value=」firstName」 />。兩者都返回相同的結果,但使用不同的接入機制。
<?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="pushTagAction" class="com.tw511.common.action.PushTagAction" > <result name="success">pages/push.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2pushtag/pushTagAction.action
在瀏覽器中開啟上面的網址,顯示結果如下: