Struts 2アクションタグの例
ダウンロードする – リンク://wp-content/uploads/2010/07/Struts2-Action-Tag-Example.zip[Struts2-Action-Tag-Example.zip]
Struts 2 ”
action
“タグは、JSPページからアクションクラスを直接呼び出すために使用されます。 ”
executeResult
“属性がtrueに設定されている場合、結果ページの内容は現在のページに直接表示されます。
これは、完全な例を使用して最もよく説明されています。
1.アクション
結果を別の結果ページに転送するメソッドはほとんどありません。
package com.mkyong.common.action;
import com.opensymphony.xwork2.ActionSupport;
public class ActionTagAction extends ActionSupport{
public String execute() {
return SUCCESS;
}
public String sayHello(){
return "sayHello";
}
public String sayStruts2(){
return "sayStruts2";
}
public String saySysOut(){
System.out.println("SysOut SysOut SysOut");
return "saySysOut";
}
}
2.アクションタグの例
JSPページは ”
action
“タグの使用を示します。アクションタグに
executeResult = “true”
が指定されている場合、メソッドが実行され、結果ページが直接表示されます。それ以外の場合は、メソッドを実行するだけで、結果ページは表示されません。
-
action.jsp **
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 action tag example</h1> <ol> <li> Execute the action's result, render the page here. <s:action name="sayHelloAction" executeResult="true"/> </li> <li> Doing the same as above, but call action's sayStruts2() method. <s:action name="sayHelloAction!sayStruts2" executeResult="true"/> </li> <li> Call the action's saySysOut() method only, no result will be rendered, By defautlt, executeResult="false". <s:action name="sayHelloAction!saySysOut"/> </li> </ol> </body> </html>
-
sayHello.jsp **
<html>
<head>
</head>
<body>
<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>Hello Hello Hello ~ from sayHello.jsp</h2>
</body>
</html>
-
sayStruts2.jsp **
<html> <head> </head> <body> <h2>Struts 2 Struts 2 Struts 2 ~ from sayStruts2.jsp</h2> </body> </html>
-
saySysOut.jsp **
<html> <head> </head> <body> <h2>SysOut SysOut SysOut ~ from saySysOut.jsp</h2> </body> </html>
3. struts.xml
-
executeResult ** エフェクトを示すために結果名を宣言しました。
<?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="actionTagAction"
class="com.mkyong.common.action.ActionTagAction" >
<result name="success">pages/action.jsp</result>
</action>
<action name="sayHelloAction"
class="com.mkyong.common.action.ActionTagAction"
method="sayHello">
<result name="sayHello">sayHello.jsp</result>
<result name="sayStruts2">sayStruts2.jsp</result>
<result name="saySysOut">saySysOut.jsp</result>
</action>
</package>
</struts>
4.デモ
-
出力**
image =//wp-content/uploads/2010/07/Struts2-Action-Tag-Example.jpg[Struts 2アクションタグの例、title = “Struts2-Action-Tag-Example”、width = 639、height = 390]