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.
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.
- Create a web project
- Add struts 2 capabilities.
- Create input page (index.jsp).
- Create the action class (Product.java).
- Map the request with the action in the (struts.xml) file and define the view components.
- Create view components (welcome.jsp).
- Start the server and deploy the project.
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.