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 2 Framework
  • In Struts, there is only one controller servlet for the entire web application.This controller servlet is called FilterDispatcher
  • The servlet intercepts every client request and populates an Action from the HTTP request parameters.
  • Action 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 Action for every HTTP request handled through the Struts framework by extending the Action 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.
  • The FilterDispatcher 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.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 Actions and invoke the business logic in this method.
  • The execute()method returns the name of next view (JSP) to be shown to the user.
  • The FilterDispatcher forwards to the selected view.

Struts 1 vs Struts 2




Why Struts2
  • Simplified Actions  (POJO Actions)
  • No more ActionForms
  • Easier To Test
  • Interfaces Instead Of Classes
  • Easy Spring integration
  • OGNL expression language integration
  • New tags
  • Different validation framework
  • Tags With Improved Ajax Support
  • Multiple view options (JSP, Freemarker, Velocity and XSLT)
  • Plug-ins to extend and modify framework features
  • However Struts 1 and 2 can co-exist each handling different URL

Request Lifecycle in Struts 2

  • User Sends request
  • Controller determines the appropriate action
  • Interceptors are applied
  • Execution of Action
  • Output rendering
  • Return of Request
  • Display the result to user




What are the pieces of struts?

  • The filter dispatcher, by which all the requests to the applications gets filtered. - Controller
  • The interceptors which called after filters, before action methods, apply common functionalities like validation, conversion etc
  • Action classes with execute methods, usually storing and/or retrieving information from a database the view,i.e the jsp files
  • Result will be figured out and the jsp file will be rendered.

Example:- A Struts 2 based Application with Eclipse


  • Create a new  dynaminc web project.
  • Select the server tomcat



  • Give it Name StrustHelloWorldApplication.



Adding Struts2 Liabraries
  • Add Strut2 Library File..!
  • Right click on project----->Build Path------>Configure Build Path



  • Click on Add External jar
  • Add all neccessory Struts2 Jar files
  • Then Click on OK.
.
index.html
  • Now we will create first file index.html
  • Create a html file as shown below


  • Add the code as shown below

 index.html

<html>
<body>
  <form action="Hello">
    <input type="submit" value="Click here">
  <form>
</body>
</html>


Creating Action File

  • Now we will create an action file
  • Create a new java class file
  • Save it as HelloAction.java




  • Add the code as shown below

HelloAction.java

package myaction;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

public class HelloAction {

  public HelloAction() {

  }

  public String execute() {

    return "valid";
  }

}

  • Create another jsp named valid.jsp
valid.jsp

<html>
  <body>
      <h2>
        <font color="green">
          Hello World..............!!
        </font>
      </h2>
    </body>
</html>


web.xml
  • This file contains the description of the filterDispatcher
  • This filterDispatcher controls the whole life cycle  of the project
  • create another xml file which is web.xml in WEB-INF folder
web.xml

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

  <filter>
    <filter-name>struts2filter</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
</filter-
class>
  </filter>
  <filter-mapping>
    <filter-name>struts2filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

struts.xml
  • Now we will create struts.xml
  • This file contains all the configuration of the struts project
  • Right click on src----------> New------>others


  • Select xml--->xml file
  • give it  name struts.xml



Add the code as shown below

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" extends="struts-default">
        <action name="Hello" class="myaction.HelloAction">
            <result name="valid">/valid.jsp</result>
        </action>
    </package>
</struts>

  • Add the package name as myaction
  • Give the claass name.as HelloAction
  • Click on the finish button  as shown below.


Directory Structure is given below



Run  the project
  • right click on project
  • click on run as------>Run On Server


Output:-

Deploying the Project





Download Source without lib

Download War with lib