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-INF\lib folder
  3. Edit web.xml to configure Struts 2 front controller
  4. Create View component WebContent\index.jsp and WebContent\pages\Success1.jsp
  5. Create src\struts.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-INF\lib 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 :
<?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">
    <display-name>Struts2App1</display-name>
    <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>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

4. Create input page WebContent\index.jsp and View component WebContent\pages\MessangerResult.jsp
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>First Struts2 Application</h1>
<a href="MessangerAction">Messager Action</a>
</body>
</html>

MessangerResult.jsp
<%@taglib uri="/struts-tags" prefix="s"%><%@ page language="java"
    contentType="text/html; charset=ISO-8859-1" 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=ISO-8859-1">
<title>Messanger Action Result</title>
</head>
<body>
    <h1>
        Your message is </h1>
        <s:property value="message" /><br><br>
    <a href="index.jsp"> Try Agaian</a>
</body>
</html>

5. Create src\struts.xml and add map configuration between View, Action and Results
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="default" extends="struts-default">
        <action name="MessangerAction" class="techmyguru.actions.MessangerAction">
            <result>/pages/MessangerResult.jsp</result>
        </action>
    </package>
</struts>

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


Write your Comment

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