JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven









Overview of Struts


What is Struts?

  • Struts is open source implementation of Java MVC (Model-View-Controller) pattern for the development of web based applications on JEE plateform
  • The core of the Struts framework is a flexible control layer based on standard technologies like Java Servlets, JavaBeans, ResourceBundles, and XML).
  • Struts encourages application architectures based on the Model 2 approach, a variation of the classic Model-View-Controller (MVC) design paradigm.

  • Struts provides its own Controller component and integrates with other technologies to provide the Model and the View
  • Struts can interact with standard data access technologies, like JDBC and EJB, as well as most any third-party packages, like Hibernate, iBATIS, or Object Relational Bridge.
  • For the View, Struts works well with JavaServer Pages, including JSTL and JSF, as well as Velocity Templates, XSLT, and other presentation systems.


Model 1 design Pattern



Problems with Model1 Architecture
  • The presentation logic usually leads to a significant amount of Java code embedded in the JSP in the form of scriptlets making it ugly and maintenance nightmare even for experienced Java developers
  • In large applications, JSPs are developed and maintained by page authors. The intermingled scriptlets and markup results in unclear definition of roles and is very problematic.
  • Application control is decentralized since the next page to be displayed is determined by the logic embedded in the current page. Decentralized navigation control can cause headaches.


    MVC Design Pattern

MVC suggests that an application should have three parts
  • Model
    Represents system state and business logic beans. E.g java beans and enterprise beans
  • View
    View portion represents the user interface and for a J2EE web application is most often constructed using JavaServer Pages (JSP) technology
  • Controller
        The Controller is focused on receiving requests from the client (typically a user running a web browser), deciding what business logic function is to be performed, and then delegating responsibility for producing the next phase of the user interface to an appropriate View component

    Model 2 Architecture - MVC



  • The Controller Servlet handles the user’s request. (This means the hyperlink in the JSP should point to the controller servlet).
  • The Controller Servlet then instantiates appropriate JavaBeans based on the request parameters (and optionally also based on session attributes).
  • The Controller Servlet then by itself or through a controller helper communicates with the middle tier or directly to the database to fetch the required data.
  • The Controller sets the resultant JavaBeans (either same or a new one) in one of the following contexts – request, session or application.
  • The controller then dispatches the request to the next view based on the request URL.
  • The View uses the resultant JavaBeans from Step 4 to display data.
Advantages

  • Since there is no presentation logic in JSP, there are no scriptlets.
  • Web applications based on Model 2 architecture are easier to maintain and extend since the views do not refer to each other and there is no presentation logic in the views.
  • It also allows you to clearly define the roles and responsibilities in large projects thus allowing better coordination among team members.

The Struts Framework
  • 3 Major Components in STRUTS
    • Servlet controller (Controller)
    • Java Server Pages (View)
    • Application Business Logic (Model)
  • Controller bundles and routes HTTP request to other objects in framework
    Controller parses configuration file.
  • Configuration file contains action mappings (determines navigation)
  • Controller uses mappings to turn HTTP requests into application actions
  • Mapping must specify 
    • A request path
    • Object type to act upon the request

Struts Environment
  • In Struts, there is only one controller servlet for the entire web application.
    This controller servlet is called ActionServlet.
  • The servlet intercepts every client request and populates an ActionForm
    from the HTTP request parameters.
  • ActionForm is a normal JavaBeans class. It has several attributes corresponding to the HTTP request parameters and getter, setter methods for those attributes. You have to create your own ActionForm for every HTTP request handled through the Struts framework by extending the ActionForm class.
  • They are essentially View Data Transfer Object.
  • View Data Transfer Object is an object that holds the data from html page and transfers it around in the web tier framework and application classes.

How the Struts Framework Work?
  • The ActionServlet then instantiates a Handler.
  • The Handler class name is obtained from an XML file based on the URL path information.
  •  This XML file is referred to as Struts configuration file and by default named as struts-config.xml.
  • The Handler is called Action in the Struts terminology
  • The Action class is abstract and defines a single method called execute().
    You override this method in your own Actionsand invoke the business logic in this method. 
  • The execute()method returns the name of next view (JSP) to be shown to the user.
  • The ActionServlet forwards to the selected view.
Struts Framework Components
  • ActionServlet
  • RequestProcessor
  • ActionForm
  • Action
  • ActionMapping
  • ActionForward
All residing in org.apache.struts.action package.

ActionServlet
  • The central component of the Struts Controller is the ActionServlet.
  • Extends the javax.servlet.HttpServlet.
  • On startup, its reads the Struts Configuration file and loads it into memory in the init() method.
  • In the doGet() and doPost() methods, it intercepts HTTP request and handles it appropriately.

The web.xml entry for configuring the ActionServlet and Struts Config file is as follows

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/config/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

  • ActionServlet intercepts HTTP requests based on the URL pattern and handles them appropriately. The URL pattern can be either path or suffix.
  •  This is specified using the servlet-mapping in web.xml. 
  • An example of suffix mapping is as follows.
        <servlet-mapping>
                <servlet-name>action</servlet-name>
                <url-pattern>*.do</url-pattern>
         </servlet-mapping>
  • Once the ActionServlet intercepts the HTTP request, it doesn’t do much. 
  • It delegates the request handling to another class called RequestProcessor
    by invoking its process() method.
Request Processor And Action Mapping




  • The RequestProcessor first retrieves appropriate XML block for the URL from struts-config.xml.
  • This XML block is referred to as ActionMapping.
  • In fact there is a class called ActionMapping.It holds the mapping between a URL and Ac

<action path="/submitDetailForm"
type="myaction.CustomerAction"
name="CustomerForm"
scope="request"
validate="true"
input="CustomerDetailForm.jsp">
<forward name="success"
path="ThankYou.jsp"
redirect=”true”/>
<forward name="failure" path="Failure.jsp" />
</action>

  • The RequestProcessor looks up the configuration file for the URL pattern
  • /submitDetailForm. (i.e. URL path without the suffix do)
  • The type attribute tells Struts which Action class has to be instantiated.
  • The above ActionMapping tells Struts to map the URL request with the path /submitDetailForm to the class mybank.example.CustomerAction.
Action Form

  • After selecting ActionMapping, the RequestProcessor instantiates the ActionForm.
    Somewhere else in struts-config.xml, you will find a declaration like this:
        <form-bean name="CustomerForm“
         type="myform.CustomerForm"/>
  • This form-bean declaration associates a logical name CustomerForm with the actual class mybank.example.CustomerForm.
  • The RequestProcessor instantiates the CustomerForm and puts it in
    appropriate scope – either session or request. 
  • Next, RequestProcessor iterates through the HTTP request parameters and populates the CustomerForm properties of the same name as the HTTP request parameters using Java Introspection. 
  • Next, the RequestProcessor checks for the validate attribute in the ActionMapping. If the validate is set to true, the RequestProcessor invokes the validate() method on the CustomerForm instance.
Action
  • The RequestProcessor instantiates the Action class specified in the ActionMapping
    and invokes the execute() method on the CustomerAction instance.
  •  The signature of the execute method is as follows:
public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

Action Forward
  • The execute() method returns the next view shown to the user.
  • ActionForward is the class that encapsulates the next view information.
  • Struts encourages you not to hardcode the JSP names for the next view.
    ActionForward forward = mapping.findForward(“success”);

Struts Configuration File – struts-config.xml
  • The Struts configuration file adheres to the struts-config_1_1.dtd
  • The five important sections are:
    • Form bean definition section
    • Global forward definition section
    • Action mapping definition section
    • Controller configuration section
    • Application Resources definition section
<struts-config> 

Form bean Definitions


Global Forward Definitions

<global-forwards>
<forward name="logon" path="/logon.jsp"/>
</global-forwards> 

Action Mappings


Controller Configuration

<controller
processorClass="org.apache.struts.action.RequestProcessor" />

Message Resource Definition

<message-resources parameter="mybank.ApplicationResources"/>

</struts-config>