Saturday, December 18, 2010

How to create web applications using Struts2 in Eclipse?

Here I will explain simple step by step guide to create a basic Struts2 Hello World! application in Eclipse IDE.

Prerequisites: To develop struts2 application you should already knowing following technologies.

  • Core Java
  • Servlet, JSP, Filters and TAG Libraries
  • Java Beans
  • HTML and HTTP
  • Web Containers e.g. Tomcat
  • XML
Java Requirements

  1. Java 5 or higher
  2. Servlet API 2.4 or higher
  3. JSP 2.0 or higher
You should also have Eclipse JEE 3.5 or higher.
To create a Struts 2 web application with the Struts 2 artifacts added to the the application's class path manually you will need to download the Struts 2 distribution from the Apache Struts website.

On the Struts 2 download page, click on the link for the current General Availability release. In that release's section you'll find several links. To get started with a basic Struts 2 web application you need to only download the Essential Dependencies Only zip file, which is approximately 12mb.

After downloading this zip file, unzip it. You should have a folder named the same as the current general availability release and in that folder will be a lib folder. The lib folder contains the Struts 2 jar files (e.g. struts2-core-X.X.X.X.jar, where X.X.X.X is the version) and other jar files Struts 2 requires (e.g. xwork-core.X.X.X.jar).

As we create our basic Struts 2 web application we will copy from the Struts 2 distribution lib folder just the jar files our application requires. As we add features to our application in future tutorials we will copy other jar files.


  • Now in the Eclipse IDE create "Dynamic Web Project" click here to learn how to create Dynamic Web Project in Eclipse IDE?
  • Now create new JSP file named index,jsp in WebContent directory and add following content to it
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Index</title>
    </head>
    <body>
    <h1>Hello World1 Welcome to Struts2 Hello World application </h1>
    </body>
    </html>
    
  • Now we need to configure struts2 filter in web.xml located at WebContent/WEB-INF/web.xml. The content of web.xml should look like follows:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>HelloWorld</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>    
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
         <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
    </web-app>
    
  • After this create action class say HelloAction as follows
    package com.test.tejash;
    
    public class HelloAction {
    
        public String execute() {
            return "success";
        }
    }
    
  • Finally create struts.xml file in the source folder with following content
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
    
     <package name="hello" extends="struts-default">
      <action name="hello" class="com.test.tejash.HelloAction" >
       <result>/index.jsp</result>
      </action>
     </package>
    </struts> 
Now your struts2 hello world application is ready you can just run the project on the server and if your web server is running at 8080 port then visit http://localhost:8080/HelloWorld/hello.action and you can see the index.jsp page automatically loaded over there...