Introduction
The infrequently used Struts include tag is similar to the jsp include tag. We have seen how to use the <s:action> tags to include a struts action's output into a jsp. There is a small difference in the <s:include> tag. It enables you to incorporate any additional resource—other than a struts action—into a jsp, including the output of a servlet or jsp. It functions identically like the <jsp:include> from a technical standpoint. Still, it allows you to send arguments to the included file and is a component of the Struts framework.
Example
Create Action Class
package com.coding.ninjas.struts2;
public class HelloWorldAction
{
private String name;
public String execute() throws Exception
{
return "success";
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
Create views
Create a file with the name HelloWorld.jsp and put the content below.
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Example of Generator Tag</h2>
<h3>The colours of rainbow:</h3>
<s:generator val = "%{'Violet,Indigo,Blue,
Green,Yellow,Orange,Red '}" count = "7" separator = ",">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
</body>
</html>
Create a file with the named employee.jsp and put the below content into it.
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employees</title>
</head>
<body>
<p>An example of the include tag: </p>
<s:include value = "HelloWorld.jsp"/>
</body>
</html>
Configuration Files
Create a file with the name struts.xml and put the below content into it.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC>
<struts>
<constant name = "struts.devMode" value = "true" />
<package name = "helloworld" extends = "struts-default">
<action name = "hello"
class = "com.coding.ninjas.struts2.HelloWorldAction"
method = "execute">
<result name = "success">/HelloWorld.jsp</result>
</action>
<action name = "employee"
class = "com.coding.ninjas.struts2.Employee"
method = "execute">
<result name = "success">/employee.jsp</result>
</action>
</package>
</struts>
Create a file with the name web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
To produce a War file:
- Perform right-click on the project name and select Export > WAR File.
- Deploy this WAR to the webapps directory of Tomcat.
- Launch the Tomcat server, then attempt to access it.