Struts 2プッシュタグの例
ダウンロードする – リンク://wp-content/uploads/2010/07/Struts2-Push-Tag-Example.zip[Struts2-Push-Tag-Example.zip]
Struts 2 ”
push
“タグはスタックの先頭に値をプッシュするために使用されるため、アクセスや参照が容易になります。完全な ”
push
“タグの例を参照してください:
1.アクション
要求を転送するためのアクションクラス。
package com.mkyong.common.action; import com.opensymphony.xwork2.ActionSupport; public class PushTagAction extends ActionSupport{ public String execute() throws Exception { return SUCCESS; } }
2.ビーンズ
単純なPersonクラスで、後で簡単にアクセスできるようにスタックにプッシュします。
-
Person.java **
package com.mkyong.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; } }
3.プッシュタグの例
それは ”
push
“タグの使用を示しています。
-
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.mkyong.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”/>
のように参照できます。 ”
push
“タグを使用すると、スタックの先頭に ”
#personBean
“をプッシュし、直接
<s:property value = “firstName”/> ** プロパティにアクセスできます。両方とも同じ結果が返されますが、アクセスメカニズムは異なります。
「プッシュ」タグは少数の文字を入力するだけで、実質的な価値は見えません。
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="pushTagAction" class="com.mkyong.common.action.PushTagAction" > <result name="success">pages/push.jsp</result> </action> </package> </struts>