Open In App

Hello World Example using Stuts2

Last Updated : 31 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Apache Struts 2 is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model–view–controller architecture. In this article, we will see how we can create a simple Hello World application in Struts 2. We are going to use Eclipse IDE so that all the required components will be created under a Dynamic Web Project.

Prerequisites:

Now, if the above conditions are fulfilled and your web application is configured with the struts2 then follow the steps given below.

Step by Step Implementation

1] web.xml file

Create a web.xml file under the webapp/WEB-INF folder and copy the following code in web.xml. This file is created by default if you have checked the Generate web.xml deployment descriptor option at the time of creating a dynamic web project. Now copy the following code in the web.xml file.

XML




<?xml version="1.0" encoding="UTF-8"?>
        xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
        id="WebApp_ID" version="4.0">
    
  <display-name>Struts2 Hello World Example</display-name>
    
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
    
  <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>


2] Result Pages

We need JSP pages to display the final result, this page will be called by the Struts 2 framework where a predefined action will be called and this mapping is defined in the struts.xml file. We are using Struts 2 UI tags to create our JSP pages. To create a JSP file, right-click on the webapp folder in the project explorer and select New >JSP File. Refer to the below image

 

login.jsp file

A JSP login page to display the username and password input fields and submit button. Copy the following code into the login.jsp file.

HTML




<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
  
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Login Page</title>
</head>
  
<body bgColor="lightBlue">
    <s:form action="Welcome">
        <s:textfield name="userName" label="User Name" />
        <s:password name="password" label="Password" />
        <s:submit value="Say Hello" />
    </s:form>
</body>
</html>


welcome.jsp file

Create a welcome.jsp file under the webapp folder. This file displays a welcome message to the user. Copy the following code into the file.

HTML




<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
  
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Welcome Page</title>
</head>
  
<body>
    <h2> Hello World Example in Struts 2</h2>
    <h3> Hello <s:property value="userName"/> !!! </h3>
</body>
  
</html>


3] Action class: WelcomeAction.java

A Struts2 Action class is used to declare all the business logic inside. Our application has only one Action class whereas our WelcomeAction class extends ActionSupport class. It is good to extend ActionSupport class as it provides a default implementation for most common tasks. Create a WelcomeAction.java file and Copy the following code into the file.

Java




package com.GeeksforGeeks.struts2;
  
import com.opensymphony.xwork2.ActionSupport;
  
public class WelcomeAction extends ActionSupport {
   
    // Java bean to hold the form parameters
    private String userName;
   
    public String getUserName() {
        return userName;
    }
   
    public void setUserName(String userName) {
        this.userName = userName;
    }
      
    // All struts logic goes here
    public String execute() {
        return SUCCESS;
    }
  
}


4] Struts configuration file: struts.xml

We need a mapping between URL to action mapping. The struts.xml maps the WelcomeAction class, and the welcome.jsp together. The mapping tells the Struts 2 framework which class will respond to the user’s action (the URL), which method of that class will be executed, and what view to render based on the String result that method returns. So we will now create a struts.xml file under the src/main/java folder. Copy the following code into struts.xml:

XML




<?xml version="1.0" encoding="UTF-8" ?>
  
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      
<struts>
  
    <constant name="struts.custom.i18n.resources" value="WelcomeAction" />
    
    <package name="default" extends="struts-default" namespace="/">
        <action name="Welcome" class="com.GeeksforGeeks.struts2.WelcomeAction" >
            <result name="success">welcome.jsp</result>
            <result name="input">login.jsp</result>
        </action>
    </package>
      
</struts>


Your project structure should now look like this:

Project Structure

 

5] Run your application

To run your application in Eclipse: Right-click on project -> run as -> run on server

 

Output:

When we run our application, you will see a web page opened on your Eclipse IDE like this:

Output

 

After you enter the username and password you’ll get the following screen:

Output

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads