Using J SR 168 for Portlet Rosario Iannella JSR168 Concepts Rosario Iannella 30/11/2009
Agenda § § § § §
2
Part I: JSR 168 Specification Part II: JSR168 and the MVC Pattern Part III: Spring MVC Portet Part IV: Pattern - Builder
11/30/09
A is Javabased technology based web–commonlycomponent, managed by a runtime portlet container, A portlet portal aa web provides personalization, single that portletis container runs application portlets andthat provides them with the required environment. processes requests and generates dynamic content. Portlets are used to by portals as pluggable sign on, …… A portal may have sophisticated personalization features A portlet container contains portlets and manages their lifecycle. provide customized contentstorage to users user interface components that provide a presentation layer to Information Systems. It also provides persistent for. [PLT.2.1] portlet preferences. A portlet container receives[PLT.2.2] requests
Part I: JSR 168 Specification
from the portal to execute requests on the portlets hosted by it. 30 [PLT.2.3]
Portal Definition § § Portlet Container
Portlet
3
11/30/09
Part I: JSR 168 Specification Portlet
vs.
Servlet
Only generates markup fragments, not complete documents
Usually controls the entire page
Interacts only through a portal system
Not required
A more refined request handling, action requests and render requests
Only httpRequest
Predefined modes and window states that indicate the function the component is performing and the amount of real state in the portal page Exists many times in a portal page
No
Accesses to user profile information
No
Accesses and stores persistent configuration and customization data No
No
No
Setting HTTP headers on the response
No
The URL of the client
4
No
Setting the character set encoding of the response
11/30/09
Part I: JSR 168 Specification § Significant Types:
• • • • •
5
PortletConfig PortletContext PortalContext Portlet PortletSession
11/30/09
Part I: JSR 168 Specification
Portlet Config: The PortletConfig object provides the portlet object with information to be used during initialization. It also provides access to the portlet context and the resource bundle that provides title-bar resources.[PLT 6]
•One per portlet definition •Access to the portlet init parameter •Resource Bundle
6
11/30/09
Part I: JSR 168 Specification
PortletContext: Defines a set of methods that a portlet uses to communicate with its portlet container and defines the portlet’s view of the portlet application within which the portlet is running.
•One context instance per portlet-app per JVM. •Access to Context init parameter •Access to Resource •Request Dispatching
7
11/30/09
Part I: JSR 168 Specification
PortalContext: The PortalContext interface provides information about the portal that is invoking the portlet.
•Portal Vendor and portal version information. •Portal properties •Portal Modes and Windows available •Obtained from the getPortalContext
8
11/30/09
Part I: JSR 168 Specification
Portlet: A portlet is managed through a well defined life cycle that defines how it is loaded, instantiated and initialized, how it handles requests from clients, ….. This life cycle of a portlet is expressed through the init, processAction, render and destroy methods of the Portlet interface…
•Lifecycle: •Init() •Destroy() •Request Processing: •ProcessAction() •RenderRequest 9
11/30/09
Part I: JSR 168 Specification
Request Processing: processAction & RenderRequest Process Action Request
Render Request
Change portlet mode Process render logic Change business data
Read business data for display
Executed on action request
Executed after each request. (render or action)
Does not render / Always dispatches to dispatches to a view a view.
10
11/30/09
Part I: JSR 168 Specification
PortletSession: PortletSession objects must be scoped at the portlet application context level. Each portlet application has its own distinct PortletSession object per user session. The portlet container must not share the PortletSession object or the attributes stored in it among different portlet applications or among different user sessions. PortletSession session = request.getSession(true); URL url = new URL(“http://www.foo.com”); session.setAttribute(“home.url”,url,PortletSession.APPLICATION_SCOPE); session.setAttribute(“bkg.color”,”RED”,PortletSession.PORTLET_SCOPE);
11
11/30/09
Part I: JSR 168 Specification Portlet Mode: •VIEW – It generates the output reflecting the actual portlet status • •HELP – Provides information about the portlet • •EDIT – Provides content and logic to customize the portlet behavior • •CUSTOMS – Defined by the portal vendor for “vendor specific functionality” 12
11/30/09
Part I: JSR 168 Specification Window State: Normal / Maximize / Minimized / Customs •NORMAL
– All render
•MAXIMIZED – One render • •MINIMIZED – Others render • •CUSTOMS – Vendor Specific
13
11/30/09
Part II: JSR168 and the MVC Pattern Model-View-Controller (MVC) software architecture that separates an application's data model, user interface, and control logic into three distinct components so that modifications to the view component can be made with minimal impact to the data model component.
Controller
View
14
Model
Controller
Responds to user events
View
Render typically in user interface
Model
Define the views when the data changes
11/30/09
Part II: JSR168 and the MVC Pattern Portlet: Model-View-Controller (MVC) Portlet MVC key point: •HttpRequest not available •PortletMode and Window State •Portlet depends on Action and Render Request •View render fragment
Portal wraps HttpRequest and handles the Portlet Request. Portlet Request does not have direct access to the URL. 15
11/30/09
Part II: JSR168 and the MVC Pattern MVC: Features
§Request Parameter Mapping §Request Handlers §Request Validation §Views §
16
11/30/09
Part III: Spring MVC Portet Portlet.xml Configuration <portlet> <description>Search Portlet <portlet-name>SearchPortlet SearchPortlet <portlet-class>org.springframework.web.portlet.DispatcherPortlet contextConfigLocation /WEB-INF/context/SearchPortlet-portlet.xml <supports> <mime-type>text/html <portlet-mode>view <portlet-mode>config <supported-locale>en <portlet-info>
Search by category Portlet <short-title>SP Search
17
11/30/09
Part III: Spring MVC Portet SearchPortlet-portlet.xml: HandlerMapping <property name="order" value="0" /> <property name="portletModeParameterMap"> <map> Mode <entry key="config"> ParameterValue <map> <entry key=“queryStr">[ <entry key=“advanced">][ <property name="parameterName" value="setaction" /> ]
18
11/30/09
Part III: Spring MVC Portet SearchPortlet-portlet.xml: HandlerMapping <property name="order" value="1" /> <property name="portletModeMap"> <map> Mode <entry key="config">[ <entry key="view">][ ]
19
11/30/09
Part III: Spring MVC Portet SearchPortlet-portlet.xml: Controller <property name="commandName" value="DisplayCommand" /> <property name="commandClass" value="portlet.command.DisplayCommand" />
20
11/30/09
Part III: Spring MVC Portet SearchPortlet-portlet.xml: View <property name="url" value=“/jsp/searchView.jsp" />
21
11/30/09
Part III: Spring MVC Portet SearchPortlet-portlet.xml: Form Validation public class ConfigValidator implements Validator { public boolean supports(Class clazz) { log.debug(clazz); if (clazz.getName().equals("portlet.command.DisplayCommand")){ return true; } return false; } public void validate(Object objectCommand, Errors errors) { errors.rejectValue(PortletConstants.queryString,"",ErrorMessages.MISSINGVALUE);
22
11/30/09
Part IV: Pattern - Builder
23
11/30/09
Part IV: Pattern - Builder Builder
24
11/30/09
Part IV: Pattern - Builder
BuildContentSource BuildContentType BuildView
BuildByChannel/Byco ntent/ByFeed
BuildLabels
25
11/30/09
Part IV: Pattern - Builder protected ModelAndView showForm(RenderRequest renderRequest, RenderResponse renderResponse, BindException bindException) throws Exception { public void buildOrderBy(String type) { … String contentSource = parameterAdapter.getParameter(PortletConstants.contentSource); SettingPageBuilder pageBuilder = new CQSettingPage(modelAndView, renderRequest); Content content = (Content)ServiceManager.getInstance().getService(contentSource); pageBuilder.buildPage(action); ….
modelAndView.addObject(PortletConstants.orderableAttributes,content.getOrderableAttrib public utes());class CQSettingPage extends AbstractSettingPageBuilder { this.getParameterAdapter().setType(ParameterAdapter.PREFERENCES); modelAndView.addObject(type,parameterAdapter.getParameter(type)); } this.buildContentSource(); this.buildContentType(); this.buildFilterBy(); // build view for type: channel this.buildDisplayView(viewForType); this.buildLabels();
26
11/30/09
Questions ?
27
11/30/09