Presentation is loading. Please wait.

Presentation is loading. Please wait.

MVC Frameworks Alternatives to coding the MVC pattern.

Similar presentations


Presentation on theme: "MVC Frameworks Alternatives to coding the MVC pattern."— Presentation transcript:

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 it’s already implemented All incoming requests are mapped to the ActionServlet in web.xml action org.struts.action.ActionServlet action *.do

9 RequestProcessor Acts as a dispatcher Instantiating a request handler (Action) and a corresponding form bean (ActionForm) <action path="/editCustomerProfile" type="packageName.EditCustomerProfileAction" name="customerProfileForm" scope="request"/> <form-bean name="customerProfileForm" type="packageName.customerProfileForm"/>

10 RequestProcessor The path in identifies a url- pattern that should be handled by this action path=“editCustomerProfile” will be triggerd by editCustomerProfile.do

11 Navigation with ActionForward ActionForward is used to determine where to go <action path="/editCustomerProfile" type="packageName.EditCustomerProfileAction" name="customerProfileForm" scope="request“ input=“failure”>

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() that’s called to validate the input ActionErrors errors = new ActionErrors(); if(name == null || name.length() <1) errors.add("name", new ActionError("error.name.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…


Download ppt "MVC Frameworks Alternatives to coding the MVC pattern."

Similar presentations


Ads by Google