Transcript
Page 1: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 2007WebSphere Portal Technical Conference U.S. 2007

Session Number: D22Jump START your Portlet development with Open Source Frameworks!

Sean Newby, Perficient

Sunil Patil, Ascendant Technology LLC

Peter Blinstrubas, IBM

Page 2: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 20072

Agenda

IBM Portlet Development Perspective

Struts 2.0

Spring 2.0

Page 3: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 20073

IBM Portlet Development Perspective

Email: [email protected]

Page 4: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 20074

Why Use a Framework for Portlet Development?

Starts off small and later is enhanced.

Model View Controller well established.Provides separation between different layers

Frameworks force developers into using a standard approach when building applications

Shield some of the underlying complexity of developing an application.

Provides interfaces or abstract classes to speed and structure your development effort.

OptionsWrite the portlet in portal apiDevelop a custom framework for portlet development.• Port servlet world framework to the portlet world

Use an existing framework!

Page 5: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 20075

Frameworks for Portlet Development

IBM FrameworksJust the Portlet APIWebSphere Portlet Factory• AJAX based on DOJO

JSF V1.1 on Portal 6.x with JWL 3.0JSF 1.2 for Portal running on WAS 7 (plans subject to change)

• AJAXStruts Apache Bridge for JSR 168 Container on WAS 6.1Currently Struts Portlet Framework supports Struts 1.1 and 1.29

Alternative FrameworksCustom MVCSpring 2.0Struts 2.0Many More… (i.e. Shale, Wicket,…)

Page 6: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 20076

What is the Struts Portlet Framework?

Runtime A package that supports Struts application in Portal.Includes a level of Struts (1.1 and 1.29)A portlet controller and a request processor.Implementations of some of the Struts tags

Tool SetFlow Editor for creating and wiring pages and actions togetherA full Struts-Config editorLink validation and re-factoring for Struts specific linksWizards for creating Actions, Struts JSPs and Form BeansPage Designer Visualizations for some Struts-html tagsFull Page Designer Palette for Struts Tags

Page 7: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 20077

What is the Struts Portlet Framework?

browser

WPS servlet

WpsStrutsPortlet

WpsRequestProcessor

WpsViewCommand

WpsStrutPortlet

WpsViewCommand

Http Request

actionPerformed

service execute

Http Response

stored in session

WpsStrutsPortlet

initActionServlet

init

recursively called to support forwards and redirects

Step One

Step Two

Step Three

Support for writing/migrating Struts applications that can be deployed in WebSphere Portal

Also provides samples to show how portal features can be incorporated by Struts applications

Page 8: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 20078

Struts 1.x in General

Struts is:Controller based framework

Struts does not have:A built-in UI component modelA built-in event model for UI componentsA built-in state management for UI componentsA built-in support of multiple renderers (Struts is

more or less tied up with HTML)

Struts is Not a standard (despite its popularity)

IBM has no plans to continue SPF Development.

Recommended for Legacy Struts Applications.Also consider Apache Bridge

Page 9: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 20079

IBM and JSF

Part of Java EE 5Participation in JSR Expert GroupJ2EE-based Web UI and application framework

WPLC

• Underlying technology in Composite Application Designer• Used in Portal Workflow portlets• Being incorporated into Lotus Expeditor• Widgets in Portlet Factory

Recommended for new portlet development

Note: Refer to session D12 for JSF details

Page 10: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200710

JSF General

BenefitsReusable UI ComponentsCustom Tag LibrariesAPI for managing UI components, events, input navigation, page navigationAddresses complex navigation requirementsExtensible - Everything is pluggableRich tooling supportFlexible Actions: different navigations for multiple components on a single pageSupports Expression Built in state management

ConcernsComplexityStill relatively newOut of the box capability to meet requirementsAvailability of skills

Page 11: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200711

Plug-in to Eclipse & Rational Application Developer

Multi-page custom portlets/applications without coding

Integration capabilities (SAP, Domino, PeopleSoft, Siebel, Web Services, Databases, Portal Content Repository)

Service-oriented development

Support for Portal features such as portlet communication, business user configuration, auto-deploy, single sign-on

“Dynamic profiling” capability, to create multiple variations from a single set of source portlets

WebSphere Portlet Factory Rapid Portlet Creation and Customization Tooling

IBM WebSphere Portlet Factory simplifies & accelerates the development, deployment, maintenance, and reuse of custom portlets and applications.

Page 12: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200712

Portlet Factory General

BenefitsDataCentric ApplicationsExpand Portlet Creation to developers of all skill levelCaptures Design Patterns/Standardize Development ModelLots of adaptersStrong XML/Web Service supportCode generation technology protects investmentProtecting against backend product upgrades

ConcernsNew vocabulary (learning curve)Complex application, process intensive

Page 13: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200713

There are so many choices?

Portlet API

JSF Framework

Struts Portlet Framework

Struts 2.0

My Servlet Framework

There may be many valid and not so valid reasons to consider.

Portlet Factory

Spring 2.0

Apache Bridge

Page 14: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200714

Apache Struts 2.0:

Sunil Patil, Senior Consultant, Ascendant Technology LLC

Email: [email protected]

Page 15: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200715

Struts 2.0 Introduction

Action Framework, allows you to map URL to Action Class

Missing features of Apache Struts 1.0 will be ported to Struts 2.0

OpenSymphony’s WebWorks framework will cease to existsWebWorks will only release patches for existing versions.

The WebWorks framework has built-in support for JSR-168 portlet development

Architecture based on OpenSymphony’s WebWorks FrameworkCommand pattern implementation• Actions are command objects

Page 16: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200716

Client Selection Example

At previous client using Apache Struts 2.0 framework

Reasons Struts 2.0 framework is consideredCustomer wanted precise control on the Portlet ScreensCustomer wanted to use common look and feel across all portletsHas considered JSF, but developing custom components in JSF requires lot of workDevelopers already had experience on Apache Struts 1.0, easy migrationProvides clear separation between Action and Render phase

Page 17: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200717

Key Struts 2.0 objects/concepts

Jsr168Dispatcher:The Controller portlet that handles all requests to the portlet application. Similar to ActionServlet in Struts 1.0 framework

ActionJsr168Dispatcher passed request to configured Action to handle request.Combines ActionForm, Action and Model classLocation in framework where you write you business logic.

ResultsDifferent view technologies are supported• JSP• Velocity• Freemarker

Page 18: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200718

Key Struts 2.0 objects/concepts Cont.

InterceptorInterceptors are one of the most powerful features of Struts 2.0 frameworkInterceptors are similar to Filters in servlet environment, they allow you to execute your code before and after Action.Common functionality is implemented as interceptor• Setting parameters• Validation workflow

Value StackUsed for carrying data from action class to JSP pageValue Stack, Stack of objects• Temporary Objects• Action Object• Named object

Page 19: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200719

Struts : Forward to JSP request flow

Page 20: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200720

Struts : Forward to Action request flow

Page 21: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200721

Main Artifacts

Page 22: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200722

Additions to portlet.xml

Jsr168Dispatcher is portlet dispatcher class

viewNameSpace defines default namespace for view mode

defaultViewAction defines default action for view mode

Page 23: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200723

Additions to web.xml

Configure to setup PreparatorServlet

Configure to setup spring integration

Page 24: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200724

Struts Configuration

Import default configuration from struts-portlet-default.xml

Divide configuration in different namespaces

Page 25: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200725

Struts Action

Optionally extend from ActionSupport class

Combines Action, ActionForm and Model class

The execute() method does not take PortletRequest and PortletResponse object

Page 26: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200726

Struts Action contd.

Setting form values in Contact class

No need for contactList, model class

Page 27: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200727

Struts JSP

OGNL expressions for reading values from Value stack

Read i18n messages from <ActionClass>.properties file

Struts tags use Freemarker Template for generating markup

You can extend tag by overriding template

XHTML Theme - No HTML Markup in the JSP

Page 28: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200728

Struts Tags

The form field tag is responsible forGenerating two column layoutGenerating form elements as well as surrounding table element

Every form field tag is responsible for generating one row in tableDisplay label on the left side of the form fieldDisplay actual form field on the right sideDisplay the field error above the form field

<s:textfield label="Name" name="contact.firstName"/>

rendered as (with error message)

Page 29: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200729

Struts Tags contd.

This is how datepicker form field and popup look like

<s:datetimepicker label="DOB" name="contact.dateOfBirth"/>

rendered as

Page 30: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200730

Support for Different View Technologies

Same tag library available in JSP, Freemarker and Velocity

Insert.ftl

insert.vm

Page 31: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200731

Form Validation in Struts 2.0

Can be use several different typesProgrammatically by implementing validate() method in Action classDeclaratively using XWork’s Validation frameworkClient side validation

Create <ActionClass>-validation.xml

Sample uses of XWork validation

Page 32: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200732

Type Conversion

Routine type conversion in the framework is transparentBoolean, boolean, Integrer, int, Float, float, Long, log, Double, doubleDate with the locale associated with current request

Register custom type conversion class<ActionClass>-conversion.properties for class specific conversion• Point=com.ascendant.converter.PointConverterXwork-conversion-properties for global conversion

Custom type conversion for more complex objectsCreate class extending StrutsTypeConverter class• convertFromString()• convertToString()

Page 33: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200733

Struts Plug-ins

Many popular optional features of the framework are distributed as plug-insSpringTilesFile upload

Plug-ins are used forIntroducing new Extension point implementation class• ObjectFactory : Spring plug-in implements StrutsSpringObjectFactory• Mutlipart parser: Allows using Pell’s multi part parser for parsing multi-part requestDefine new packages with results, interceptors or actions• Results: Tiles plug-in introduces Tiles Results typeOverride framework constants

Page 34: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200734

Spring Integration

Allows Actions, Interceptors and Results to be created by Spring

Struts created object can be auto wired by spring after creation

Provides two interceptors that auto wire actions if not using the Spring Objectfactory

Add entry for StrutsSpringObjectFactory in struts.propertiesstruts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory

Page 35: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200735

Spring 2.0:

Sean Newby, Senior Consultant, Perficient Inc.

Email: [email protected]

Page 36: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200736

Spring Intro

Spring is a popular Java Application Framework that has gained significant momentum in the past few years.

Made up of several modules that can be used independentlyInversion of Control Container (Core)AOP module DAO moduleMVC module

Portlet MVC became a part of the general Spring 2.0 release in November of 2006.

Page 37: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200737

Client Selection Example

At a previous client that decided to Spring MVC.

Reasons Spring MVC was selected.Similar in design to IBM Struts implementation. Had considered JSF, but no developers had any experience. JSF seemed to be more of a paradigm shift.Developers already had experience with other elements of Spring.Confident that would be able to find answers to issues in the Spring community.Addressed use case we needed to solve (Wizard Portlet).Designed around portlet api, addressed Render and Action separately.

Page 38: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200738

Key Portlet MVC objects / concepts

DispatcherPortletThe controller portlet that handles all requests to the portlet application. Similar to ActionPortlet or ActionServlet in the Struts framework.

ControllerLocation in Framework where your code begins. Similar to Struts Action class.DispatcherPortlet passed request to configured Controller to handle request. Separate methods for render and action phases.Several default implementations to handle common use cases:• ParameterizableViewController – Simple controller that passes to a view.• SimpleFormController – Controller to handle form submissions.• AbstractWizardController – Controller to implement wizard like form.

Form that extends over several pages.

HandlerMappingDetermines which Controller will be called.Several out of the box implementations.

Page 39: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200739

Key Portlet MVC objects / concepts cont.

ModelAndViewObject that is returned by Controllers to DispatcherPortlet.Contains the Model that needs to be displayed by the view. Also contains the “name” of the view.Behavior is similar to a regular Map object.

ViewResolverMaintains a clean separation of View technology.Several Default ViewResolvers• JSP• Velocity• XSLT

Command ObjectsJavaBean type objects. Object is bound to request parameters, usually from form submission. Similar to Struts ActionForm, except is does not extend framework specific class.

Page 40: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200740

Simplified Render Sequence Diagram

Some steps have been omitted for clarity.

Page 41: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200741

Simplified Action Sequence Diagram

Some steps have been omitted for clarity.

Page 42: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200742

Getting Started setting up a Spring Portlet

WEB-INF/web.xml and WEB-INF/portlet.xml

WEB-INF/context/applicationContext.xml – main application wide config file. Values can be overridden in portlet config.

WEB-INF/<PORTLETNAME>-portlet.xml – individual config file for each portlet.

WEB-INF/lib/spring.jar – main spring file.*

WEB-INF/lib/spring-portlet.jar – additions for Spring Portlet MVC

WEB-INF/lib/spring-modules-validation.jar – additions for validation of forms.

*could pick out specific Spring jar’s to reduce size of application.

Page 43: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200743

Main Artifacts

Page 44: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200744

Additions to Web.xml

Configuration to set up Spring MVC servlet.

Portlet MVC leverages Servlet base code.

Page 45: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200745

Portlet.xml

portlet-class is Portlet MVC class

portlet-name = <PortletName>-portlet.xml Spring MVC config file.

Page 46: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200746

Spring IoC Core (Dependency Injection)

Don’t really need to understand to use Portlet MVC, but it is helpful.

From Spring Documentation: The basic principle behind Dependency Injection (DI) is that objects define their dependencies (that is to say the other objects they work with) only through constructor arguments, arguments to a factory method, or properties which are set on the object instance after it has been constructed or returned from a factory method. Then, it is the job of the container to actually inject those dependencies when it creates the bean. This is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself being in control of instantiating or locating its dependencies on its own using direct construction of classes, or something like the Service Locator pattern. It becomes evident upon usage that code gets much cleaner when the DI principle is applied, and reaching a higher grade of decoupling is much easier when beans do not look up their dependencies, but are provided with them (and additionally do not even know where the dependencies are located and of what actual class they are).

Really enables “Programming to the interface”

Page 47: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200747

Spring config basics

Everything is defined as a bean.

Container will call methods on defined beans.Passes in a StringPasses in a reference to another bean

Page 48: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200748

Spring Config Basics - inject another bean

Creates two beanslocationDao = new LocationDaoHardCodedImpl()locationService = new LocationServiceImpl()

locationService has a locationDao “injected” into it.“name” is method to call (setLocationDao)“ref” indicates pass in reference to another defined bean

Page 49: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200749

Spring Config Basics – inject a String

Creates a bean referred to as “displayIndexController”“value” indicates pass in StringInjects a String in the first propertyInjects a bean in the second property

Page 50: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200750

Portlet MVC application.xml entries - View

Defines where named “views” are located.

Defines what view technology to use.

Sample uses jsp’s located in /WEB-INF/jsp/ directory

Page 51: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200751

Portlet MVC application.xml entries - Messages

Defines message bundle to use for i18n.

Page 52: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200752

Portlet MVC application.xml entries - Exception

Define default views for exceptions.

PortletSecurityException goes to notAuthorized view.

UnavailableException goes to notAvailable view.

Page 53: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200753

Sample Portlets

Spring DisplayExample of a ParameterizableViewControllerDisplays dynamic data

Spring FormExample of a SimpleFormControllerHandles a form submission

Spring WizardExample of an AbstractWizardFormControllerPresents a form over several pagesAssists with navigating back and forth

Page 54: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200754

SpringDisplay-portlet.xml – ParameterDispatchController

Declares controller used, referred to as displayIndexController.

Property viewName corresponds to jsp that will be displayed.

Page 55: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200755

PortletModeParameterHandlerMapping

Allows you to call other controller from the view.

Page 56: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200756

DisplayIndexController

ParameterizedViewController implementation

Page 57: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200757

SpringForm-portlet.xml - SimpleFormController

formView – view to display form initially

successView – confirmation page once form submitted

commandClass – form backing object, like ActionForm

commandName – how to refer to commandClass in view

validator – validation to use

Page 58: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200758

FormIndexController

SimpleFormController implementation.

Page 59: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200759

formIndex.jsp

Uses Spring 2.0 style form tags

commandName is form backing object

errors path=“*”, show validation errors for all fields

path=“name”, binds to name property of location object

Page 60: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200760

Form Validation in Spring MVC

Can use several different typesProgrammatically through Validator interfaceDeclaratively using Apache Commons Validation FrameworkDeclaratively using Valang

Sample uses Valang validation

Page 61: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200761

SpringWizard-portlet.xml

pages – List of view that are available through the wizard.Order is significant.wizardBasicInfo is page 0.wizardContactInfo is page 1.wizardBillingInfo is page 2.

Page 62: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200762

WizardIndexController

Page 63: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200763

wizardContactInfo.jsp

Middle step in wizard

JSP wizard navigation code, _target[PAGEINDEX]

Uses older Spring 1.0 form tags

_page1, pageAttribute from configuration file

Page 64: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200764

Spring MVC Pros / Cons

ProsEasy for experienced Struts Developers to transition.Strong community, should be viable solution for some time.Modular, pick and choose features. Entry point to use more of the Spring framework.Design goal of the framework is to be non-evasive, your code does not depend on the framework.• Class extension or Interface implementation.

Good documentation for open source project.

ConsCan be xml config intensive.Can not call IBM and create a PMR for support.

Page 65: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200765

Additional Information and Resources

Websphere Portal Business Solutions Catalog:

http://catalog.lotus.com/wps/portal/portal

Webspere Portal Product Information:http://www-306.ibm.com/software/genservers/portal

WebSphere Portal Information Center Documentation:http://www.ibm.com/developerworks/websphere/zones/portal/proddoc.html

Page 66: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200766

Summary

IBM Perspective

Struts 2.0

Spring 2.0

Page 67: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200767

Avoid Boring People: Lessons from a Life in Science (Hardcover)by James D. Watson (Author)

Page 68: D22 Portlet Development With Open Source Frameworks

WebSphere Portal Technical Conference U.S. 200768

Programming Portlets: From JSR 168 to IBM WebSphere Portal Extensions

Ron Lynn, Joey Bernal, Peter Blinstrubas, Stefan Hepper, Usman Memon, Varadarajan (Varad) Ramamoorthy