Home » Struts » Creating Struts Application In Eclipse

Creating Struts Application In Eclipse

In this tutorial we will create a hello world Struts application in Eclipse editor.I have used Eclipse Indigo and Tomcat 6 for developing this example.

We are going to implement a web application using Struts framework which will display Hello World in Browser.

Tools used :

In order to create an application we are going to use the following tools.

Step 1 : Create Dynamic Web Project

Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.

Struts Application in Eclipse

After selecting Dynamic Web Project, press Next Then Eclipse will ask you for name of the project. Enter the name of the project as StrutsExample1 and click on Finish.

Struts Application in Eclipse

Step 3 : Add Jar files to the project

Now copy all the required JAR files in WebContent -> WEB-INF -> lib folder.The following jar files should be added to the project for successful deployment of struts project :

Struts Application in Eclipse

Step 4 : Configure web.xml

Now we have to configure ActionServlet of struts with web.xml. The following xml shows how to configure struts in 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">


We have registered ActionServlet class in web.xml for the url "*.do" , that means every url that ends with ".do" will be taken care by Struts.

We have to also specify location of struts-config.xml as init-param with name "config". Struts reads the file struts-config.xml about the configuration that we provide to the struts such as FormBeans, Actions, Plugins , Global Forwards etc.

After that we have to create a form bean that will be sub class of ActionForm class which will act as model in out application and will contain application state. Following is our form bean named "HelloWorldForm".

Step 5 : Create FormBean Class :


package com.example.javawebtutor.form;

import org.apache.struts.action.ActionForm;

public class HelloWorldForm extends ActionForm {

private String hello = null;

public HelloWorldForm() {
hello = "Hello World";
public String getHello() {
return hello;
public void setHello(String hello) {
this.hello = hello;

This form bean class contains single property named as "hello" which stores "Hello World" as default value.

Our next step is to create a controller class that will help to handle a particular url pattern. The url pattern will be specified and associated with our controller helper class in the file struts-config.xml.

Our controller helper class must be a subclass of "Action" class. When the url will be requested to the server, struts will run "execute" method of the associated with the url and returns the view in response.

Step 6 : Create Action Class :

Following is our controller helper class named as "HelloWorldAction" :


package com.example.javawebtutor.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class HelloWorldAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("hello");


The ActionForm object will contain instance of our form bean i.e. HelloWorldForm. The execute method returns string "hello" as the ActionForward.

All the configuration regarding the action will be made as in the file struts-config.xml mentioned bellow.

Step 7 : Create struts-config.xml file

Create struts-config.xml inside WEB-INF directory of project and add below line of code in this file.


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"

<form-bean name="helloForm"
type="com.example.javawebtutor.form.HelloWorldForm" />
<action name="helloForm" path="/HelloWorld"
type="com.example.javawebtutor.action.HelloWorldAction" scope="request">
<forward name="hello" path="/hello.jsp" />

We have registered our form bean com.example.javawebtutor.form.HelloWorldForm with name "helloForm" and declared our controller class com.example.javawebtutor.action.HelloWorldAction with the following attributes :

  • name = "helloForm" : It specifies the name of the form bean to be use as Model and in this case it is com.example.javawebtutor.form.HelloWorldForm.

  • path="/HelloWorld" : It indicate the url associated to the controller helper class. In this case whene we will hit the url /HelloWorld.do , action will be invoked.

  • scope="request" : It indicates that the model or the form bean HelloForm will be stored in HttpServletRequest instance. the other option can be "session".

Step 8 : Create jsp files

create jsp file hello.jsp inside WebContent directory of your project with following contents.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><bean:write name="helloForm" property="hello"/></title>
	<h1><bean:write name="helloForm" property="hello"/></h1>

Now create jsp file index.jsp inside WebContent directory of your project with following contents


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

This JSP is very simple its simply forwarding the request to HelloWorld.do

Step 9 : Run the application

To run the project, right click on Project name from Project Explorer and select Run as -> Run on Server (Shortcut: Alt+Shift+X, R)

Output :

Struts Application in Eclipse

Directory Structure Of the Project

Directory Structure of the project is given below.

Struts Application in Eclipse

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

Download this example(src+lib) developed in eclipse

Previous Next Article

comments powered by Disqus