Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Generator Tag
3.
When to use the generator tag?
4.
Generator Tag Example
4.1.
Create action class
4.2.
Creating View file
4.2.1.
CodingNinja.jsp file 
4.3.
Configuration files
4.3.1.
struts.xml file 
4.3.2.
Web.xml file 
4.4.
Output
5.
Frequently Asked Questions
5.1.
What is struct2?
5.2.
What is the default value set to executeResult?
5.3.
In struts, what is form bean?
5.4.
What is a set in java?
5.5.
What is an ORM tool?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

The Generator Tag

Author Saksham Gupta
0 upvote

Introduction 

Struts is frequently used while creating Java-based apps. It is the foundation for all tasks, including creation, distribution, and upkeep. Struts is the name of the MVC (Model View Controller)-based framework. In Strut, the generator tag is located under the Control tags.

Struts2

 

Generator Tag

In struts, the collection of elements provided by the val attribute is iterated over using the generator tag. It enables us to repeatedly go through each part and apply the necessary action, such as printing each piece, as needed.

When to use the generator tag?

Often we come across situations where we have to create a list or array on the fly and iterate through the list. You could create the list or array using scriptlets, or you can use the generator tag

Generator Tag Example

Create action class

package com.CodingNinjas.struts2;

public class Ninja
{
	private String n;
	public String execute() throws Exception{
		return "success";
	}
	public String getN(){
		return n;
	}
	public void setN(String n){
		this.n = n;
	}
}


After this, we will create the view file

Creating View file

JSP Files

CodingNinja.jsp file
 

<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>


<html>

   <head>
      <title></title>
   </head>
   
   <body>
      <h2>Example of Generator Tag</h2>
      <h3>The courses offered here are:</h3>
      <s:generator val = "%{ ‘Machine Learning, C++ with Data Structures and Algorithms, Full Stack Web Development’}" count = "3" separator = ",">
         <s:iterator>
            <s:property /><br/>
         </s:iterator>
      </s:generator>    
   </body>
   
</html>


To parse the string that comprises a comma-separated list of some of the courses that are offered by us, we are constructing a generator tag in this instance. The separator is "," and we want all three values in the list; we inform the generator tag.

The count would be set to 2 if we were only interested in the first two values. We used the iterator to cycle over the values produced by the generator tag in the generator tag's body, and we printed the value of the property.

Now let's have a look at the configuration files

Configuration files

XML Files

struts.xml file
 

<?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 = "CodingNinja" extends = "struts-default">
     
      <action name = "Ninja" 
         class = "com.CodingNinja.struts2.Ninja" 
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

 

Web.xml file
 

<?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" 
   xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   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>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. After that, deploy this WAR to the webapps directory of Tomcat. Launch the Tomcat server, then attempt to access the URL http://localhost:8080/CodingNinjaStruts2/Ninja.action. This will result in the screen shown below:

Output

Output

Frequently Asked Questions

What is struct2?

A well-liked and established web application framework based on the MVC design paradigm is Struts2. The Struts architecture has been completely rewritten in Struts2, which is more than just a new version of Struts 1.

What is the default value set to executeResult?

The default value of executeResult attribute of the action tag is false. If you want to render the results of action directly from the view page to the current page, it must be set to true.

In struts, what is form bean?

A Java bean, known as a form bean, is filled with the information from an HTML form created using Struts when a user presses the Submit button on that form. A form bean's characteristics correspond to every field on the form. The bean properties are immediately filled in after the form is submitted.

What is a set in java?

A set in java is a data structure used to store unique elements. Duplications are not allowed in a set collection. To be able to add an element to a set, it must implement both the equals() and hashCode() methods. 

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.

Conclusion

In this blog, we have briefly learned about the struts and generator tag.

To know more about the struts and other statements. You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in pygameCompetitive ProgrammingJavaScriptSystem Design, and many more! 

If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

 

Thank You
Live masterclass