MVC Frameworks - PowerPoint PPT Presentation

About This Presentation
Title:

MVC Frameworks

Description:

Title: Java 2 Enterprise Edition Author: Fredrik lund Last modified by: Fredrik lund Created Date: 2/2/2003 5:42:43 PM Document presentation format – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 17
Provided by: Fredr180
Category:

less

Transcript and Presenter's Notes

Title: MVC Frameworks


1
MVC Frameworks
  • Alternatives to coding the MVC pattern

2
The problem with MVC
  • If you want to implement the MVC pattern
    correctly, there will be a lot administrative
    code
  • Request processing
  • Error handling
  • View mapping
  • Hard to maintain

3
The solution MVC Frameworks
  • There are several frameworks that handles the MVC
    pattern for us
  • The frameworks handles things like
  • The frontcontroller
  • Request mappings
  • What should happened with different requests
  • Error handling
  • View handling
  • Choosing the correct view
  • Tag libraries to help us code the view

4
Different frameworks
  • There are a lot of different frameworks to choose
    from
  • Struts (from Jakarta)
  • Java Server Faces (a new emerging Java standard)
  • Vendor specific frameworks (like Oracles BC4J)
  • We will talk about Struts

5
Struts
  • A Open source framework from the Apache group
  • Very widely used
  • Great flexibility for the developer
  • Easy to change the behavior after the application
    have been deployed
  • All mapping between requests and views is done in
    a XML-file, struts-config.xml
  • A very rich set of Custom tags (overlapping JSTL
    a bit)
  • Great support for localization and
    internationalization

6
The main parts of Struts
  • ActionServlet
  • RequestProcessor
  • ActionMapping
  • ActionForm
  • Action
  • ActionForward
  • ActionError

7
Struts architecture in short
  • Requests are mapped to Action in
    struts-config.xml
  • One Action per request
  • Determines where to go next and tells the system
    by using ActionForward
  • ActionForm is mapped to a Action and is used to
    capture all data sent to the system (from a form)
  • Performs validation

8
ActionServlet
  • The ActionServlet is the front controller in
    Struts, and its already implemented
  • All incoming requests are mapped to the
    ActionServlet in web.xml
  • ltservletgt
  • ltservlet-namegtactionlt/servletgt
  • ltservlet-classgt
  • org.struts.action.ActionServlet
  • lt/servlet-classgt
  • lt/servletgt
  • ltservlet-mappinggt
  • ltservlet-namegtactionlt/servlet-namegt
  • lturl-patterngt.dolt/url-patterngt
  • lt/servlet-mappinggt

9
RequestProcessor
  • Acts as a dispatcher
  • Instantiating a request handler (Action) and a
    corresponding form bean (ActionForm)
  • ltaction-mappingsgt
  • ltaction path"/editCustomerProfile"
  • type"packageName.EditCustomerProfileAction"
  • name"customerProfileForm"
  • scope"request"/gt
  • lt/action-mappingsgt
  • ltform-bean name"customerProfileForm"
  • type"packageName.customerProfileForm"/gt

10
RequestProcessor
  • The path in ltaction-mappinggt identifies a
    url-pattern that should be handled by this action
  • patheditCustomerProfile will be triggerd by
    editCustomerProfile.do

11
Navigation with ActionForward
  • ActionForward is used to determine where to go
  • ltaction-mappingsgt
  • ltaction path"/editCustomerProfile"
  • type"packageName.EditCustomerProfileAction"
  • name"customerProfileForm"
  • scope"request
  • inputfailuregt
  • ltforward name"success" path"/customer.jsp"/gt
  • ltforward name"failure" path"/error.jsp"/gt
  • lt/actiongt
  • lt/action-mappingsgt

12
ActionForward
  • Name is a unique identifier to be able to
    identify where to go with a simple name, like
    success

13
Action
  • The method ActionForward execute(ActionMapping
    mapping, ActionForm form, HttpServletRequest
    request, HttpServletResponse response) is
    executed for each mapped request
  • execute() is the main entry point
  • execute() returns the mapping to be used by the
    RequestProcessor using the simple name
  • return mapping.findForward("success")

14
ActionForm
  • The ActionForm is automatically popluated with
    data from the request
  • Used as a bridge between the view and the model

15
ActionErrors and ActionError
  • ActionErrors is an array of ActionError
  • Simplifies error handling and validation in the
    system
  • ActionForm hava a method called validate() thats
    called to validate the input
  • ActionErrors errors new ActionErrors()
  • if(name null name.length() lt1)
  • errors.add("name", new ActionError("error.na
    me.required"))
  • return errors
  • Returned errors will trigger the ActionForward
    defined in input of the action-mapping, i.e.
    error.jsp

16
Walk through of simple example
Write a Comment
User Comments (0)
About PowerShow.com