Struts 2 include tag example
Struts 2 “include” tag is used to include JSP or HTML page directly into the current page. See below example for the “include” tag demonstration.
1. Action
A simple Action class to do the forward task only.
IncludeTagAction.java
package com.mkyong.common.action; import com.opensymphony.xwork2.ActionSupport; public class IncludeTagAction extends ActionSupport{ public String execute() throws Exception { return SUCCESS; } }
2. include tag example
It shows the use of include” tag to include a “mkyong.jsp” page into the current page “include.jsp“.
include.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 include tag example</h1> <s:include value="/pages/mkyong.jsp"></s:include> </body> </html>
mkyong.jsp
<html> <head> </head> <body> <h2>Message from mkyong.jsp</h2> </body> </html>
3. struts.xml
Link it ~
<?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.mkyong.common.action.IncludeTagAction" > <result name="success">pages/include.jsp</result> </action> </package> </struts>
4. Demo
http://localhost:8080/Struts2Example/includeTagAction.action
Output







