Struts2 <s:include>標籤範例


Struts2 <s:include>標籤用於直接包含JSP或HTML頁面到當前頁面。參見下面<s:include>標籤的例子。

1. 動作

一個簡單的動作類只做轉發任務。

IncludeTagAction.java

package com.tw511.common.action;

import com.opensymphony.xwork2.ActionSupport;
 
public class IncludeTagAction extends ActionSupport{
	
	public String execute() throws Exception {
		
		return SUCCESS;
	}
}

2. <s:include>標籤範例

它顯示了使用<s:include>標籤包括一個yiibai.jsp頁面到當前 include.jsp 頁面。

include.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
 
<body>
<h1>Struts2 <s:include>標籤範例</h1>

<s:include value="/pages/yiibai.jsp"></s:include>

</body>
</html>

yiibai.jsp

<html>
<head>
</head>
 
<body>
</div><h2>Message from yiibai.jsp</h2>

</body>
</html>

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="includeTagAction" 
			class="com.tw511.common.action.IncludeTagAction" >
			<result name="success">pages/include.jsp</result>
		</action>
		
	</package>
</struts>

4. 範例

http://localhost:8080/struts2includetag/includeTagAction.action

在瀏覽器中存取上面網址,結果輸出如下:

參考

  1. Struts2 <s:include>標籤文件

程式碼下載 - http://pan.baidu.com/s/1i3Iv1rR