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
(5) TestAction.java

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

eclipse-web-project

Choose a name for your project and click finish

web-project-in-eclipse

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

spring-hello-world-project

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.

WebContent\WEB-INF\lib

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

WebContent\WEB-INF\web.xml
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="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>struts2HelloWord</display-name>
   <welcome-file-list> 
   <welcome-file>Welcome.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
   </filter>
   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>
   

WebContent\Welcome.jsp
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" %>
<html>
   <head>
      <title>Java Guys | Home</title>
   </head>
   <body style="font-family:Segoe Print ">
      <center>
         <div >To call your first Struts2 Action </div>
         <a href="<s:url action="testAction"/>">click here</a>
      </center>
   </body>
</html>
   

src\struts.xml
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"  "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <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>
      </action>
      <!-- Actions end -->
   </package>
</struts>
   

src\javaguys\tutorials\actions\TestAction.java
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; } }

WebContent\Home.jsp
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
   <head>
      <title>Java Guys | Home</title>
   </head>
   <body style="font-family: Segoe Print">
      <center>
         <div>Congratulations ! We just called a simple struts2 action !</div>
      </center>
   </body>
</html>
   
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 :

spring-hello-world

spring-hello-world
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"