Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enterprise Java v050228MVC1 Model, View, Controller Web Architecture.

Similar presentations


Presentation on theme: "Enterprise Java v050228MVC1 Model, View, Controller Web Architecture."— Presentation transcript:

1 Enterprise Java v050228MVC1 Model, View, Controller Web Architecture

2 Enterprise Java v050228MVC2 First, there were servlets And things were good Faster than CGI programs More scalable with built-in threading capability Value-added features –Request/Response, Sessions Full Java API access –JDBC, JMS, EJB, JavaMail, etc.

3 Enterprise Java v050228MVC3 Hmm, maybe not so good out.printlns in the code became tedious Takes a programmer to write HTML pages –Skill mismatch No automated tool support Mechanisms developed to output HTML pages –Inclusion of pages –htmlKona –Element Construction Set (ECS) There has to be a better way

4 Enterprise Java v050228MVC4 Then there were JSPs Page-centric view of the world Limited programmer involvement JSPs became ubiquitous for dynamic HTML page generation

5 Enterprise Java v050228MVC5 Model 1 Architecture Browser JSP Java Bean Database Custom Tags EJB

6 Enterprise Java v050228MVC6 Model 1 Architecture JSP-centric Suitable for small systems Susceptible to abuse –Excessive Java code in JSPs –Flow control issues

7 Enterprise Java v050228MVC7 Hybrid Approach Use servlets for flow-control; JSPs for HTML pages with dynamic content Allows programmer/web designer specialization Hybrid Approach=Model 2 Architecture A.K.A. MVC –Controller=Servlet+Helper Classes –Model=Application Data/Operations –View=Output Rendered for User

8 Enterprise Java v050228MVC8 Model 2 Architecture Browser JSP Java Bean Database Custom Tags EJB Servlet 1 3 4 5 6 2

9 Enterprise Java v050228MVC9 Model 2 Benefits Allows programmer to implement flow control, system operations in Java/Servlets Allows web page designers to develop HTML/JSP pages –Automated tool support Higher degree of re-use; more maintainable architecture for larger systems

10 Enterprise Java v050228MVC10 Struts ‘Standard’ MVC framework Struts 1.0 released in January 2001 MVC architecture with support for: –Configurable site navigation –Form handling –Internationalization –Extensive Custom tag libraries

11 Enterprise Java v050228MVC11 Struts Architecture Browser ActionServlet Action Mappings Action Database Bean View (Servlet/JSP/HTML) 1 2 3 4 5 6 7 8

12 Enterprise Java v050228MVC12 Struts Application Flow All requests are first processed by the action servlet Refers to ActionMappings to determine action to invoke –Calls Actions you’ve implemented for your application –Should be adapters to the rest of the application Action returns view to forward to View renders data placed in request or session scope by Action class

13 Enterprise Java v050228MVC13 A corej2ee MVC-light Struts and similar solutions provide for robust solutions –should be considered for most real implementations –Too little time to cover the details in this course Introduce MVC using a smaller-scale implementation –based on same similar MVC concepts –functional, but does not cover full array of issues involved in a Web-based development its simplicity makes understanding the MVC components possible within the scope of this course

14 Enterprise Java v050228MVC14 corej2ee.web Interfaces/Classes

15 Enterprise Java v050228MVC15 corej2ee.web.Controller public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ String path=request.getServletPath(); try { Route route = navigation_.getRoute(path); String uri = unknownUri_; if (route != null) { Worker worker = route.getWorker(); Object result = worker.execute( getServletContext(),request,response); uri = route.getNextPage(result); } RequestDispatcher rd = getServletContext().getRequestDispatcher(uri); if (rd != null) { rd.forward(request, response); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND, "unable to locate uri '" + uri + "' while handling path '" + path + "'"); }

16 Enterprise Java v050228MVC16 corej2ee.web.EnvEntryNavigator web.xml Configuration route/content-listPrincipals1/worker corej2ee.examples.principal.web.GetPrincipalsDAOWorker java.lang.String route/content-listPrincipals1/success /WEB-INF/content/showResult.jsp java.lang.String route/content-listPrincipals1/fail /WEB-INF/content/ErrorPage.jsp java.lang.String

17 Enterprise Java v050228MVC17 corej2ee.web.EnvEntryNavigator web.xml Configuration route/content-listPrincipals2/worker corej2ee.examples.principal.web.GetPrincipalsDAOWorker java.lang.String route/content-listPrincipals2/success /WEB-INF/content/printPrincipals.jsp java.lang.String route/content-listPrincipals2/fail /WEB-INF/content/ErrorPage.jsp java.lang.String

18 Enterprise Java v050228MVC18 corej2ee.examples.principal.web Workers

19 Enterprise Java v050228MVC19 corej2ee.examples.principal.web. GetPrincipalsDAOWorker public Object execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection conn = null; try { conn = getConnection(); Collection principals = getDAO().findByWhere(conn, "1=1"); request.setAttribute(RESULT, principals); return SUCCESS; } catch (Exception ex) { request.setAttribute(ERROR_MESSAGE, ex.toString()); return ERROR; } finally { closeConnection(conn); }

20 Enterprise Java v050228MVC20 principalWEB showResult.jsp Show Result Results

21 Enterprise Java v050228MVC21 principalWEB showResult.jsp

22 Enterprise Java v050228MVC22 principalWEB printPrincipals.jsp List Principals Principals Collection principals = (Collection)request.getAttribute(Worker.RESULT); for(Iterator itr=principals.iterator(); itr.hasNext(); ) { itr.next() }

23 Enterprise Java v050228MVC23 principalWEB printPrincipals.jsp

24 Enterprise Java v050228MVC24 Resources http://jakarta.apache.org/struts/ http://jguru.com/faq/Struts http://jguru.com/forums/home.jsp?topic=Struts


Download ppt "Enterprise Java v050228MVC1 Model, View, Controller Web Architecture."

Similar presentations


Ads by Google