Search

Struts Hello World Example in Eclipse

In this tutorial you will learn how to create a Struts hello world application in eclipse. First create a new project, go to File->New and select DynamicWebProject. 

Enter the project name and click the Finish button.
Add the following jar files to the WEB-INF\lib directory.
Right click the src folder and select New->Package.
Enter the package name as com.vaannila.form and click Finish.
Now right click the newly created package and select New->Class.
Enter the class name as HelloWorldForm and the superclass name asorg.apache.struts.action.ActionForm and click Finish.
In the HelloWorldForm class add the following code.
01.package com.vaannila.form;
02. 
03.import org.apache.struts.action.ActionForm;
04. 
05.public class HelloWorldForm extends ActionForm {
06. 
07.private static final long serialVersionUID = -473562596852452021L;
08. 
09.private String message;
10. 
11.public String getMessage() {
12.return message;
13.}
14. 
15.public void setMessage(String message) {
16.this.message = message;
17.}
18.}
In the same way create a new package com.vaannila.action and create a HelloWorldAction class extending org.apache.struts.action.Action. Add the following code to the action class and save it.
01.package com.vaannila.action;
02. 
03.import javax.servlet.http.HttpServletRequest;
04.import javax.servlet.http.HttpServletResponse;
05. 
06.import org.apache.struts.action.Action;
07.import org.apache.struts.action.ActionForm;
08.import org.apache.struts.action.ActionForward;
09.import org.apache.struts.action.ActionMapping;
10. 
11.import com.vaannila.form.HelloWorldForm;
12. 
13.public class HelloWorldAction extends Action {
14. 
15.@Override
16.public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throwsException {
17.HelloWorldForm hwForm = (HelloWorldForm) form;
18.hwForm.setMessage("Hello World");
19.return mapping.findForward("success");
20.}
21.}
Here we typecast the ActionForm to HelloWorldForm and set the message value.
Add the following entries in the struts-config.xml file.
01.<?xml version="1.0" encoding="ISO-8859-1" ?>
02. 
03.<!DOCTYPE struts-config PUBLIC
04."-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
06. 
07.<struts-config>
08. 
09.<form-beans>
10.<form-bean name="helloWorldForm"type="com.vaannila.form.HelloWorldForm"/>
11.</form-beans>
12. 
13.<global-forwards>
14.<forward name="helloWorld" path="/helloWorld.do"/>
15.</global-forwards>
16. 
17.<action-mappings>
18.<action path="/helloWorld"type="com.vaannila.action.HelloWorldAction" name="helloWorldForm">
19.<forward name="success" path="/helloWorld.jsp" />
20.</action>
21.</action-mappings>
22. 
23.</struts-config>
Now configure the deployment descriptor. Add the following configuration information in the web.xmlfile.
01.<?xml version="1.0" encoding="UTF-8"?>
03.<display-name>StrutsExample1</display-name>
04. 
05.<servlet>
06.<servlet-name>action</servlet-name>
07.<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
08.<init-param>
09.<param-name>config</param-name>
10.<param-value>/WEB-INF/struts-config.xml</param-value>
11.</init-param>
12.<load-on-startup>2</load-on-startup>
13.</servlet>
14. 
15.<servlet-mapping>
16.<servlet-name>action</servlet-name>
17.<url-pattern>*.do</url-pattern>
18.</servlet-mapping>
19. 
20.<welcome-file-list>
21.<welcome-file>index.jsp</welcome-file>
22.</welcome-file-list>
23.</web-app>
When we run the application the index.jsp page will be executed first. In the index.jsp page we redirect the request to the helloWorld.do URI, which inturn invokes the HelloWorldAction.
1.<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
2.<logic:redirect forward="helloWorld"/>
In the action class we return the ActionForward "success" which is mapped to the helloWorld.jsppage. In the helloWorld.jsp page we display the "Hello World" message.
01.<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
02.<html>
03.<head>
04.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
05.<title>Hello World</title>
06.</head>
07.<body>
08.<bean:write name="helloWorldForm" property="message"/>
09.</body>
10.</html>
After creating all the files the directory structure of the application looks like this.


Struts MVC Architecture Tutorial

The model contains the business logic and interact with the persistance storage to store, retrive and manipulate data.
The view is responsible for dispalying the results back to the user. In Struts the view layer is implemented using JSP.
The controller handles all the request from the user and selects the appropriate view to return. In Sruts the controller's job is done by the ActionServlet

The following events happen when the Client browser issues an HTTP request.
  • The ActionServlet receives the request.
  • The struts-config.xml file contains the details regarding the ActionsActionFormsActionMappingsand ActionForwards.
  • During the startup the ActionServelet reads the struts-config.xml file and creates a database of configuration objects. Later while processing the request the ActionServlet makes decision by refering to this object.
When the ActionServlet receives the request it does the following tasks.
  • Bundles all the request values into a JavaBean class which extends Struts ActionForm class.
  • Decides which action class to invoke to process the request.
  • Validate the data entered by the user.
  • The action class process the request with the help of the model component. The model interacts with the database and process the request.
  • After completing the request processing the Action class returns an ActionForward to the controller.
  • Based on the ActionForward the controller will invoke the appropriate view.
  • The HTTP response is rendered back to the user by the view component.