Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Misagh Moayyed
Software Developer
Unicon, Inc.
Copyright Unicon, Inc., 2008. Some rights reserved. This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/
Unicon
2
Agenda
1. Portlet Overview
a) JSR168
b) JSR286: Events, Resources
2. Spring Portlet MVC Framework
a) Framework overview
b) Controllers, View Resolvers, Annotations
c) Exceptions, Internationalization
3. Getting started with Portlet Development
a) Development Environment
b) Plugins, Libraries and Tips
4) Q&A 3
Portlet Overview
4
Definitions
What is a portlet?
A portlet is an application that provides a specific
piece of content (information or service) to be
included as part of a portal page.
What is a portal?
A portal container runs portlets and provides
them with the required runtime environment.
5
Diagram from Java Portlet Specification, Version 2.0 Public Draft
6
uPortal Content Management Portlet
7
Java Portlet Standards
10
Multiple Request Phases
Action Requests
Executed only once
Used to change system state (e.g. form post)
No markup produced
Render Requests
Executed at least once
May be executed repeated
Produces the fragment markup
Results can be cached
12
Portlet.xml
<portlet>
<description xml:lang="en">A Content Management Portlet</description>
<portlet-name>ContentManagementPortlet</portlet-name>
<display-name xml:lang="en">Content Management System</display-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet
</portlet-class>
<init-param>
<name>contextConfigLocation</name>
<value>/WEB-INF/context/ContentManagementPortlet-context.xml</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>*/*</mime-type>
<portlet-mode>VIEW</portlet-mode>
<portlet-mode>EDIT</portlet-mode>
<portlet-mode>HELP</portlet-mode>
<portlet-mode>CONFIG</portlet-mode>
</supports>
13
</portlet>
Portlet URL Handling
14
JSR 286 Features
Portlet Events
Resource Serving
Portlet Filters
Caching Changes
15
Portlet Events
16
Portlet Events
17
EventPortlet Interface
javax.portlet.EventPortlet Interface
May be implemented by a Portlet
Contains one method:
void processEvent(EventRequest, EventResponse)
EventRequest object provides event payload and
other typical portlet info (mode, window state,
etc)
processEvent is similar to processAction for
copying renderParameters
18
Publishing Events
19
Event Definitions
Event naming:
Must use the W3C QName standard
Receiving events can end with a * wildcard
Can declare default-event-namespace in
portlet.xml and just use local names
20
Supported Events in Portlet.xml
<portlet>
<supported-processing-event>
<qname xmlns:x="https://source.jasig.org/schemas/uportal/search">
x:SearchRequest
</qname>
</supported-processing-event>
<supported-publishing-event>
<qname xmlns:x="https://source.jasig.org/schemas/uportal/search">
x:SearchResults
</qname>
</supported-publishing-event>
</portlet>
21
Event Definitions in Portlet.xml
<portlet-app>
<event-definition>
<qname xmlns:x="https://source.jasig.org/schemas/uportal/search">
x:SearchRequest
</qname>
<value-type>org.jasig.portal.search.SearchRequest</value-type>
</event-definition>
</portlet-app>
22
Resource Serving
23
Resource Serving
Direct Links
More efficient
Not guaranteed to go through Portal
Will not have portal context available
No portal access control
ResourceServingPortlet Interface
void serveResource
(ResourceRequest, ResourceResponse)
26
Using A Framework
27
Spring MVC
Lookup Generate
ModelAndView
Request Dispatcher Map (Model)
Return
(Servlet/Portlet)
Response
viewName
Render
Lookup
Select View
View Resolver
29
Spring Controllers
30
Spring Views
32
Spring Controllers
33
Spring Controllers
Controller (The Interface)
AbstractController
SimpleFormController
AbstractWizardFormController
PortletWrappingController
PortletModeNameViewController
Several others!
Use @Controller
@Controller
@RequestMapping("CONFIG")
public class ConfigController { ... }
35
Spring Handler Mappings
36
HandlerMapping
PortletModeHandlerMapping
Map to a Controller based on current PortletMode
ParameterHandlerMapping
Map to a Controller based on a Parameter value
PortletModeParameterHandlerMapping
Map to a Controller based on current PortletMode
and a Parameter value
Or create your own custom HandlerMapping
37
PortletModeHandlerMapping
<bean id="portletModeHandlerMapping"
class="org.springframework.web.portlet.handler.
PortletModeHandlerMapping">
<property name="portletModeMap">
<map>
<entry key="view" value-ref="viewController"/>
<entry key="edit" value-ref="editController"/>
<entry key="help" value-ref="helpController"/>
</map>
</property>
</bean>
<bean id="viewController" class="ViewController"/>
...
38
HandlerMapping with Annotations
@RequestMapping @ModelAttribute
@ActionMapping @RequestParam
@RenderMapping
...
@Controller
@RequestMapping("HELP")
public class HelpController extends AbstractPortletController {
@RequestMapping
public String getHelpView() {
logDebug("Forwarding request to help view...");
return "help";
}
}
39
Internationalization
40
Internationalization
button.home = Home
button.edit = Edit
button.next = Next
button.previous = Previous
button.finish = Finish
button.cancel = Cancel
exception.notAuthorized.title = Access Not Permitted
exception.notAuthorized.message = You do not have
permission to access this area.
41
MessageSource
42
Using Messages in Views
JSP example:
<%@ taglib prefix="spring"
uri="http://www.springframework.org/tags" %>
...
<p><spring:message
code="exception.notAuthorized.title"/></p>
43
Localization
44
Portlet Exception Handling
with Spring
45
Exception Handling
46
Exception Handling with Annotations
Annotate handler method with
@ExceptionHandler
@Controller
public class SimpleController {
@ExceptionHandler(IOException.class)
public String handleIOException(IOException ex,
HttpServletRequest request) {
return ClassUtils.getShortName(ex.getClass());
}
}
48
Handling Portlet Events
@EventMapping: Map Portlet event requests
onto handler methods
@EventMapping(SearchConstants.SEARCH_REQUEST_QNAME_STRING)
protected void searchContent(EventRequest request,
EventResponse response) {
final Event event = request.getEvent();
final SearchRequest query = (SearchRequest) event.getValue();
res.getSearchResult().add(searchResult);
response.setEvent(SearchConstants.SEARCH_RESULTS_QNAME, res);
}
}
49
Handling Resource Requests
Create a ResourceURL targetting the Portlet:
ResourceUrl url = renderResponse.createResourceURL()
@ResourceMapping
@ResourceMapping
public void handleResourceRequest(ResourceRequest request,
ResourceResponse response) {
...
50
Getting started with
Portlet Development
51
Tools And Libraries
Development Environment
Eclipse, IntelliJ, etc
Apache Maven
Manage builds and dependencies
Apache Ant
Automate portlet deployment tasks
53
uMobile Maven Portlet Archetype
54
uPortal Portlet Deployment
deployPortetApp Ant target
Plutofies the portlet web application file
$ ant deployPortletApp
-DportletApp=/path/to/portet/target/YourPortlet.war
55
uPortal Portlet Deployment
Remote Application
debugger for the
portlet project
59
Javascript-Enabled Portlets
60
Javascript-Enabled Portlets
62
Javascript-Enabled Portlets
<script src=<c:url value=rs/jquery/1.3.2/jquery.min.js />
<c:set var=n><portlet-namespace/></c:set>
<div id=${n}container>
<script type=text/javascript>
var ${n} = {}
${n}.jQuery = jQuery.noConflict(true);
${n}.jQuery(function() {
//Interesting Javascript code
}
</script>
63
Javascript-Enabled Portlets
Use <c:url> to refer to resources in your
webapp
jQuery
http://jQuery.com
jQuery UI
http://jqueryui.com
jQuery plugins
http://plugins.jquery.com
Fluid Project
http://www.fluidproject.org/products/infusion
65
AJAX callbacks using AJAX
Portlet Support
66
AJAX Portlet Support
67
AJAX Portlet Support
AjaxPortletSupport Addon Library
<dependency>
<groupId>org.jasig</groupId>
<artifactId>AjaxPortletSupport</artifactId>
<version>1.0.9</version>
</dependency>
JsonView in applicationContext.xml to
represent the model object as a JSON
68
AJAX Portlet Support
Make an AJAX POST call to an ActionURL
@RequestMapping(params="action=ajaxTest")
public void ajaxTest(ActionRequest request,
ActionResponse response)
throws Exception {
final Map<String, Object> model =
new HashMap<String, Object>();
model.put("testData", "foo");
ajaxPortletSupportService.redirectAjaxResponse
(model, request, response);
}
69
Prevent XSS Attacks using
OWASP AntiSamy
70
OWASP AntiSamy
71
OWASP AntiSamy
72
Resources
73
Resources
OWASP AntiSamy
https://www.owasp.org
More Examples:
74
https://github.com/Jasig
Q&A
Misagh Moayyed
Software Developer
Unicon, Inc.
75