DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Struts 2 Hello World Tutorial

Struts 2 Hello World Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 14, 12 · Tutorial
Like (1)
Save
Tweet
Share
119.80K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial we will see how to create a simpe Struts 2 Hello World Application. The following files are needed to create a Hello World Application.

  • web.xml
  • struts.xml
  • HelloWorld.java
  • index.jsp
  • success.jsp

The following picture shows the directory structure of the Hello World application.

web.xml

web.xml is used to configure the servlet container properties of the hello world appliation. The filter and the filter-mapping elements are used to setup the Struts 2 FilterDispatcher. The filter is mapped to the URL pattern "/*". This means all the incoming request that targets to the Struts 2 action will be handled by FilterDispatcher class.

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

The gateway for our hello world application is index.jsp file. The index.jsp file should be mentioned in web.xml as shown above.

struts.xml

The entry point to the XML declarative architecture is struts.xml file. The struts.xml file contains the following action mapping.

<struts>
    <package name="default" extends="struts-default">
        <action name="HelloWorld" class="vaannila.HelloWorld">
            <result name="SUCCESS">/success.jsp</result>
        </action>
    </package>
</struts>

index.jsp

The Struts 2 UI tags are simple and powerful. To use the struts tags in the jsp page the following taglib directive should be included.

<%@taglib uri="/struts-tags" prefix="s" %>

<html>
<head>
<title>Hello World</title>
</head>
<body>
    <s:form action="HelloWorld" >
        <s:textfield name="userName" label="User Name" />
        <s:submit />
    </s:form>
</body>
</html>

HelloWorld.java

As you see the HelloWorld class is very simple. It contains two properties one for the user name and the other for displaying the message.

public class HelloWorld {

    private String message;

    private String userName;

    public HelloWorld() {
    }

    public String execute() {
        setMessage("Hello " + getUserName());
        return "SUCCESS";
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}

In the execute() method of the HelloWorld action we compose the message to be displayed. Note we need not have a seperate form bean like struts 1 to access the form data. We can have a simple java class as action. The action need not extend any class or implement any interface. The only obligation is that you need to have an execute() method which returns a String and has a public scope.

success.jsp

In the success page we display the "Hello User" message using the property tag.

<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
    <h1><s:property value="message" /></h1>
</body>
</html>

Extract the downloaded files into the webapps folder of Tomcat. Start the Tomcat server. Type the following url in the browser "http://localhost:8080/Example1/index.jsp". The index page will be displayed.

Enter the user name and submit the form. Hello user name message will be displayed.

You can download the Struts 2 Hello World example by clicking the download link.

Source :Download
Source + Lib :Download
application Property (programming) Form (document) Apache Tomcat Download Filter (software) Data (computing) Strings Extract Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • NoSQL vs SQL: What, Where, and How
  • Spring Boot, Quarkus, or Micronaut?
  • Introduction to Containerization
  • The Power of Docker Images: A Comprehensive Guide to Building From Scratch

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: