Recommended Links

FLIPKART ASSURED STORE

Royal Oak Entertainment Unit (Dark Finish)     Amazon-GREAT INDIAN FESTIVAL



Our Advertising Partners

Sunday 28 August 2011

Struts (Part3)




Hello and Welcome You All once again to learning center of Struts, where after completing two very important struts2 parts (Part 1 and Part 2) we are now exploring a simple  example. Please remember that we are working on Apache 6.


Struts-2 Example



To create a "Hello World" example, you need to do four things:
  • Create a class to store the welcome message (the model)
  • Create a server page to present the message (the view)
  • Create an Action class to control the interaction between the user, the model, and the view (the controller)
  • Create a mapping (struts.xml) to couple the Action class and view


The Code

   Step 1 - Create The Model Class MessageStore.java
   
 package org.apache.struts.helloworld.model;

   public class MessageStore {

        private String message;

        public MessageStore() {

                setMessage("Hello Struts User");
        }

        public String getMessage() {

               return message;
        }

        public void setMessage(String message) {

               this.message = message;
        }

}

Step 2 - Create The Action Class HelloWorldAction.java


package org.apache.struts.helloworld.action;
import org.apache.struts.helloworld.model.MessageStore;
import com.opensymphony.xwork2.ActionSupport; 
public class HelloWorldAction extends ActionSupport
 {
         private static final long serialVersionUID = 1L;
         private MessageStore messageStore;
         public String execute() throws Exception
               {
                              messageStore = new MessageStore() ;
                              return SUCCESS;
               }
         public MessageStore getMessageStore()
               {
                             return messageStore;
               }
         public void setMessageStore(MessageStore messageStore)
              {
                           this.messageStore = messageStore;
               }
 }

Step 3 - Create The View HelloWorld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Hello World!</title>
</head>
<body>
    <h2><s:property value="messageStore.message" /></h2>
</body>
</html>

Step 4 - Add The Struts Configuration In struts.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

<struts>

  <constant name="struts.devMode" value="true" />

  <package name="basicstruts2" extends="struts-default">

  <action name="index">
    <result>/index.jsp</result>
  </action>

  <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
    <result name="success">/HelloWorld.jsp</result>
  </action>

</package>

</struts>

Step 5 - Create The URL Action

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>  

Step 6 – Create web.xml

<?xml version="1.0" encoding="UTF-8"?>
<display-name>HelloWorldStruts2</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

                                                             
    <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>
   
</web-app>

 
Step 7 - Build the WAR File and Run The Application

How the Code Works

  1. The container receives from the web server a request for the resource hello.action. According to the settings loaded from the web.xml, the container finds that all requests are being routed toorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter, including the *.action requests. The StrutsPrepareAndExecuteFilter is the entry point into the framework.
  2. The framework looks for an action mapping named "hello", and it finds that this mapping corresponds to the class "HelloWorldAction". The framework instantiates the Action and calls the Action's execute method.
  3. The execute method creates the MessageStore object and returns SUCCESS. The framework checks the action mapping to see what page to load if SUCCESS is returned. The framework tells the container to render as the response to the request, the resource HelloWorld.jsp.
  4. As the page HelloWorld.jsp is being processed, the <s:property value="messageStore.message" /> tag calls the getter getMessageStore of the HelloWorld Action and then calls the getMessage of the MessageStore object returned by getMessageStore, and the tag merges into the response the value of the message attribute.
  5. A pure HTML response is sent back to the browser.


Regards,
Gaurav

No comments:

Post a Comment