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
  1. DZone
  2. Coding
  3. Frameworks
  4. Struts Hello World Tutorial

Struts Hello World Tutorial

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

Join the DZone community and get the full member experience.

Join For Free
Lets say a quick hello to struts. Struts follows MVC 2 pattern. The following files are needed to create a hello world application.
  • index.jsp
  • helloWorld.jsp
  • web.xml
  • struts-config.xml
  • HelloWorldAction.java
  • HelloWorldActionForm.java


web.xml

web.xml is used to configure the servlet container properties of the hello world appliation.

<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.

index.jsp

In the hello world example the index.jsp page simply forwards the request to the hello world action.

<jsp:forward page="HelloWorld.do"/>

struts-config.xml

struts-config.xml file is used to configure the struts framework for the hello world application. This file contains the details regarding the form bean and the action mapping.

<struts-config>

    <form-beans>
        <form-bean name="HelloWorldActionForm"
              type="com.vaannila.HelloWorldActionForm"/>
    </form-beans>

    <action-mappings>
        <action input="/index.jsp" name="HelloWorldActionForm" path="/HelloWorld" scope="session" type="com.vaannila.HelloWorldAction">
        	<forward name="success" path="/helloWorld.jsp" />
        </action>
    </action-mappings>

</struts-config>

HelloWorldActionForm.java

HelloWorldActionForm extends org.apache.struts.action.ActionForm. HelloWorldActionForm class has one String variable message and the corresponding getter and setter methods.

public class HelloWorldActionForm extends
           org.apache.struts.action.ActionForm {

    private String message;

    public HelloWorldActionForm() {
        super();
    }

    public String getMessage() {
        return message;
    }

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

}

HelloWorldAction.java

HelloWorldAction class extends org.apache.struts.action.Action. The action class contains an execute method which contains the business logic of the application. To access the HelloWorldActionForm variables in the Action we need to type caste the form object to HelloWorldActionForm. Then we can access the variables using the getter and setter methods. The execute method returns a value of type ActionForward, based on its value the corresponding view will be called. This configuration is done in struts-config.xml file.

public class HelloWorldAction extends org.apache.struts.action.Action {

    private final static String SUCCESS = "success";

    public ActionForward execute(ActionMapping mapping,ActionForm form,
           HttpServletRequest request,HttpServletResponse response) throws Exception {

        HelloWorldActionForm helloWorldForm = (HelloWorldActionForm) form;
        helloWorldForm.setMessage("Hello World!");
        return mapping.findForward(SUCCESS);

    }
}
<action-mappings>
    <action input="/index.jsp" name="HelloWorldActionForm" path="/HelloWorld"
    scope="session" type="com.vaannila.HelloWorldAction">
        <forward name="success" path="/helloWorld.jsp" />
    </action>
</action-mappings>

The name "success" is mapped to the view helloWorld.jsp. So when the execute method in the action returns "success" the request will be forwarded to the helloWold.jsp page.

helloWorld.jsp

In helloWorld.jsp we get the value of the form variable message and display it. We use struts bean tag to do this. The name attribute of the bean tag hold the value of the action form and the property attribute holds the value of the variable to be displayed.

<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>
<bean:write name="HelloWorldActionForm" property="message" />
</h1>
</body>
</html>
Extract the downloaded files into the webapps folder of the Tomcat server. Start the Tomcat server. Type the following url in the browser "http://localhost:8080/Example1/index.jsp". There you go, you have your first struts program up and running.



You can download the source code of the Hello World example by clicking on the Download link below.

Source :Download
Source + Lib :Download
application Form (document) Business logic Bean (software) Spring Framework Apache Tomcat Requests Property (programming) Attribute (computing) Download

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices
  • DeveloperWeek 2023: The Enterprise Community Sharing Security Best Practices
  • The Beauty of Java Optional and Either
  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer

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: