Pay As You Go MVC in JSPs and Servlets Larry Karnowski OpenNMS.org OReilly Open Source Convention Ju - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Pay As You Go MVC in JSPs and Servlets Larry Karnowski OpenNMS.org OReilly Open Source Convention Ju

Description:

Gives many benefits of pure MVC at less cost in development time and developer expertise ... Ability to 'cheat' when deadlines loom without hating yourself too much ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 14
Provided by: ORA2
Category:

less

Transcript and Presenter's Notes

Title: Pay As You Go MVC in JSPs and Servlets Larry Karnowski OpenNMS.org OReilly Open Source Convention Ju


1
Pay As You Go MVC in JSPs and ServletsLarry
Karnowski OpenNMS.orgOReilly Open Source
ConventionJuly 22-26, 2002
2
What is OpenNMS?
  • The Open Network Management System
  • A Java-based system for discovering, monitoring,
    and managing computer networks
  • OpenNMS Web UI has almost 150 JSPs, almost 100
    servlets, and over 200 helper classes

3
What should you already have?
  • Basic knowledge of Model-View-Controller design
    pattern
  • Basic knowledge of Java Server Pages
  • Basic knowledge of Java Servlets
  • A desire to do more with less in your web
    applications

4
Pay As You Go MVC in JSPs and Servlets
  • MVC is important in large user interfaces
  • However, for small or quickly developed user
    interfaces, MVC can take too much time
  • Many developers opt to not use any MVC designs at
    all, which greatly increases cost of maintenance
  • Instead, use my strategies for minimal MVC that
    can be scaled up to true MVC when the cost is
    warranted

5
What is MVC?
  • Model-View-Controller, a trusted design pattern
    to ease user interface development
  • Separates concerns to ease maintenance and
    parallel development
  • Model - business logic, completely nonvisual
  • View - handles human interaction, completely
    visual, no concept of business logic
  • Controller - ferries events and data between the
    model and view, decoupling them
  • Rule of thumb - Your model should be reusable
    without change regardless of UI medium (web,
    command line, GUI, virtual reality, etc)

6
Why not always use MVC?
  • In theory, you always should!
  • However, not all projects seem worth the cost of
    MVC
  • Requires more developer expertise than simple
    JSPs (more Java code, Servlet APIs, etc)
  • Requires more up-front development time
  • Quicker to prototype without MVC
  • Particularly in Open Source projects - developers
    familiar with PHP or ASPs can contribute easier
    if less MVC

7
How do I pay as I go?
  • Use simpler UI-Delegate version of MVC
  • More JSP-centric, less Java code, less separation
    of concerns
  • Gives many benefits of pure MVC at less cost in
    development time and developer expertise
  • Uses coding strategies to avoid poorly written
    JSPs that tightly couple model, view, and
    controller code
  • Given a good template JSP, easy to copy style by
    beginner developers code reviews essential
  • Easier to decouple and scale to pure MVC later

8
UI-Delegate Adaptation of MVC
  • Popular in Swing (JFC)
  • View and controller are combined to create the
    UI
  • A nonvisual model is still required, now called
    the delegate
  • Same rules apply to the model from pure MVC
  • Easily implemented in JSPs by just adding the
    controller logic

9
UI-Delegate in JSPs
  • Have controller Java code and view HTML in the
    same JSP, but not intermixed
  • Avoid Java in the HTML section of your JSP, keep
    all controller code in a block at the top
  • Use only expressions, for-loops, and
    if-conditionals in your HTML body (or use a tag
    library if available)
  • Have any convenience methods in a declaration
    block at the far bottom
  • Avoid convenience methods that generate HTML,
    keep all HTML in JSP body

10
Benefits of Pay as You Go
  • Open Source concept - Pages start out as
    bazaars and only necessary pages become
    cathedrals
  • Extreme programming concept - Code for today
    if you are not sure yet which pages will need to
    be complex, there is no need to make them all
    complex
  • Adds another tool in your toolkit - like
    Perl/Python for a Java/C developer, make sure you
    use the right tool for the job
  • Ability to cheat when deadlines loom without
    hating yourself too much

11
Moving to a Purer MVC Later
  • When JSP gets too complex or when more parallel
    development is necessary
  • Move to JSP Model 2 - move controller code to a
    servlet, have servlet forward request to JSP
  • Move to Struts - move controller code to a Struts
    action object and use JSP for display
  • Move to Velocity - move controller code to a
    servlet, move HTML to a Velocity template, have
    servlet fill out a Velocity context and evaluate
    the template

12
Summary
  • Keep all business logic in the model!!
  • Loose decoupling gives you some benefits from
    both JSP syntax and MVC design pattern
  • Only adapt the pages that truly need it to pure
    MVC UI-Delegate is better for most pages in most
    projects
  • know when to break your own rules

13
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com