First Struts2 application on Tomcat Web Server v6

Now it is time to create first struts 2 application step by step. The following software's are required for this application:

You can use any J2EE enabled Eclipse, I am using Eclipse Helios, and any Java enabled Web Container like - Tomcat, JBoss Application Server, GlassFish Application Server, Oracle WebLogic Application Server. I am using Apache Tomcat Server 6, and Struts 2 library and Struts 2 version 2.3.8.

Steps are creating and configuring web application based on Struts 2:

  1. Create Dynamic Web Project
  2. Copy the required jar files in the WEB-INFlib folder
  3. Edit web.xml to configure Struts 2 front controller
  4. Create View component WebContentindex.jsp and WebContentpagesSuccess1.jsp
  5. Create srcstruts.xml and add map configuration between View, Action and Results
  6. Create Action class
  7. Verify the project directory structure
  8. Run the application and get output

1. Create Dynamic Web Project in Eclipse

[ Note - I assume that You have already configured Apache Tomcat Server in Eclipse environment ]

File -> New -> Dynamic Web Project and specify project name "Struts2App1" as below

Dynamic Web Project

and click Next -> Next -> Finish

2. Copy the following jar files in the WEB-INFlib folder

commons-collections-3.1.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.8.jar
xwork-core-2.3.8.jar

3. Edit web.xml to configure Struts 2 controller as below :

    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">
    Struts2App1
   
        struts2
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   

   
        struts2
        /*
   

   
        index.jsp
   


4. Create input page WebContentindex.jsp and View component WebContentpagesMessangerResult.jsp
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




Insert title here


First Struts2 Application


Messager Action


MessangerResult.jsp
<%@taglib uri="/struts-tags" prefix="s"%><%@ page language="java"
    contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>




Messanger Action Result


   


        Your message is


       


    Try Agaian


5. Create srcstruts.xml and add map configuration between View, Action and Results

    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

   
       
            /pages/MessangerResult.jsp
       

   


6. Create Action class MessangerAction.java -
package techmyguru.actions;
import com.opensymphony.xwork2.ActionSupport;
public class MessangerAction extends ActionSupport {
    private String message;
    public String getMessage() {
        return message;
    }
    @Override
    public String execute() throws Exception {
        message = "This is first Struts2 application";
        return SUCCESS;
    }
}

7. Verify the directory structure as below -
Web Application Directory Structure

8. Run the application and get output :

Right click on the project -> Run As -> Run on Server and see screen -
Struts2App1 Output1

Struts2App1 Output2

If you see the last two screen, it means your first application running successfully.
Congratulations !!!!!!!!!!!

Top


Previous Download Source Code Next


Share your valuable comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code :