Struts 2 include tag

The include tag in struts is used to include some other resource like other jsp or servlet file in the existing jsp page. In the struts web application, it is used to include another resource into existing one like <jsp:include>. In any case, if we want to use any output multiple times in multiple files, include tag is used to include it in the existing resource.

Struts Data tags- Include tag Example:

Create the index.jsp file as first jsp file:

The index.jsp page has the heading Generator Tag Example to view days of the week with the display name Get days of the week. It is used to display All days of the week according to the specified count. The val attribute specifies days of the week and displays each of them on the next line. We can change the value of the count according to the requirement. Iterator is further used to print values set by the generator tag.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Index page </title>
</head>
<body>
<h3> Generator Tag Example </h3>
<br>
      <h3> The days of the Week : </h3>
<br>
      <s:generator val = "%{'Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday'}" count = "7" separator = ",">
         <s:iterator>
            <s:property /> 
           <br/>
         </s:iterator>
      </s:generator>
</body>
</html>

Create welcomes.jsp file to include index.jsp file:

The welcome.jsp page is used to include output of other page in welcome page. It shows content of welcome.jsp page that is An example of the include tag in welcome page with output of index.jsp file that is days of week, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday included in it.

<%@ page language ="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding ="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>
   <head>
      <title> welcome page </title>
   </head>
   
   <body>
      <h3> An example of the include tag in welcome page  </h3>
      <br>
      <s:include value = "index.jsp"/>
   </body>
</html>

Create the class to include execute method:

The Demo class contains execute() method which returns a success string if all the things are executed properly.

public class Demo 
{ 

public String execute() throws Exception 
{
      return "success";
}
}

Construct struts.xml file:

In the struts.xml file, make the entry of the action class and link for it and result from pages. The result determines what browser will display after the execution of the action. Results have optional names like success and error. We have made the entry of two Actions namely hello and welcome and called different jsp pages on them. We specified class name and method name with action.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
   <constant name = "struts.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">


      <action name = "hello" 
         class = "Demo " 
         method = "execute">
         <result name = "success"> /index.jsp </result>
      </action>
      
      <action name = "welcome" 
         class = "Demo" 
         method = "execute">
         <result name = "success">  /welcome.jsp </result>
      </action>


   </package>
</struts>

web.xml file is created inside WEB-INF folder in WebContent folder:

The web.xml file in the WEB-INF folder specify how elements are processed. The entry of FilterDispatcher or StrutsPrepareAndExecuteFilter is done in the web.xml file. /* specifies all urls will be parsed. This task is done by struts filter.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name> StrutsInclude </display-name>
  <welcome-file-list>
    <welcome-file> index.html </welcome-file>
    <welcome-file> index.htm </welcome-file>
    <welcome-file> index.jsp </welcome-file>
    <welcome-file> default.html </welcome-file>
    <welcome-file> default.htm </welcome-file>
    <welcome-file> default.jsp </welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>  struts2  </filter-name>
    <filter-class>  
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
   </filter-class>
  </filter>
  <filter-mapping>
    <filter-name> struts2 </filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Output:

In order to run the application, right-click on the project -> Click on the option Run As -> then select Run on Server. After that run the url, http://localhost:8080/StrutsInclude/welcome.action. It will show welcome.jsp page displaying An example of the include tag in welcome page and content of the index.jsp page included in it that is Generator Tag Example and The days of the Week: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday on separate lines.

Struts Data tags- Include tag