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
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
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
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
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
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
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