Make a Directory Structure
In the first step, create a directory structure as shown in the image below:
Make an input page (index.jsp)
Using struts UI tags, we can design a form with the assistance of the JSP page. To utilise UI tags, we must supply uri/struts-tags. We used s:form to make a form, s:textfield to make a textfield, and s:form to make a submit button. The complete code for index.jsp is shown below:
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:form action="Login">
<s:textfield name="id" label="Email Id"></s:textfield>
<s:textfield name="first_name" label="First Name"></s:textfield>
<s:textfield name="password" label="Password"></s:textfield>
<s:submit value="save"></s:submit>
</s:form>
Provide the Controller entry
In this step, we will provide the Controller entry in the web.xml file. The controller is the StrutsPrepareAndExecuteFilter class. As we know, Struts2 uses a filter for the controller, which is given internally by the struts framework. The code for the web.xml file is shown below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<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>
Make the action class.
We must design an introductory bean class. In struts2, action is a POJO (Plain Old Java Object) with a different method called execute() that is automatically triggered by the struts framework. Let's name our action class Login.java. The code for Login.java is given below:
package com.info;
public class Login
{
private int id;
private String first_name;
private String password;
public int getId ()
{
return id;
}
public void setId (int id)
{
this.id = id;
}
public String getFirst_Name ()
{
return first_name;
}
public void setFirst_Name (String first_name)
{
this.first_name = first_ name;
}
public float getPassword ()
{
return password;
}
public void setPassword (String password)
{
this.password = password;
}
public String execute ()
{
return "success";
}
}
Mapping the request
In this step, we will map the request to the action in the (struts.xml) file and specify the view components. The struts.xml file offers information about the action to the struts framework, which then decides on the right result invocation based on the information. The code for struts.xml is shown below:
?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>
<package name="default" extends="struts-default">
<action name="Login" class="com.Info.Login">
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>
Make view components
To display the information obtained through the use of struts tags. So it's just a view component. The s:property function retrieves the values of action objects such as id, first name, and password. In this step, we will create all the view components, create a file with the name welcome.jsp and paste the following code.
<%@ taglib uri="/struts-tags" prefix="s" %>
Login Id:<s:property value="id"/><br/>
First Name:<s:property value="first_name"/><br/>
Password:<s:property value="password"/><br/>
Load the jar files
To execute the application, we'll need the essential jar files. All of the jar files are available here. Download it and save it in your project's lib folder. You can download the jar files from here.
Start the server and deploy the project.
In the final step of creating our Struts 2 application, we need to start the server and deploy the application.
Must Read Apache Server
Frequently Asked Questions
What is Struts2?
Apache Struts2 is a Java open source framework for developing web applications. The OpenSymphony WebWork framework serves as the foundation for Struts2. It's vastly enhanced over Struts1, making it more adaptable, simple to use, and extensible. Struts2's main components are Action, Interceptors, and Result pages.
Why are Struts used?
Struts is an open-source framework that utilises a Model, View, Controller (MVC) architecture that extends the Java Servlet API. It allows you to build stable, extendable, and adaptable online applications using standard technologies like JSP pages, JavaBeans, resource bundles, and XML.
In Struts2, which class is the Front Controller?
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter is the Front Controller class in Struts2, where all request processing begins. Earlier versions of Struts2 used the Front Controller class org.apache.struts2.dispatcher.FilterDispatcher.
Struts2 interceptors implement which design pattern?
Interceptors in Struts2 are based on the intercepting filter design pattern. Interceptor invocation in an interceptor stack closely mimics the Chain of Responsibility design paradigm.
What is Struts XML?
The struts.xml file contains the configuration data that will be changed when actions are generated. DevMode = false, as well as other default parameters provided in property files, can be altered for an application that uses this file, such as struts.
Conclusion
This article has extensively discussed the steps to create the Struts 2 application.
Do you not feel eager to read/explore additional information on the subject of Struts 2 after reading about the steps to create the Struts 2 application? See the Struts 2 documentation, Struts Tutorial for beginners, Struts 2 Intro, and 15 Best Java frameworks to use in 2022-Coding Ninjas.
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!