Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Steps to create a Struts 2 application
2.1.
Create a web project
2.2.
Add struts 2 capabilities
2.3.
Create input page (index.jsp)
2.4.
Create the action class (Product.java)
2.5.
Mapping the request in the file and defining the view components
2.6.
Creating the view components (welcome.jsp)
2.7.
Start the server and deploy the project
3.
Frequently Asked Questions
3.1.
What are the Struts 2 core components?
3.2.
What are the differences between Struts 1 and Struts 2?
3.3.
Is Struts 2 a standalone application?
3.4.
What is the current version of struts?
3.5.
What is Struts xml?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Create a struts 2 application in MyEclipse

Introduction

Struts 2 is a second-gen web application framework based on the Model-View-Controller (MVC) design pattern. The foundation of Struts 2 is based on the best practices and established, widely-accepted design patterns.

Struts 2 Image

In the following article, we will learn how to build a working application using the Struts 2 framework.

Steps to create a Struts 2 application

We'll use the MyEclipse IDE to build the Struts 2 application. Because MyEclipse offers the jar files, we don't need to worry about them.

You need to follow these steps to create the struts 2 application.

  1. Create a web project
  2. Add struts 2 capabilities.
  3. Create input page (index.jsp).
  4. Create the action class (Product.java).
  5. Map the request with the action in the (struts.xml) file and define the view components.
  6. Create view components (welcome.jsp).
  7. Start the server and deploy the project.
Lets build image

Create a web project

To create a web project, click on the File menu - new - project - web project - write the project name e.g. firststruts - finish.

Add struts 2 capabilities

To add the struts 2 capabilities, select your project - click on the myeclipse menu - add project capabilities - add struts capabilities.

Select the 2.1 and /* as the url pattern - finish.

Create input page (index.jsp)

It creates a form with fields using struts core tags.

index.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>  
<s:form action="product">  
<s:textfield name="id" label="Product Id"></s:textfield>  
<s:textfield name="name" label="Product Name"></s:textfield>  
<s:textfield name="price" label="Product Price"></s:textfield>  
<s:submit value="save"></s:submit>  
</s:form>  

Create the action class (Product.java)

It is a straightforward action class with setters and getters for its properties. Defining the business logic, this class also includes the execute method.

Product.java

package com.codingNinjas;  
public class Product {  
private int id;  
private String name;  
private float price;  
public int getId() {  
    return id;  
}  
public void setId(int id) {  
    this.id = id;  
}  
public String getName() {  
    return name;  
}  
public void setName(String name) {  
    this.name = name;  
}  
public float getPrice() {  
    return price;  
}  
public void setPrice(float price) {  
    this.price = price;  
}  
  
public String execute(){  
    return "success";  
}  
}  

Mapping the request in the file and defining the view components

The action and view components are registered in this XML file.

struts.xml

<?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="product" class="com.javatpoint.Product">  
<result name="success">welcome.jsp</result>  
</action>  
  
</package>  
</struts>      

Creating the view components (welcome.jsp)

This jsp page displays the information set in the action object.

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>  
  
Product Id:<s:property value="id"/><br/>  
Product Name:<s:property value="name"/><br/>  
Product Price:<s:property value="price"/><br/>  

Start the server and deploy the project

For starting the server and deploying the project, right-click on your project - Run As - MyEclipse server application.

Frequently Asked Questions

What are the Struts 2 core components?

The Action, Interceptor, and Result pages are the three main Struts2 building blocks. Action classes can be created in various methods with Struts2 and configured using Struts.

What are the differences between Struts 1 and Struts 2?

Struts 1 allows for distinct Request Processors (lifecycles) for every module, but every module's Actions must use the same lifecycle. Through interceptor stacks, Struts 2 enables the creation of various lifecycles for each Action. As required, custom stacks can be constructed and applied to multiple Actions.

Is Struts 2 a standalone application?

Since Struts2 is an MVC-based framework, all Struts2 applications must include the following three elements: We will build our business logic in the Action class, a POJO class (meaning it is not a part of any type hierarchy and may be used as a standalone class).

What is the current version of struts?

The most recent version of the Struts 2 framework, Struts 2.5. 22, was made available on November 29, 2019. The 2.5 Struts.

What is Struts xml?

The configuration data you will change when actions are created is contained in the struts.xml file. DevMode = false, and other default options given in property files can be overridden for an application using this file, such as struts.

Conclusion

In this article, we built an application using the Struts 2 framework. We began with a brief introduction to Struts 2, followed by steps for building the project. 

After reading about building a project in Struts 2, refer to Struts 2 documentationStruts Tutorial for beginnersStruts 2 Intro, and 15 Best Java frameworks to use in 2022-Coding Ninjas or a deeper understanding of Struts 2 development and other related topics.

Thank you Image
Live masterclass