ダウンロードする – リンク://wp-content/uploads/2010/07/Struts2-Set-Tag-Example.zip[Struts2-Set-Tag-Example.zip]
Struts 2 ”
set
“タグは、指定されたスコープ(アプリケーション、セッション、リクエスト、ページ、アクション)内の変数に値を割り当てるために使用され、アクションはデフォルトスコープです。完全な ”
set
“タグの例を参照してください:
”
value
“は、ハードコーディングされた文字列、プロパティ値、または参照可能なものだけを意味します。
1.アクション
「msg」プロパティを持つアクションクラス。
package com.mkyong.common.action; import com.opensymphony.xwork2.ActionSupport; public class SetTagAction extends ActionSupport{ private String msg = "Struts 2 is a funny framework"; public String getMsg() { return msg; } public String execute() throws Exception { return SUCCESS; } }
2.タグの例を設定する
”
set
“タグの使用方法を示します。
-
set.jsp **
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 set tag example</h1> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-2836379775501347" data-ad-slot="8821506761" data-ad-format="auto" data-ad-region="mkyongregion"></ins> <script> (adsbygoogle = window.adsbygoogle ||[]).push({}); </script><h2>1. <s:set var="varMsg" value="msg"/></h2> <s:set var="varMsg" value="msg"/> <s:property value="varMsg"/> <h2>2. <s:set var="varUrl" value="%{'/'}"/></h2> <s:set var="varUrl" value="%{'/'}"/> <s:property value="varUrl"/> </body> </html>
-
それがどのように働きますか?**
-
1。 <s:set var = “varMsg” value = “msg”/>
+アクションの
getMsg()
メソッドを呼び出し、返された値を ”
varMsg ** “という名前の変数に代入します。 -
2。 <s:set var = “varUrl” value = “%\ {‘/’}”/>
文字列をハードコードし、 ”
varUrl ** “という名前の変数に代入します。 -
プロパティ値ではなく変数に値を割り当てる**
例えば、
public class SetTagAction extends ActionSupport{ private String msg; public String setMsg(String msg) { this.msg = msg; } ...
<s:set var="msg" value="%{'this is a message'}"/>
Struts 2の多くの開発者は、 ”
set
“タグ
var = “msg”
は
setMsg()
メソッドを介して関連するアクションクラスに値を割り当てると考えました。
-
これは間違っています
setタグは
setMsg()
メソッドを呼び出さず、アクションのプロパティ値ではなく、 ”
msg ** “という名前の変数にのみ “value”を割り当てます。
3. 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="setTagAction" class="com.mkyong.common.action.SetTagAction" > <result name="success">pages/set.jsp</result> </action> </package> </struts>
5.デモ
-
出力**
image =//wp-content/uploads/2010/07/Struts2-Set-Tag-Example.jpg[Struts 2 setタグの例、title = “Struts2-Set-Tag-Example”、width = 640、height = 344]