Friday, 7 March 2014

HelloWorld Example in Struts 2

At this point I can guess that you are now familiar with architecture of struts2 and have enough confidence that you can start learning of new framework.

Now as you move to new post let’s learn and try to be clear with struts by understanding or making a demo application in struts2. 

We can take famous example of “Hello World” to understand the same.


As every process/system has its pre-requisites, in struts also we do have. Before we can proceed with framework let’s try to be more clear about struts request process and its flow (I mentioned in my previous post).


Reminder: 

Request Life cycle in Struts 2 applications:

  • Client Machine/Web Browser request for resource to server
  • Request reach at web.xml file, where we have entry of “Filter Dispatcher”
  • “Filter Dispatcher” calls to struts configuration file named “Struts.xml” and decides the suitable action.
  • Then the Interceptors use the required functions
  • After that the Action method executes all the functions like storing and retrieving data from a database.
  • Then Result name sent to configuration file (Struts.xml)
  • It can be sending on the output of the browser in HTML, PDF, images or any other.
Now I am starting basic demo example of “HelloWorld”.

Environment used:
  •  JDK 1.6
  • Tomcat 6.0.18
  • MyEclipse
  • Struts2 jar files

We will flow the request process step by step to get the better understanding to make Helloworld demo example.

I assumed that you already created the web project structure using myeclipse, where you can find default JSP file named “index.jsp” under project’s WebRoot folder. If it is not there then you have to create it manually and another JSP file “helloWorld.jsp” should be created under same folder.

Just write a code under index.jsp file which shows the welcome message and when user click on welcome message so another message should be displayed on screen.

<a href="http:/localhost:8080/HelloWorld.action">Hello</a>


Client Machine/Web Browser request for resource to server. This Request goes to Web.xml file and find entry of FilterDispacher interface within the filter tags as mentioned below. 

 <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>



”Filter Dispatcher” calls to struts configuration file named “Struts.xml” and find out the request action within struts configuration file.


<action name=" HelloWorld " class=" x.y.z.actions.HelloWorld " method="message">            
 <result name="success">/ helloWorld.jsp</result>
  </action>


In our case we have a request named “HelloWorld.action”, it will calls the appropriate action class.

package x.y.z.actions; 


import com.opensymphony.xwork2.ActionSupport;

 public class HelloWorld extends ActionSupport{

 public String message(){

System.out.println("Hello World Method Execute");

return SUCCESS;

}

}


If we are not defined any method name so by default it will call to default execute() method and return SUCCESS as result name.

Now a response goes to struts.xml file and compares the result name and forwards it to appropriate JSP file.

Here response sent to helloWorld.jsp(write your own code) in your client browser.



No comments:

Post a Comment