Back to: Java Struts Tutorials
Steps to Create Struts 2 Application
In this article, I am going to discuss the Steps to Create a Struts 2 Application with Examples. Please read our previous article where we discussed Struts 2 Environment Setup.
Steps to Create a Struts 2 Basic Application
In this project, we will be programming a simple application using struts2. Follow the steps to do so:
Step 1: Create a new maven project in Eclipse. We will use Maven to manage the dependencies of struts2. This may, we do not have to import JARs.
Remember to set the packaging to war. This is because we shall be deploying a web application.
Finally, click on the finish button to create the project. The following project directories need to be created:
Step 2: Initially, the pom.xml file should contain the following content:
Modify pom.xml to add the required dependencies and the build configuration:
Step 3: In the src/main/webapp directory, create a new directory called WEB-INF. In this directory, create a new file called web.xml. This file is responsible for implementing struts2 in the web application. Add the following content to the file:
Step 4: In the src/main/webapp directory, create a file called index.html. This file is a basic HTML page that takes the name of the user. Add the following content to the file:
Step 5: In the src/main/java directory, create a new class file called HelloAction.java. This file is a basic POJO class. The code in the pojo class is called when the user clicks on the submit button on the aforementioned index.html page. Add the following content to the file:
Step 6: In the src/main/webapp directory, create a file called results.jsp. This file is supposed to print the data obtained from the HelloAction class. Add the following content to the file:
Step 7: In the src/main/resources directory, create a file called struts.xml. This file is supposed to configure struts2. Add the following content to the file:
Step 8: Compile and execute the program on the Tomcat server. The following page must open in your default web browser:
Enter a name. In my case, I have entered John. Then, click on submit. The following webpage must load:
Congratulations! You have successfully completed an application in struts2.
The complete code
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dotnet</groupId> <artifactId>strutex</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>6.1.2.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>6.1.2.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-taglib</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>6.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> </project>
src/main/webapp/WEB-INF/web.xml
<?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" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/web-app_3_0.xsd" id="apache-struts-config-example" version="3.0"> <display-name>Struts Config XML</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
src/main/webapp/index.html
<!DOCTYPE html> <html> <head> <title>Example of Struts</title> </head> <body> <form action="hello.action"> <p>What is your name?</p> <input type="text" name="userName"/> <input type="submit" value="submit"/> </form> </body> </html>
src/main/java/com/dotnet/HelloAction.java
package com.dotnet; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; @Action("/hello") @Result(name = "success", location = "/results.jsp") public class HelloAction { public String execute() { System.out.println("User name: " + userName); return "success"; } private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
src/main/webapp/results.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <%@ page contentType="text/html; charset=UTF-8" %> <!DOCTYPE html> <html> <head> <title>Results page</title> </head> <body> <p>Hello <s:property value="userName"/>!</p> </body> </html>
src/main/resources/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="hello" class="com.dotnet.HelloAction" method="execute"> <result name="success">/results.jsp</result> </action> </package> </struts>
In the next article, I am going to discuss Creating First Struts 2 Application. Here, in this article, I try to explain the Steps to Create the Struts 2 Application and I hope you enjoy this Steps to Create the Struts 2 Application article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.