位置:首页 > Java技术 > Struts2在线教程 > Struts2 <s:include>标签示例

Struts2 <s:include>标签示例

Struts2 <s:include>标签用于直接包含JSP或HTML页面到当前页面。参见下面<s:include>标签的例子。

1. 动作

一个简单的动作类只做转发任务。

IncludeTagAction.java

package com.gitbook.netmon.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.gitbook.netmon.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