Wednesday, March 6, 2013

Struts 2 Hello World Example In Eclipse




Today we will discuss a very important part of our Struts 2. 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 will 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 snapshot of all required libraries please add those libraries before we start.
WebContent\WEB-INF\lib

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.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   
  5.  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  6.   
  7.  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  8.   
  9.  id="WebApp_ID" version="2.5">  
  10.   
  11.  <display-name>struts2HelloWord</display-name>  
  12.   
  13.  <welcome-file-list>  
  14.   
  15.   <welcome-file>Welcome.jsp</welcome-file>  
  16.  </welcome-file-list>  
  17.   
  18.  <filter>  
  19.   
  20.   <filter-name>struts2</filter-name>  
  21.   
  22.   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  23.   
  24.  </filter>  
  25.   
  26.  <filter-mapping>  
  27.   
  28.   <filter-name>struts2</filter-name>  
  29.   
  30.   <url-pattern>/*</url-pattern>  
  31.   
  32.  </filter-mapping>  
  33.   
  34. </web-app>  
WebContent\Welcome.jsp

This is the entry point for our application , as mentioned in web.xml this file will 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.

  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. <html>  
  4.   
  5. <head>  
  6.   
  7. <title>Java Guys | Home</title>  
  8.   
  9. </head>  
  10.   
  11. <body style="font-family:Segoe Print ">  
  12.   
  13. <center>  
  14.   
  15. <div >To call your first Struts2 Action </div>  
  16.   
  17. <a href="<s:url action="testAction"/>">click here</a>  
  18.   
  19. </center>  
  20.   
  21. </body>  
  22.   
  23. </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 other 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.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.   
  4.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  5.   
  6.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  7.   
  8. <struts>  
  9.   
  10.  <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  11.   
  12.  <constant name="struts.devMode" value="true" />  
  13.   
  14.  <!-- Add your package and namespace here -->  
  15.   
  16.  <package name="default" namespace="/" extends="struts-default">  
  17.   
  18.   <!--Add your actions here -->  
  19.   
  20.   <action name="testAction" class="javaguys.tutorials.actions.TestAction">  
  21.   
  22.    <result name="success">/Home.jsp</result>  
  23.   
  24.   </action>  
  25.   
  26.   <!-- Actions end -->  
  27.  </package>  
  28.   
  29. </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 .
  1. package javaguys.tutorials.actions;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class TestAction extends ActionSupport {  
  6.   
  7.  public String execute(){  
  8.   
  9.   return SUCCESS;  
  10.   
  11.   }  
  12.   
  13. }  

WebContent\Home.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s"%>  
  2.   
  3. <html>  
  4. <head>  
  5. <title>Java Guys | Home</title>  
  6.   
  7. </head>  
  8.   
  9. <body style="font-family: Segoe Print">  
  10.   
  11.  <center>  
  12.   
  13.   <div>Congratulations ! We just called a simple struts2 action !</div>  
  14.   
  15.  </center>  
  16.   
  17. </body>  
  18.   
  19. </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 will get output something like these screen :



This is a very simple and introductory Struts 2 code 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 will see other concepts

2 comments: