位置:首頁 > Java技術 > Struts2教學 > Struts2 <s:action>標簽示例

Struts2 <s:action>標簽示例

Struts2 的“action”標簽是用來直接從JSP頁麵中調用Action類。如果“executeResult”屬性設置為true,則結果頁的內容將直接在當前網頁渲染。


這是用一個完整的例子很好地說明:

1. 動作

Action類有幾個方法用來轉發不同結果的結果頁麵。

ParamTagAction.java

package com.gitbook.netmon.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. <s:action>標簽示例

下麵的JSP頁麵顯示如何使用“action”標簽。如果 executeResult=”true”,動作標簽被指定方法執行且結果頁麵將直接顯示; 否則,它隻是執行的方法,結果頁麵不會顯示出來。

action.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>struts2 action標簽示例<title>
</head>
 
<body>
<h1>Struts2 action標簽示例</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>
<div><div class="ads-in-post hide_if_width_less_800">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 728x90 - After2ndH4 -->
<ins class="adsbygoogle hide_if_width_less_800" 
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-2836379775501347"
     data-ad-slot="3642936086"
	 data-ad-region="yiibairegion"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div></div><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

聲明一些結果名稱來演示 ExecuteReuslt 的效果。
<?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.gitbook.netmon.action.ActionTagAction" >
		<result name="success">pages/action.jsp</result>
	</action>
		
	<action name="sayHelloAction" 
		class="com.gitbook.netmon.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. 示例

http://localhost:8080/struts2actiontag/actionTagAction.action

在瀏覽器中打開上麵的URL,顯示結果如下圖:

參考


  1. Struts2 <s:action>標簽文檔

代碼下載 - http://pan.baidu.com/s/1kT8NTUf