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.
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
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
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>
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
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.
If you want to test your competency in coding, you may check out the mocktest 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!