Struts 2 Hello World Example In Eclipse

Today we will discuss a very important part of our Struts 2 tutorials, tips and tricks series. In this particular blog we will create a simple hello world application in eclipse with a practical introduction of struts 2 actions, struts tag libraries and basic components and functionality of struts.xml configuration.

Struts 2 is one of the most popular java web application framework over the years. It is basically based on simple java classes so called POJO(plain old java object). In this blog we will show you how to create a simple Struts 2 Hello World Application in Eclipse. The following files are needed to create a simple Hello World Example.
(1) web.xml
(2) struts.xml
(3) Home.jsp
(4) Welcome.jsp

To start just create a Dynamic Web Project from Eclipse as shown in figure :
Now a Project Wizard w'll open like this :
File > New > Dynamic Web Project


Choose a name for your project and click finish


Now we are done with creating a dynamic web project in Eclipse. The overall structure of Struts Hello World Application looks something like this :


To make it working we have to add some struts2 libraries in lib folder. Here is a snapshoot of all required libraries please add those libraries before we start.


Now let me show you all files code , add these files as per the project structure shown above :

web.xml tells the Servlet Container about all configurations and the way in which the project will behave. The filter and the filter-mapping tags are used to setup  Struts 2 FilterDispatcher. The filter URL pattern "/*" tells the container that all incoming request are going to be handled by Struts 2 itself.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""xmlns="" xmlns:web=""xsi:schemaLocation=""id="WebApp_ID" version="2.5">

This is the entry point for our application , as mentioned in web.xml this file w'll render as welcome file. As click here is clicked  <s:url action="testAction"/> tag will call an action named testAction. Now a action with name testAction is searched in struts.xml.
<%@ taglib uri="/struts-tags" prefix="s" %>
      <title>Java Guys | Home</title>
   <body style="font-family:Segoe Print ">
         <div >To call your first Struts2 Action </div>
         <a href="<s:url action="testAction"/>">click here</a>

Struts 2 Application flow is controlled by struts.xml ,this is entry point for all action mappings and filter configurations. <action>  tag defines the action associated with an request and the location of that class to monitors that action. In <result> tag we defines our views and sometimes othet actions to be called after the specified action is done with its working.
After a required action is found that the associated class and defined method is called.
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  "">
   <constant name="struts.enable.DynamicMethodInvocation" value="false" />
   <constant name="struts.devMode" value="true" />
   <!-- Add your package and namespace here -->
   <package name="default" namespace="/" extends="struts-default">
      <!--Add your actions here --> 
      <action name="testAction" class="javaguys.tutorials.actions.TestAction">
         <result name="success">/Home.jsp</result>
      <!-- Actions end -->

In struts 2 action classes are simple java classes , we can extends ActionSupport class to take advantage of form validation and some other stuff but extending ActionSupport is optional in Struts 2 .

package javaguys.tutorials.actions; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport { public String execute() { return SUCCESS; } }

<%@ taglib uri="/struts-tags" prefix="s"%>
      <title>Java Guys | Home</title>
   <body style="font-family: Segoe Print">
         <div>Congratulations ! We just called a simple struts2 action !</div>
This is a simple jsp page that is being rendered after the action is successfully called.
Here we are all done with adding all required files and libraries to our project. Now run the application on server and you w'll get output something like these screen :


This is a very simple and introductory tutorial on how to start with struts 2, we simple called an action and shows a jsp page as response. One thing to be noted here is that in struts 2 almost everything is related to and depends upon action classes. In upcoming blogs we w'll see other concepts, tutorials, tips and tricks related to Struts2 , Spring and Hibernate Technologies . Download "Struts2 HelloWorld Example" from "SkyDrive"