Model, View, Controller Web Architecture PowerPoint PPT Presentation

presentation player overlay
1 / 24
About This Presentation
Transcript and Presenter's Notes

Title: Model, View, Controller Web Architecture


1
Model, View, ControllerWeb Architecture
2
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
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
Then there were JSPs
  • Page-centric view of the world
  • Limited programmer involvement
  • JSPs became ubiquitous for dynamic HTML page
    generation

5
Model 1 Architecture
EJB
Java Bean
Browser
JSP
Database
Custom Tags
6
Model 1 Architecture
  • JSP-centric
  • Suitable for small systems
  • Susceptible to abuse
  • Excessive Java code in JSPs
  • Flow control issues

7
Hybrid Approach
  • Use servlets for flow-control JSPs for HTML
    pages with dynamic content
  • Allows programmer/web designer specialization
  • Hybrid ApproachModel 2 Architecture
  • A.K.A. MVC
  • ControllerServletHelper Classes
  • ModelApplication Data/Operations
  • ViewOutput Rendered for User

8
Model 2 Architecture
EJB
Servlet
2
1
Browser
Java Bean
4
Database
3
5
JSP
6
Custom Tags
9
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
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
Struts Architecture
Action Mappings
2
Database
Action
3
1
ActionServlet
Browser
4
5
Bean
6
8
View (Servlet/JSP/HTML)
7
12
Struts Application Flow
  • All requests are first processed by the action
    servlet
  • Refers to ActionMappings to determine action to
    invoke
  • Calls Actions youve 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
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
corej2ee.web Interfaces/Classes
15
corej2ee.web.Controller
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException Object
eventContext request.getParameter(EVENT_CONTEXT)
Object event request.getParameter(EVEN
T) try Route route
navigation_.getRoute(eventContext, event)
String uri unknownUri_ if
(route ! null) Worker worker
route.getWorker() Object result
worker.execute(getServletContext(),request,respons
e) uri route.getNextPage(result)
RequestDispatcher rd
getServletContext().getRequestDispatcher(uri)
if (rd ! null) rd.forward(request,
response) else
response.sendError(HttpServletResponse.SC_NOT_FOUN
D, "unable to
locate uri '" uri)
16
corej2ee.web.EnvEntryNavigatorweb.xml
Configuration
lt!-- begin listPrincipals 1 --gt
ltenv-entrygt ltenv-entry-namegtroute/listPrinc
ipals1/workerlt/env-entry-namegt
ltenv-entry-valuegtcorej2ee.examples.principal.web.G
etPrincipalsDAOWorker lt/env-entry-valuegt
ltenv-entry-typegtjava.lang.Stringlt/env-entry-t
ypegt lt/env-entrygt ltenv-entrygt
ltenv-entry-namegtroute/listPrincipals1/successlt/env
-entry-namegt ltenv-entry-valuegt/content/show
Result.jsplt/env-entry-valuegt
ltenv-entry-typegtjava.lang.Stringlt/env-entry-typegt
lt/env-entrygt ltenv-entrygt
ltenv-entry-namegtroute/listPrincipals1/faillt/env-en
try-namegt ltenv-entry-valuegt/content/ErrorPa
ge.jsplt/env-entry-valuegt
ltenv-entry-typegtjava.lang.Stringlt/env-entry-typegt
lt/env-entrygt
17
corej2ee.web.EnvEntryNavigatorweb.xml
Configuration
lt!-- begin listPrincipals 2 --gt ltenv-entrygt
ltenv-entry-namegtroute/listPrincipals2/worker
lt/env-entry-namegt ltenv-entry-valuegtcorej2ee
.examples.principal.web.GetPrincipalsDAOWorker
lt/env-entry-valuegt ltenv-entry-typegtjava
.lang.Stringlt/env-entry-typegt lt/env-entrygt
ltenv-entrygt ltenv-entry-namegtroute/listPrinc
ipals2/successlt/env-entry-namegt
ltenv-entry-valuegt/content/printPrincipals.jsplt/env
-entry-valuegt ltenv-entry-typegtjava.lang.Str
inglt/env-entry-typegt lt/env-entrygt
ltenv-entrygt ltenv-entry-namegtroute/listPrinc
ipals2/faillt/env-entry-namegt
ltenv-entry-valuegt/content/ErrorPage.jsplt/env-entry
-valuegt ltenv-entry-typegtjava.lang.Stringlt/e
nv-entry-typegt lt/env-entrygt
18
corej2ee.examples.principal.webWorkers
19
corej2ee.examples.principal.web.GetPrincipalsDAOW
orker
public Object execute(ServletContext context,
HttpServletRequest
request,
HttpServletResponse response)
throws ServletException, IOException
Connection conn null try conn
getConnection() Collection principals
getDAO().findByWhere(conn, "11")
request.setAttribute(RESULT, principals)
return SUCCESS catch (Exception
ex) request.setAttribute(ERROR_MESSAGE,
ex.toString()) return ERROR
finally closeConnection(conn)

20
principalWEB showResult.jsp
ltjspdirective.page errorPage"/ExceptionPage.jsp"
/gt ltjspdirective.page import"corej2ee.web."
/gt lt!doctype html public "-//w3c//dtd html 4.0
transitional//en"gt lthtmlgt ltheadgt lttitlegtShow
Resultlt/titlegt lt/headgt ltbodygt
ltcentergtlth1gtResultslt/h1gtlt/centergt
ltrequest.getAttribute(Worker.RESULT)gt lt/bodygt lt
/htmlgt
21
principalWEB showResult.jsp
22
principalWEB printPrincipals.jsp
ltjspdirective.page errorPage"/ExceptionPage.jsp"
/gt ltjspdirective.page import"corej2ee.examples.
principal." /gt ltjspdirective.page
import"corej2ee.web." /gt lthtmlgt ltheadgt
lttitlegtList Principalslt/titlegt lt/headgt ltbodygt
ltcentergtlth1gtPrincipalslt/h1gtlt/centergt ltnlgt
ltjspscriptletgt Collection principals
(Collection)request.getAttribute(Worker.RESULT)
for(Iterator itrprincipals.iterator()
itr.hasNext() ) lt/jspscriptletgt
ltligtltjspexpressiongtitr.next()lt/jspexpressiongtlt/l
igt ltjspscriptletgt
lt/jspscriptletgt lt/nlgt lt/bodygt lt/htmlgt
23
principalWEB printPrincipals.jsp
24
Resources
  • http//jakarta.apache.org/struts/
  • http//jguru.com/faq/Struts
  • http//jguru.com/forums/home.jsp?topicStruts
Write a Comment
User Comments (0)
About PowerShow.com