Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Example
2.1.
Create Action Class
2.2.
Create views
2.3.
Configuration Files
3.
Frequently Asked Questions
3.1.
What are structs in JAVA?
3.2.
What does struts2 mean in action?
3.3.
What is an ORM tool?
3.4.
Write about Struts XML.
3.5.
What is a set in java?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

The Include Tag

Author Manan Singhal
0 upvote

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.

Structs 2

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.

 

Frequently Asked Questions

What are structs in JAVA?

Structs is an open-source Java framework used for developing JAVA EE web applications. Many developers use it as it allows them to build maintainable, flexible, and extensible web applications faster. 

What does struts2 mean in action?

The action tag allows programmers to execute the action specified on the view page. They can do this by giving the activity a name. They can set the "executeResult" option to "true" to render the result directly in the view.

What is an ORM tool?

An object-relational mapping (ORM) tool makes it easier to create, manipulate, and store objects and makes them more accessible. It offers a means of translating programming languages (such as Python and Java) into objects that can be mapped to a SQL database.

Write about Struts XML.

The struts.xml file contains the configuration information that will modify as actions are generated. For an application using this file, such as struts, DevMode = false, and other default choices provided in property files can be modified.

What is a set in java?

A data structure called a set is used in Java to contain distinct components. There can be no duplicates in a set collection. The equals() and hashCode() methods must be implemented for an element to be added to a set.

Conclusion

In this article, we learned the include tags concept. We have also discussed the concept of the include tags using an example.

I hope you would have gained a better understanding of these topics now!

Refer to our guided paths on Coding Ninjas Studio to learn about Data Structure and Algorithms, Competitive Programming, JavaScript, etc. Enroll in our courses and refer to our mock test available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Thank You
Live masterclass