Docstoc

mvc

Document Sample
mvc Powered By Docstoc
					                                    Enterprise
                                         Java




          Model, View, Controller
            Web Architecture




v050228             MVC                    1
                                                       Enterprise
                                                            Java

              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.




v050228                         MVC                           2
                                              Enterprise
                                                   Java

             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



v050228                         MVC                  3
                                                 Enterprise
                                                      Java

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




v050228                  MVC                            4
                                       Enterprise
                                            Java

            Model 1 Architecture

                   Java Bean        EJB



Browser          JSP               Database



                       Custom
                        Tags

  v050228               MVC                   5
                                        Enterprise
                                             Java

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




v050228                          MVC           6
                                                      Enterprise
                                                           Java

                    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




v050228                         MVC                          7
                                                       Enterprise
                                                            Java

                    Model 2 Architecture

                                               2
                       Servlet                      EJB
                                           3
                1
                        4          Java Bean
Browser                                            Database
                                       5

            6           JSP
                                       Custom
                                        Tags
  v050228                        MVC                          8
                                                        Enterprise
                                                             Java

                    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




v050228                         MVC                            9
                                           Enterprise
                                                Java

                                  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




v050228                              MVC         10
                                                         Enterprise
                                                              Java

                      Struts Architecture

                          Action
                         Mappings
                         2

             1                     3     Action       Database
Browser            ActionServlet
                                            4

                        6                Bean     5
      8

                         View       7
                 (Servlet/JSP/HTML)
   v050228                         MVC                         11
                                                                   Enterprise
                                                                        Java

                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




v050228                              MVC                                 12
                                                                           Enterprise
                                                                                Java

                  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




v050228                              MVC                                         13
                                                                                               Enterprise
                                                                                                    Java

             corej2ee.web Interfaces/Classes

                  HttpServlet
           Control l er                interface                interface                  interface
                                      Navi gati on               R oute                     Worker
   +PRINT_DEBUG:boolean
   +DEBUG_PREFIX:String                                                             +SUCCESS:String
   +NAVIGATION_CLASS:String         +getRoute:Route        +getNextPage:String      +ERROR:String
   +UNKNOWN_URI:String                                                              +RESULT:String
   +EVENT_CONTEXT:String                                   worker:Worker            +ERROR_MESSAGE:String
   +EVENT:String
   -navigation_:Navigation                                                          +execute:Object
   -unknownUri_:String
                                   En vEn tryNa vi gator
   +init:void
   +doGet:void                  +PRINT_DEBUG:boolean             Basi cR oute
   +doPost:void                 +DEBUG_PREFIX:String
   #debug:void                  +ROUTE_CONTEXT:String         -worker_:Worker                Nul lWork er
                                #routes_:Map                  -successUri_:String
    servletInfo:String                                        -failUri_:String
                                +EnvEntryNavigator                                         +execute:Object
                                +init:void                    +BasicRoute
                                +getRoute:Route               +getNextPage:String
                                #buildRoute:Route             +toString:String
                                +toString:String
                                #debug:void                   worker:Worker




v050228                                            MVC                                                 14
                                                                                        Enterprise
                                                                                             Java

                  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
                    + "'");
             }
v050228    }                                     MVC                                          15
        corej2ee.web.EnvEntryNavigator                                            Enterprise
                                                                                       Java



             web.xml Configuration
                    <!-- begin: listPrincipals 1 -->
        <env-entry>
         <env-entry-name>route/content-listPrincipals1/worker</env-entry-name>
         <env-entry-value>corej2ee.examples.principal.web.GetPrincipalsDAOWorker
         </env-entry-value>
         <env-entry-type>java.lang.String</env-entry-type>
       </env-entry>

       <env-entry>
         <env-entry-name>route/content-listPrincipals1/success</env-entry-name>
         <env-entry-value>/WEB-INF/content/showResult.jsp</env-entry-value>
         <env-entry-type>java.lang.String</env-entry-type>
       </env-entry>

        <env-entry>
          <env-entry-name>route/content-listPrincipals1/fail</env-entry-name>
          <env-entry-value>/WEB-INF/content/ErrorPage.jsp</env-entry-value>
          <env-entry-type>java.lang.String</env-entry-type>
v050228 </env-entry>                      MVC                                           16
        corej2ee.web.EnvEntryNavigator                                             Enterprise
                                                                                        Java



             web.xml Configuration
      <!-- begin: listPrincipals 2 -->
        <env-entry>
         <env-entry-name>route/content-listPrincipals2/worker</env-entry-name>
         <env-entry-value>corej2ee.examples.principal.web.GetPrincipalsDAOWorker
         </env-entry-value>
         <env-entry-type>java.lang.String</env-entry-type>
       </env-entry>

       <env-entry>
         <env-entry-name>route/content-listPrincipals2/success</env-entry-name>
         <env-entry-value>/WEB-INF/content/printPrincipals.jsp</env-entry-value>
         <env-entry-type>java.lang.String</env-entry-type>
       </env-entry>

        <env-entry>
          <env-entry-name>route/content-listPrincipals2/fail</env-entry-name>
          <env-entry-value>/WEB-INF/content/ErrorPage.jsp</env-entry-value>
          <env-entry-type>java.lang.String</env-entry-type>
v050228 </env-entry>                      MVC                                            17
           corej2ee.examples.principal.web                                                                      Enterprise
                                                                                                                     Java



                       Workers                             Worker
                                           P rinci pal DAOWorker                          interface
                                                                                     corej2ee. web. Worker
                                       +PRINT_DEBUG:boolean
                                       +DEBUG_PREFIX:String                        +SUCCESS:String
                                       -DB_NAME:String                             +ERROR:String
                                       connectionBean_:ConnectionBean              +RESULT:String
                                       principalDAO_:PrincipalDAO                  +ERROR_MESSAGE:String

                                       +PrincipalDAOWorker                         +execute:Object
                                       #closeConnection:void
                                       #getPrincipalType:String
                                       #getPrincipal:Principal
                                       #log:void

                                        connection:Connection
                                        DAO:PrincipalDAO




G etP rincip al sDAOWorkerG etP rincip al DA OWorker InsertP ri nci pal DAOWorkerModi fyP rinci pal DAOWorker


+GetPrincipalsDAOWorker   +GetPrincipalDAOWorker     +InsertPrincipalDAOWorker   +ModifyPrincipalDAOWorker
+execute:Object           +execute:Object            +execute:Object             +execute:Object




v050228                                                 MVC                                                           18
          corej2ee.examples.principal.web.                                  Enterprise
                                                                                 Java



             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);
             }
           }
v050228                                    MVC                                    19
                                                                           Enterprise
                                                                                Java

          principalWEB showResult.jsp
          <jsp:directive.page errorPage="/ExceptionPage.jsp" />
          <jsp:directive.page import="corej2ee.web.*" />

          <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
          <html>
          <head>
            <title>Show Result</title>
          </head>
          <body>
            <center><h1>Results</h1></center>
            <%=request.getAttribute(Worker.RESULT)%>
          </body>
          </html>




v050228                                MVC                                       20
                                        Enterprise
                                             Java

          principalWEB showResult.jsp




v050228               MVC                     21
                                                                                    Enterprise
                                                                                         Java

        principalWEB printPrincipals.jsp
       <jsp:directive.page errorPage="/ExceptionPage.jsp" />
       <jsp:directive.page import="corej2ee.examples.principal.*" />
       <jsp:directive.page import="corej2ee.web.*" />
       <html>
       <head>
         <title>List Principals</title>
       </head>
       <body>
         <center><h1>Principals</h1></center>
         <nl>
         <jsp:scriptlet>
         Collection principals = (Collection)request.getAttribute(Worker.RESULT);
         for(Iterator itr=principals.iterator(); itr.hasNext(); ) {
           </jsp:scriptlet>
           <li><jsp:expression>itr.next()</jsp:expression></li>
           <jsp:scriptlet>
         }
         </jsp:scriptlet>
         </nl>
       </body>
v050228</html>                                 MVC                                        22
                                             Enterprise
                                                  Java

          principalWEB printPrincipals.jsp




v050228                 MVC                        23
                                                  Enterprise
                                                       Java

                      Resources
• http://jakarta.apache.org/struts/
• http://jguru.com/faq/Struts
• http://jguru.com/forums/home.jsp?topic=Struts




v050228                    MVC                          24

				
DOCUMENT INFO
Categories:
Tags:
Stats:
views:9
posted:7/31/2012
language:
pages:24