SlideShare a Scribd company logo
1 of 48
Developing Portlets With the Spring MVC Framework
Presented by: Michael Slaven
Teachers College, Columbia University
March 21st, 2011
Session ID 4271
Session Rules of Etiquette
• Please turn off your cell phone/pager
• If you must leave the session early, be sure not
to knock over anyone’s coffee.
• If you must yawn or snore, please do so as
discreetly as possible.
Session ID 4271 2
Agenda
• Some examples of portlets built using the Spring
MVC framework
• Why develop portlets?
• Why use Spring MVC?
• An overview of portlets and the JSR portlet specs
—request phases
—portlet modes
—window states
—URL handling
• An overview of Spring MVC
—model, view, controller
Session ID 4271 3
Agenda - continued
• Putting it all together
—Setting up a dev environment and directory structure
—Configuring Spring and annotations
—Creating the ViewController
—Creating the DirectoryService
—Create the JSP views “search” and “searchresults”
—Deployment
Session ID 4271 4
About Teachers College
• The oldest and largest graduate
school of education in the U.S.
• Affiliated with Columbia
University
• Located in the northern end of
Manhattan
• ~5400 students
• ~1000 staff
• 165 full time faculty
Session ID 4271 5
Some examples of portlets built using
the Spring MVC framework
The myTC Message Center
Our most recent (and biggest) Spring MVC portlet
Session ID 4271 7
Session ID 4271 8
Session ID 4271 9
Features of the myTC Message Center
• Uses Spring 2.5
• Hibernate for data persistence
• Sends emails instantly and generates daily digest
emails for each user
• Emails use Apache Velocity templating engine
Session ID 4271 10
The myTC Photo Directory
Our first (and smallest)
Spring MVC portlet
“More Options” allows search by
title, office, phone, email, role
(staff, student or faculty)
Session ID 4271 11
Session ID 4271 12
Why portlets?
Why Spring MVC?
Why develop portlets?
• Make your code portable across portal products.
— The JSR-168 portlet API is supported by all major
portal products
• Easily re-use code by having multiple instances.
• If you want to do custom development in Luminis 5,
you have to.
(Lum 5 doesn’t support channels)
Session ID 4271 13
Why use the Spring MVC framework?
• It separates presentation from business logic.
• It uses dependency injection for simplification and to
increase testability.
• It lets you focus on functionality instead of
infrastructure.
• Spring's view resolution is very flexible.
—Out of the box, Spring enables you to use JSPs, Velocity
templates and XSLT views, among others.
Session ID 4271 14
An overview of the JSR portlet specs
JSR 168 and JSR 286 Portlet Specs
• JSR 168 aka Portlet 1.0 specification
—released Oct 2003
— supported by Luminis 4.2 and Lum 5
• JSR 286 aka Portlet 2.0 specification
—released Jun 2008
—supported by Luminis 5
Session ID 4271 16
Request Phases
• Action requests
—can be used to change system state
—executed only once
—produces no markup
• Render requests
—Cannot change system state
—executed at least once
—produces markup
• Event requests (Portlet 2.0 only)
• Resource requests (Portlet 2.0 only)
Session ID 4271 17
Request Handling Sequence (from JSR 286 spec)
Session ID 4271 18
Portlet Modes
• Modes specify the current function of the portlet.
• There are three standard portlet modes:
—VIEW: normal mode
—EDIT: edit mode, for portlet specific preferences
—HELP: help mode, for portlet specific help
• All 3 are not necessary. Some portlets have only a
VIEW mode.
• Portlets can have other custom-defined modes as
well.
• These can be changed by the user, usually with an
icon in the portlet’s toolbar, or they can be changed
programmatically.
Session ID 4271 19
Window States
• Window States specify the size or amount of detail.
• There are three standard windows states:
—NORMAL: normal window state
—MAXIMIZED: largest window, usually fills the portal
window
—MINIMIZED: smallest window, usually only a toolbar
remains
• These can be changed by the user, usually with an
icon in the portlet’s toolbar, or they can be changed
programmatically.
Session ID 4271 20
URL Handling
• There is no portlet URL you can link to, since the
portal controls the actual URL.
• Use the API for generating URLs and setting URL
parameters (PortletURL Objects).
• Use a combination of Portlet Mode, Window State,
and Request Parameters for navigation.
Session ID 4271 21
An overview of the Spring MVC framework
What is Spring MVC?
• Spring is a flexible and lightweight application
framework that utilizes dependency injection.
• MVC is a common design pattern that isolates the
data (model) and domain logic (controller) from
the user interface (view).
Session ID 4271 23
Model – The M in MVC
• The model encapsulates the data and application
state.
• It responds to requests for information about its
state (usually coming from the view).
• It responds to instructions to change state (usually
coming from the controller).
Session ID 4271 24
Views – The V in MVC
• The view displays the user interface and renders the
model for display.
• It also sends user actions to the controller.
• JSP & JSTL are the most common view
technologies, but XSLT, Velocity, PDF Docs, etc. are
all supported.
• In portlets you generally only use view technology
that produces HTML markup.
• Multiple views can exist for a single model for
different purposes.
Session ID 4271 25
Controllers – The C in MVC
• A controller processes user input from a view and
initiates a response by making changes to the
model.
• It selects the view to use for the response.
• It often delegates to the service layer for the
business logic.
• There are often multiple controllers.
Session ID 4271 26
Putting it all together
Photo Directory “Lite”
• Our example is a person search that returns a
thumbnail image of a person as well as their contact
info and office hours (if applicable).
• Has 2 views:
—SEARCH: a form to enter first name, last name, etc
—SEARCHRESULTS: the results, paginated 5 at a time
• Has a single domain object:
—DirectoryEntry: setters and getters for a single entry in the
photo directory
• Has a single controller
• Has a single DB service object
Session ID 4271 28
Notes on setting up a dev environment
• Install the JDK and set JAVA_HOME
—JRE is not enough
• Install an IDE
—Eclipse
—NetBeans
—Use Maven to simplify dependencies, building, and
deploying
• Install a portal / portlet container
—uPortal 2.5.6 is best if developing for Lum 4
—Liferay 5.2.x is best if developing for Lum 5
—Or you can use Pluto from Apache
Session ID 4271 29
Set up the directory
structure
• /WEB-INF -- all protected resources
• Individual class files should be stored
in a directory structure within /WEB-
INF/classes that reflects the class
package.
• /WEB-INF/jsp – jsp views
• /WEB-INF/lib – referenced JAR files
Session ID 4271 30
Include the Spring libraries
Session ID 4271 31
• Use Maven to handle your dependencies.
• This is done using the “Project Object Model” pom.xml file at
the root of your project.
• Maven then downloads all necessary libraries and places them
in WEB-INF/lib.
Configure portlet.xml
• The Spring DispatcherPortlet dispatches requests to
your controllers.
• This will be the same for any Spring portlet.
Session ID 4271 32
Configure web.xml
• Spring portlets are an extension of spring servlets.
• DispatcherPortlet uses ViewRendererServlet to
dispatch view rendering into a servlet context.
• Also, load the parent ApplicationContext using
ContextLoaderListener.
Session ID 4271 33
Configure PhotoDirectory-portlet.xml
-- Annotations --
• Setup DefaultAnnotationHandlerMapping.
• Tell Spring where to scan for annotations like
@Controller, @RequestMapping, @Autowired etc.
Session ID 4271 34
Configure PhotoDirectory-portlet.xml
-- View Resolver --
• Using JstlView we don’t have to build views ourselves.
• Refer to a view by name, and the jsp file of that name
becomes the view.
• The prefix and suffix are added, so that the view “search”
becomes /WEB-INF/jsp/search.jsp
Session ID 4271 35
Controller & Handler Mapping
• With Spring 2.5+ we can simplify by using
annotations.
• Without annotations:
—The controller needs to extend a base controller
like AbstractController.
—Handler beans have to be defined in the application
context.
• With annotations:
—Annotate the controller class with “@Controller”.
—Annotate handlers with
“@RequestMapping([params])”.
Session ID 4271 36
ViewController
Session ID 4271 37
ViewController - Continued
Session ID 4271 38
DirectoryServiceImpl.java
Session ID 4271 39
DirectoryServiceImpl.java - Continued
Session ID 4271 4040
DirectoryServiceImpl.java - Continued
Session ID 4271 41
VIEW: search.jsp
Session ID 4271 42
VIEW: searchresults.jsp
Session ID 4271 43
Deploy!
• Use Maven or Ant to package your portlet into a
deployable war file.
• On Lum 4
—Run cptool deploy portlet /path/to/PhotoDirectory.war
—A restart is required.
• On Lum 5 and Liferay
—Simply copy the war file to your hot deploy directory, or
upload using the Admin GUI.
• On uPortal 2.5
—Run ant deployPortletApp -DportletApp=
/path/to/PhotoDirectory.war
Session ID 4271 44
Summary
The portlet API is defined by the JSR portlet specs:
—request phases
—portlet modes
—window states
—URL handling
• Portlets can be built once, and deployed to any compliant
portal with little or no modification.
• MVC is a design pattern that separates presentation from
business logic
• Portlets built using the Spring MVC framework can be
simple or complex. Using JSP views you could create a
portlet that doesn’t require any java coding.
Session ID 4271 4545
Summary - Continued
• To deploy a Spring MVC portlet
— setup a dev environment
— setup the directory structure
— include the spring libraries
— configure the application context (portlet, web, and
myPortlet-portlet.xml)
— use annotations to define controller and handler mappings
— create the controllers and domain objects
— create the service layer
— create the views (we used JSP views)
— deploy!
Session ID 4271 46
Questions & Answers
?
Session ID 4271 47
Session ID 4271 48
SunGard or its subsidiaries in the U.S. and other countries is the owner of numerous marks, including “SunGard,” the SunGard logo, “Banner,” “PowerCAMPUS,”
“Advance,” “Luminis,” "DegreeWorks," "fsaATLAS," “Course Signals,” and “Open Digital Campus.” Other names and marks used in this material are owned by third parties.
© 2011 SunGard. All rights reserved.
Session Sponsor
Thank You!
Michael Slaven
slaven@tc.edu
Please complete the online Session evaluation form
Session ID 4271

More Related Content

What's hot

Software Design Principles (SOLID)
Software Design Principles (SOLID)Software Design Principles (SOLID)
Software Design Principles (SOLID)ASIMYILDIZ
 
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012Jagadish Prasath
 
Building Enterprise Application with J2EE
Building Enterprise Application with J2EEBuilding Enterprise Application with J2EE
Building Enterprise Application with J2EECalance
 
All the Java ADF beginners need to know - part1
All the Java ADF beginners need to know - part1All the Java ADF beginners need to know - part1
All the Java ADF beginners need to know - part1Markus Eisele
 
F428435966 odtug web-logic for developers
F428435966 odtug   web-logic for developersF428435966 odtug   web-logic for developers
F428435966 odtug web-logic for developersMeng He
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologySimon Ritter
 
Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Rohit Kelapure
 
Developing Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologyDeveloping Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologySimon Ritter
 
Haj 4308-open jpa, eclipselink, and the migration toolkit
Haj 4308-open jpa, eclipselink, and the migration toolkitHaj 4308-open jpa, eclipselink, and the migration toolkit
Haj 4308-open jpa, eclipselink, and the migration toolkitKevin Sutter
 
Types of Drivers in JDBC
Types of Drivers in JDBCTypes of Drivers in JDBC
Types of Drivers in JDBCHemant Sharma
 

What's hot (20)

Spring framework
Spring frameworkSpring framework
Spring framework
 
Oracle History #5
Oracle History #5Oracle History #5
Oracle History #5
 
Software Design Principles (SOLID)
Software Design Principles (SOLID)Software Design Principles (SOLID)
Software Design Principles (SOLID)
 
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
 
Building Enterprise Application with J2EE
Building Enterprise Application with J2EEBuilding Enterprise Application with J2EE
Building Enterprise Application with J2EE
 
All the Java ADF beginners need to know - part1
All the Java ADF beginners need to know - part1All the Java ADF beginners need to know - part1
All the Java ADF beginners need to know - part1
 
F428435966 odtug web-logic for developers
F428435966 odtug   web-logic for developersF428435966 odtug   web-logic for developers
F428435966 odtug web-logic for developers
 
EJB 3.1 by Bert Ertman
EJB 3.1 by Bert ErtmanEJB 3.1 by Bert Ertman
EJB 3.1 by Bert Ertman
 
Java ee introduction
Java ee introductionJava ee introduction
Java ee introduction
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans Technology
 
Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010
 
Developing Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologyDeveloping Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java Technology
 
Hybernat and structs, spring classes in mumbai
Hybernat and structs, spring classes in mumbaiHybernat and structs, spring classes in mumbai
Hybernat and structs, spring classes in mumbai
 
Jdbc
JdbcJdbc
Jdbc
 
Haj 4308-open jpa, eclipselink, and the migration toolkit
Haj 4308-open jpa, eclipselink, and the migration toolkitHaj 4308-open jpa, eclipselink, and the migration toolkit
Haj 4308-open jpa, eclipselink, and the migration toolkit
 
Enterprise JavaBeans(EJB)
Enterprise JavaBeans(EJB)Enterprise JavaBeans(EJB)
Enterprise JavaBeans(EJB)
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Types of Drivers in JDBC
Types of Drivers in JDBCTypes of Drivers in JDBC
Types of Drivers in JDBC
 
Jdbc driver types
Jdbc driver typesJdbc driver types
Jdbc driver types
 
Introduction to EJB
Introduction to EJBIntroduction to EJB
Introduction to EJB
 

Viewers also liked

IEEE Day 2013 Baku - Spring Framework overview and Spring MVC
IEEE Day 2013 Baku - Spring Framework overview and Spring MVCIEEE Day 2013 Baku - Spring Framework overview and Spring MVC
IEEE Day 2013 Baku - Spring Framework overview and Spring MVCIlkin Ashrafli
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC frameworkMohit Gupta
 
Presentation Spring, Spring MVC
Presentation Spring, Spring MVCPresentation Spring, Spring MVC
Presentation Spring, Spring MVCNathaniel Richand
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Tuna Tore
 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVCDzmitry Naskou
 
Introdução ao Spring Framework MVC
Introdução ao Spring Framework MVCIntrodução ao Spring Framework MVC
Introdução ao Spring Framework MVCMessias Batista
 
Application de gestion des projets en J2EE (Spring-Hibernate) avec architectu...
Application de gestion des projets en J2EE (Spring-Hibernate) avec architectu...Application de gestion des projets en J2EE (Spring-Hibernate) avec architectu...
Application de gestion des projets en J2EE (Spring-Hibernate) avec architectu...Saâd Zerhouni
 
Conférence #nwxtech2 : Les "design pattern" MVC appliqués au jeu vidéo par Al...
Conférence #nwxtech2 : Les "design pattern" MVC appliqués au jeu vidéo par Al...Conférence #nwxtech2 : Les "design pattern" MVC appliqués au jeu vidéo par Al...
Conférence #nwxtech2 : Les "design pattern" MVC appliqués au jeu vidéo par Al...Normandie Web Xperts
 

Viewers also liked (13)

IEEE Day 2013 Baku - Spring Framework overview and Spring MVC
IEEE Day 2013 Baku - Spring Framework overview and Spring MVCIEEE Day 2013 Baku - Spring Framework overview and Spring MVC
IEEE Day 2013 Baku - Spring Framework overview and Spring MVC
 
J2 ee
J2 eeJ2 ee
J2 ee
 
2 ModéLe Mvc
2 ModéLe Mvc2 ModéLe Mvc
2 ModéLe Mvc
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC framework
 
Presentation Spring, Spring MVC
Presentation Spring, Spring MVCPresentation Spring, Spring MVC
Presentation Spring, Spring MVC
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5
 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVC
 
Introdução ao Spring Framework MVC
Introdução ao Spring Framework MVCIntrodução ao Spring Framework MVC
Introdução ao Spring Framework MVC
 
Presentation Spring
Presentation SpringPresentation Spring
Presentation Spring
 
Application de gestion des projets en J2EE (Spring-Hibernate) avec architectu...
Application de gestion des projets en J2EE (Spring-Hibernate) avec architectu...Application de gestion des projets en J2EE (Spring-Hibernate) avec architectu...
Application de gestion des projets en J2EE (Spring-Hibernate) avec architectu...
 
Spring MVC 3.0 Framework
Spring MVC 3.0 FrameworkSpring MVC 3.0 Framework
Spring MVC 3.0 Framework
 
Conférence #nwxtech2 : Les "design pattern" MVC appliqués au jeu vidéo par Al...
Conférence #nwxtech2 : Les "design pattern" MVC appliqués au jeu vidéo par Al...Conférence #nwxtech2 : Les "design pattern" MVC appliqués au jeu vidéo par Al...
Conférence #nwxtech2 : Les "design pattern" MVC appliqués au jeu vidéo par Al...
 
Support de cours Spring M.youssfi
Support de cours Spring  M.youssfiSupport de cours Spring  M.youssfi
Support de cours Spring M.youssfi
 

Similar to Developing Portlets

Spring MVC framework features and concepts
Spring MVC framework features and conceptsSpring MVC framework features and concepts
Spring MVC framework features and conceptsAsmaShaikh478737
 
Chapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration TechnologiesChapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration TechnologiesIt Academy
 
MVC + ORM (with project implementation)
MVC + ORM (with project implementation)MVC + ORM (with project implementation)
MVC + ORM (with project implementation)Prateek Chauhan
 
Introduction to j2 ee frameworks
Introduction to j2 ee frameworksIntroduction to j2 ee frameworks
Introduction to j2 ee frameworksMukesh Kumar
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsmichaelaaron25322
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework tola99
 
Sprint Portlet MVC Seminar
Sprint Portlet MVC SeminarSprint Portlet MVC Seminar
Sprint Portlet MVC SeminarJohn Lewis
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework Serhat Can
 
Asp 1-mvc introduction
Asp 1-mvc introductionAsp 1-mvc introduction
Asp 1-mvc introductionFajar Baskoro
 
DEVNET-1166 Open SDN Controller APIs
DEVNET-1166	Open SDN Controller APIsDEVNET-1166	Open SDN Controller APIs
DEVNET-1166 Open SDN Controller APIsCisco DevNet
 
Cloud compiler - Minor Project by students of CBPGEC
Cloud compiler - Minor Project by students of CBPGEC  Cloud compiler - Minor Project by students of CBPGEC
Cloud compiler - Minor Project by students of CBPGEC vipin kumar
 
Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVCJohn Lewis
 
spring framework ppt by Rohit malav
spring framework ppt by Rohit malavspring framework ppt by Rohit malav
spring framework ppt by Rohit malavRohit malav
 
Framework adoption for java enterprise application development
Framework adoption for java enterprise application developmentFramework adoption for java enterprise application development
Framework adoption for java enterprise application developmentClarence Ho
 

Similar to Developing Portlets (20)

Spring MVC framework features and concepts
Spring MVC framework features and conceptsSpring MVC framework features and concepts
Spring MVC framework features and concepts
 
Chapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration TechnologiesChapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration Technologies
 
MVC + ORM (with project implementation)
MVC + ORM (with project implementation)MVC + ORM (with project implementation)
MVC + ORM (with project implementation)
 
Introduction to j2 ee frameworks
Introduction to j2 ee frameworksIntroduction to j2 ee frameworks
Introduction to j2 ee frameworks
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applications
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Sprint Portlet MVC Seminar
Sprint Portlet MVC SeminarSprint Portlet MVC Seminar
Sprint Portlet MVC Seminar
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Asp 1a-aspnetmvc
Asp 1a-aspnetmvcAsp 1a-aspnetmvc
Asp 1a-aspnetmvc
 
Aspnetmvc 1
Aspnetmvc 1Aspnetmvc 1
Aspnetmvc 1
 
70487.pdf
70487.pdf70487.pdf
70487.pdf
 
Asp 1-mvc introduction
Asp 1-mvc introductionAsp 1-mvc introduction
Asp 1-mvc introduction
 
DEVNET-1166 Open SDN Controller APIs
DEVNET-1166	Open SDN Controller APIsDEVNET-1166	Open SDN Controller APIs
DEVNET-1166 Open SDN Controller APIs
 
Cloud compiler - Minor Project by students of CBPGEC
Cloud compiler - Minor Project by students of CBPGEC  Cloud compiler - Minor Project by students of CBPGEC
Cloud compiler - Minor Project by students of CBPGEC
 
Ra framework 0.1
Ra framework 0.1Ra framework 0.1
Ra framework 0.1
 
Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVC
 
spring framework ppt by Rohit malav
spring framework ppt by Rohit malavspring framework ppt by Rohit malav
spring framework ppt by Rohit malav
 
Maven tutorial
Maven tutorialMaven tutorial
Maven tutorial
 
Framework adoption for java enterprise application development
Framework adoption for java enterprise application developmentFramework adoption for java enterprise application development
Framework adoption for java enterprise application development
 
Java Spring Framework
Java Spring FrameworkJava Spring Framework
Java Spring Framework
 

Developing Portlets

  • 1. Developing Portlets With the Spring MVC Framework Presented by: Michael Slaven Teachers College, Columbia University March 21st, 2011 Session ID 4271
  • 2. Session Rules of Etiquette • Please turn off your cell phone/pager • If you must leave the session early, be sure not to knock over anyone’s coffee. • If you must yawn or snore, please do so as discreetly as possible. Session ID 4271 2
  • 3. Agenda • Some examples of portlets built using the Spring MVC framework • Why develop portlets? • Why use Spring MVC? • An overview of portlets and the JSR portlet specs —request phases —portlet modes —window states —URL handling • An overview of Spring MVC —model, view, controller Session ID 4271 3
  • 4. Agenda - continued • Putting it all together —Setting up a dev environment and directory structure —Configuring Spring and annotations —Creating the ViewController —Creating the DirectoryService —Create the JSP views “search” and “searchresults” —Deployment Session ID 4271 4
  • 5. About Teachers College • The oldest and largest graduate school of education in the U.S. • Affiliated with Columbia University • Located in the northern end of Manhattan • ~5400 students • ~1000 staff • 165 full time faculty Session ID 4271 5
  • 6. Some examples of portlets built using the Spring MVC framework
  • 7. The myTC Message Center Our most recent (and biggest) Spring MVC portlet Session ID 4271 7
  • 10. Features of the myTC Message Center • Uses Spring 2.5 • Hibernate for data persistence • Sends emails instantly and generates daily digest emails for each user • Emails use Apache Velocity templating engine Session ID 4271 10
  • 11. The myTC Photo Directory Our first (and smallest) Spring MVC portlet “More Options” allows search by title, office, phone, email, role (staff, student or faculty) Session ID 4271 11
  • 12. Session ID 4271 12 Why portlets? Why Spring MVC?
  • 13. Why develop portlets? • Make your code portable across portal products. — The JSR-168 portlet API is supported by all major portal products • Easily re-use code by having multiple instances. • If you want to do custom development in Luminis 5, you have to. (Lum 5 doesn’t support channels) Session ID 4271 13
  • 14. Why use the Spring MVC framework? • It separates presentation from business logic. • It uses dependency injection for simplification and to increase testability. • It lets you focus on functionality instead of infrastructure. • Spring's view resolution is very flexible. —Out of the box, Spring enables you to use JSPs, Velocity templates and XSLT views, among others. Session ID 4271 14
  • 15. An overview of the JSR portlet specs
  • 16. JSR 168 and JSR 286 Portlet Specs • JSR 168 aka Portlet 1.0 specification —released Oct 2003 — supported by Luminis 4.2 and Lum 5 • JSR 286 aka Portlet 2.0 specification —released Jun 2008 —supported by Luminis 5 Session ID 4271 16
  • 17. Request Phases • Action requests —can be used to change system state —executed only once —produces no markup • Render requests —Cannot change system state —executed at least once —produces markup • Event requests (Portlet 2.0 only) • Resource requests (Portlet 2.0 only) Session ID 4271 17
  • 18. Request Handling Sequence (from JSR 286 spec) Session ID 4271 18
  • 19. Portlet Modes • Modes specify the current function of the portlet. • There are three standard portlet modes: —VIEW: normal mode —EDIT: edit mode, for portlet specific preferences —HELP: help mode, for portlet specific help • All 3 are not necessary. Some portlets have only a VIEW mode. • Portlets can have other custom-defined modes as well. • These can be changed by the user, usually with an icon in the portlet’s toolbar, or they can be changed programmatically. Session ID 4271 19
  • 20. Window States • Window States specify the size or amount of detail. • There are three standard windows states: —NORMAL: normal window state —MAXIMIZED: largest window, usually fills the portal window —MINIMIZED: smallest window, usually only a toolbar remains • These can be changed by the user, usually with an icon in the portlet’s toolbar, or they can be changed programmatically. Session ID 4271 20
  • 21. URL Handling • There is no portlet URL you can link to, since the portal controls the actual URL. • Use the API for generating URLs and setting URL parameters (PortletURL Objects). • Use a combination of Portlet Mode, Window State, and Request Parameters for navigation. Session ID 4271 21
  • 22. An overview of the Spring MVC framework
  • 23. What is Spring MVC? • Spring is a flexible and lightweight application framework that utilizes dependency injection. • MVC is a common design pattern that isolates the data (model) and domain logic (controller) from the user interface (view). Session ID 4271 23
  • 24. Model – The M in MVC • The model encapsulates the data and application state. • It responds to requests for information about its state (usually coming from the view). • It responds to instructions to change state (usually coming from the controller). Session ID 4271 24
  • 25. Views – The V in MVC • The view displays the user interface and renders the model for display. • It also sends user actions to the controller. • JSP & JSTL are the most common view technologies, but XSLT, Velocity, PDF Docs, etc. are all supported. • In portlets you generally only use view technology that produces HTML markup. • Multiple views can exist for a single model for different purposes. Session ID 4271 25
  • 26. Controllers – The C in MVC • A controller processes user input from a view and initiates a response by making changes to the model. • It selects the view to use for the response. • It often delegates to the service layer for the business logic. • There are often multiple controllers. Session ID 4271 26
  • 27. Putting it all together
  • 28. Photo Directory “Lite” • Our example is a person search that returns a thumbnail image of a person as well as their contact info and office hours (if applicable). • Has 2 views: —SEARCH: a form to enter first name, last name, etc —SEARCHRESULTS: the results, paginated 5 at a time • Has a single domain object: —DirectoryEntry: setters and getters for a single entry in the photo directory • Has a single controller • Has a single DB service object Session ID 4271 28
  • 29. Notes on setting up a dev environment • Install the JDK and set JAVA_HOME —JRE is not enough • Install an IDE —Eclipse —NetBeans —Use Maven to simplify dependencies, building, and deploying • Install a portal / portlet container —uPortal 2.5.6 is best if developing for Lum 4 —Liferay 5.2.x is best if developing for Lum 5 —Or you can use Pluto from Apache Session ID 4271 29
  • 30. Set up the directory structure • /WEB-INF -- all protected resources • Individual class files should be stored in a directory structure within /WEB- INF/classes that reflects the class package. • /WEB-INF/jsp – jsp views • /WEB-INF/lib – referenced JAR files Session ID 4271 30
  • 31. Include the Spring libraries Session ID 4271 31 • Use Maven to handle your dependencies. • This is done using the “Project Object Model” pom.xml file at the root of your project. • Maven then downloads all necessary libraries and places them in WEB-INF/lib.
  • 32. Configure portlet.xml • The Spring DispatcherPortlet dispatches requests to your controllers. • This will be the same for any Spring portlet. Session ID 4271 32
  • 33. Configure web.xml • Spring portlets are an extension of spring servlets. • DispatcherPortlet uses ViewRendererServlet to dispatch view rendering into a servlet context. • Also, load the parent ApplicationContext using ContextLoaderListener. Session ID 4271 33
  • 34. Configure PhotoDirectory-portlet.xml -- Annotations -- • Setup DefaultAnnotationHandlerMapping. • Tell Spring where to scan for annotations like @Controller, @RequestMapping, @Autowired etc. Session ID 4271 34
  • 35. Configure PhotoDirectory-portlet.xml -- View Resolver -- • Using JstlView we don’t have to build views ourselves. • Refer to a view by name, and the jsp file of that name becomes the view. • The prefix and suffix are added, so that the view “search” becomes /WEB-INF/jsp/search.jsp Session ID 4271 35
  • 36. Controller & Handler Mapping • With Spring 2.5+ we can simplify by using annotations. • Without annotations: —The controller needs to extend a base controller like AbstractController. —Handler beans have to be defined in the application context. • With annotations: —Annotate the controller class with “@Controller”. —Annotate handlers with “@RequestMapping([params])”. Session ID 4271 36
  • 44. Deploy! • Use Maven or Ant to package your portlet into a deployable war file. • On Lum 4 —Run cptool deploy portlet /path/to/PhotoDirectory.war —A restart is required. • On Lum 5 and Liferay —Simply copy the war file to your hot deploy directory, or upload using the Admin GUI. • On uPortal 2.5 —Run ant deployPortletApp -DportletApp= /path/to/PhotoDirectory.war Session ID 4271 44
  • 45. Summary The portlet API is defined by the JSR portlet specs: —request phases —portlet modes —window states —URL handling • Portlets can be built once, and deployed to any compliant portal with little or no modification. • MVC is a design pattern that separates presentation from business logic • Portlets built using the Spring MVC framework can be simple or complex. Using JSP views you could create a portlet that doesn’t require any java coding. Session ID 4271 4545
  • 46. Summary - Continued • To deploy a Spring MVC portlet — setup a dev environment — setup the directory structure — include the spring libraries — configure the application context (portlet, web, and myPortlet-portlet.xml) — use annotations to define controller and handler mappings — create the controllers and domain objects — create the service layer — create the views (we used JSP views) — deploy! Session ID 4271 46
  • 48. Session ID 4271 48 SunGard or its subsidiaries in the U.S. and other countries is the owner of numerous marks, including “SunGard,” the SunGard logo, “Banner,” “PowerCAMPUS,” “Advance,” “Luminis,” "DegreeWorks," "fsaATLAS," “Course Signals,” and “Open Digital Campus.” Other names and marks used in this material are owned by third parties. © 2011 SunGard. All rights reserved. Session Sponsor Thank You! Michael Slaven slaven@tc.edu Please complete the online Session evaluation form Session ID 4271