D22 portlet development with open source frameworks


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Mention that planned availability for Lotus Quickr is late 2Q. Therefore, some of the details in this presentation might change before then.
  • The typical portlet starts off small and later is enhanced. The business logic is distributed throughout the portlet. The business logic then changes over time causing a rewrite of the portlet. Model View Controller design pattern has been around a long time. This design pattern advocates separating the view aspect from the model and using a controller to orchestrate the flow. Portlet development adds some unique features over the servlet world. Portlet supports modes Portlet must be able to redisplay a view. ( if a user is interacting with other portlets, each portlet on the page must save the state so the view can be redisplayed. ) Options Write the portlet in portal api, but be careful to design the application correctly Develop a custom framework for portlet development. Some customers have done this in the servlet world and have ported it to the portlet world Use an existing framework!
  • There are numerous features of Portlet Factory specifically intended to ensure the unsurpassed competitive differentiation of WebSphere Portal, as well as contributing to increased customer success and satisfaction. With its tight WebSphere Portal integration and automation via Builders it dramatically simplifies the development of complex portlets – including multi-page transactional composite portlets via integrations to existing applications and systems like SAP and Domino, among others. In addition, Portlet Factory’s patented Profiling technology generates multiple portlet variations from one code base. This feature enables developers to quickly vary portlets across any number of multiple user dimensions (roles, geography, brands) and greatly simplifies code maintenance, by automatically propagating changes. Finally, developers can target multiple computing environments (portal, web, rich-client, phone, PDA) from a single application, which means developers can repurpose applications with minimal development effort. It is also important to understand that Portlet Factory extensible and not limited to the capabilities provided out of the box. For example, J2EE developers can capture an organization’s proprietary APIs as a custom function that they can then add to the tool’s palette so other developers may use it. In doing this, they can ensure all developers are accessing and using the API the same way, enforcing consistent design patterns and architecture.
  • Mention that planned availability for Lotus Quickr is late 2Q. Therefore, some of the details in this presentation might change before then.
  • The Jsr168Dispatcher is a controller portlet that is responsible for handling all the client request. You can think about it as something similar to ActionServlet in Struts 1.0 framework. Actions in struts framework are combination of Actions, ActionForm and Model classes. What i meant by that is, Lets say if you want to handle simple form submission, then in Struts 1.0 framework you will have to create 3 different classes. First is ActionForm class which is simple Java Bean with properties matching to your html form. Second is Action class which is a place where you write your business logic and third is Model class that is used for carrying data from Action class to JSP. In case of struts framework your Action class is combination of all three classes. First it will have java bean properties matching your html form, then it will have execute() method where you write your business logic and same Action class object is available to JSP page at the time of markup generation so it also plays the role of Model class. The basic concept of Results is same as that of Struts 1.0 framework, which is that execute() method of your Action class will return result code and based on that framework will dispatch control to JSP for final markup generation. Only difference is that Struts framework wraps request dispatching functionality in separate class in order to maintain clear separation of view layer
  • Interceptors is this basic concept that allows you to execute some code before or after execute method of your action class. In fact most of the struts framework logic such as setting action properties with values submitted by user or exception handling is implemented in interceptors. The Struts framework also allows you to group set of interceptors together into stack and define the order in which they are executed. Every application will have a default interceptor stack and then your allowed to override that value at individual action level. For example if your application does not need ability to handle file upload then you can remove that interceptor from your applications default interceptor stack. Value stack as name suggests is simply stack of object that is used for carrying data from your action class to JSP page. When using the value stack there is no need to keep track of which scope the target object is in. If you want the attribute “firstname”, then you query the value stack for this attribute. Each stack element, in the provided order, is asked whether it has the property. If it does, then the value is returned and we are done. If not, then the next element down is queried. This continues until the end of the stack is reached. This is a great feature, as you don’t care where the value is – the action, the model, or the request object – you just know that if the value exists it will be returned.
  • Take a look at this sequence diagram that describes sequence of events that happen when Dispatcher portlet gets request. First thing that dispatcher portlet does is that it finds out action class that should be used to handle this request along with interceptor stack that should be applied to the action. Once the interceptor stack is decided, it starts calling all the interceptors in the stack one by one followed by call to execute method of your action class. Inside the execute method you will execute your business logic, populate model class and based on the outcome of business logic return a result code. Struts framework will use return code of your execute method to find out the location of JSP that should be used to generate final markup Before ending action phase struts framework will store both your action object as well as final location of the JSP in the portlet session. After that whenever dispatcher portlet gets render request instead of re executing action class, it will read location of JSP from the session and forward control to it for markup generation.
  • But the question is, What if your developing say Stock Ticker portlet and want to query stock service every time for latest update. If that's the case then first you should create a separate action class and add render phase logic in it. Then you should change your struts.xml file so that success code for first action maps to action URL of the second action. Once you do that your action phase request processing will remain same. But in the render phase Struts framework will forward control to your render action, which can execute render logic and then forward control to JSP page for final markup generation. I will show you an example of how to do this on the later slide.
  • Lets walk through steps that are required to build Struts portlet. You can download sample code for this session from Conference web site. In order to build struts portlet, you will have to create 3 set of files First is I18n and validation related resources. The actual struts configuration is stored in couple of files first is struts.properties and second is struts.xml And since we want to use spring integration, we will have to create applicationContext.xml file. Now lets start taking closer look at each of these files
  • The Jsr168Dispatcher is main dispatcher portlet so you should set it as portlet class. Value of viewNameSpace initialization parameter defines default namespace for the view mode Value of defaultViewAction defines, default action for view mode. If you want to support any other portlet mode then you need to set parameters for that.
  • The web.xml file will have two section, in the spring setup configuration first set contextConfigLocation parameter pointing to location of spring context file and then define ContextLoaderListerner which will be responsible for loading your spring configuration. The Struts portlet community wanted to use same tag library as that of servlet environment but problem was that struts tag library has dependency on ServletContext, httpServletrequest and HttpServletResponse object. But these objects are not available in Portlet environment. In order to solve this problem they introduced PreparatorServlet. The way it works is before forwarding control to actual JSP for markup generation, Struts framework will forward control to PreparatorServlet. This servlet will store object of HttpServletRequest, Response and ServletContext in thread local variable. Since the JSP page is called in the same thread as that of the the preparatorServlet, custom tag library will get access to these objects.
  • This is how your struts configuration file would look like The Struts framework allows you to break your configuration in multiple namespaces to facilitate parallel development. It uses combination of packagename and actionname to uniquely identify action class that will be used to handle particular request. For example in our sample code selectAction class will be used only for handling selectAction in the view mode. If you have more than one developers working on one portlet, you can break your configuration into multiple files and then import those files into your struts.xml. In fact most of the struts framework configuration such as definitions for built-in interceptors and result classes is defined in struts-portlet-default.xml. You should always import struts-portlet-default.xml in your configuration file and extend your packages from struts-portlet-default to take advantage of built in functionality. Take a look at Forward Control to JSP block, in this we are saying that whenever it gets request for deleteAction it should forward control deleteAction class. Since we are using Spring integration actual action class name is defined in spring configuration file. Then we are saying that if delete action returns success framework should forward control to list.jsp. Success is default value for result code so you can omit it if you want. The forward control to action block is example of how you can forward control to some other action class for handling render phase. The interceptor-ref element is example of how you can apply particular interceptor stack to your action class.
  • In our struts.xml file we have mapped deleteAction url to DelteAction class. So the way it works is when you go to contact spread sheet, select a contact and say delete then struts framework will create new object of DeleteAction, set its contactId property and call its execute() method. In the execute method first we delete that contact then query database to get updated contactList and store it in contactList variable that list.jsp will use to display contact spreadsheet. Did you notice that the execute() method does not get object of either PortletRequest or PortletResponse, this is because one of the design goals of Struts frameworks was to make unit testing easy. In the unit test case of DeleteAction, you can create its object set contactId and call execute() method, and there is no need to mock portletrequest, response objects. The way portlet containers work is that they use separate thread for handling every request. The Struts framework tries to take advantage of this fact by storing all the objects related to the request such as portlet request, response or session in ThreadLocal object and the PortletActionContext class provides static methods to access these object anywhere you want in current request. But as we know static methods make unit testing difficult so Struts framework defines one more way to access portlet specific object. Lets say your interested in PortletPreferences object, if that's the case then first you should implement PortletPreferencesAware interface in your action and then define portletPreferences as bean property in your action class along with setter method. If you do that then at runtime framework will inject portletpreferences object in your action class.
  • This is another example of action class. Now if your HTML form has more than 4-5 parameters then instead of adding each of these parameter as property in your action class you may want to create separate JavaBeans and add those input parameters as properties into it. In our sample code contact form has 5 fields so we had created contact bean with 5 properties. You will have to change your JSP page to use contact.propertyname expression for getting or setting properties from contact bean, Also we want to delegate control to SelectAction for actual markup generation. So we don’t need model class related functionality in our Action class. Only thing that our execute method does is insert contact then return SUCCESS return code. The Struts framework will forward control to SelectAction for handling render phase and it will take care of populating contactList variable before forwarding control to list.jsp for actual markup generation.
  • This is how typical JSP page looks like. The Struts JSPs use OGNL as expression language for getting and setting properties from beans. OGNL does everything that JSP expression language does and in addition to that it has some advanced features such as projections, filters Projection is this cool feature that allows you to set project particular property of bean into temporary collection. For example, Lets say you have this list of contact objects and you want to display first names of all the contacts in combo box, in normal case you will have to write some JSP scriplet for this. But In OGNL you can use projections to project firstName property of every contact objects into another collection and then use that collection for displaying firstName combo. Developers of the struts tag library had couple of design goals. First they wanted to provide consistent look and feel through out the application and second they wanted to provide extendible tags. So the way struts tag library works is every custom tag has one Java file and one template file associated with it. For example when you use texfield tag first control will go to TextField.java which will perform some basic logic such as reading value of the java bean property from value stack and then it will forward control to TextField.ftl which is freemarker template file for actual markup generation of input tag. In addition to this Struts tag library has concept of theme which allows you to group set of template files that provide similar look and feel. Did you notice that our JSP page does not use any HTML markup elements such as <table>,<tr> or td that is because we are using XHTML theme and it takes care of displaying forms in two column layout.
  • The XHTML implementation of The form tag is responsible for creating the form element as well as the surrounding table. The rest of the form element tags are responsible for creating one row each in the table. In which label is displayed on the left and actual form element is displayed on the right. In addition to being responsible for creating a single row, each tag is also responsible for displaying any validation or type conversion errors that may be associated with that field. If you don't want Struts tag library to create two column layout for you, you can change your theme to simple and take complete control on markup.
  • In addition to simple form tags struts tag library also provides few out of box widgets such as date picker. In our sample code we are using datetimepicker tag for date of birth input The datetimepicker tag generates 3 things first a label on the left hand side, after that input box, followed by button. When you click on the button it will open a calendar control like this that you can use for selecting date.
  • In addition to powerful tag library struts framework also has support for different view technologies such as Freemarker and Velocity. Take a look at how you can create insert.jsp page using either Velocity or Freemarker templating language. Good thing about struts is that it provides native access to struts tags from both Freemarker and Velocity.
  • Ability to validate user input is another common requirements and Struts framework provides quite few choices for how you want to implement your validation rules.. If you decide to use programmatic validation then you can implement validate() method in your Action class and add your validation rules there. But if you decide to use declarative validation then you will have to create ActionClassName-validation.xml file in the same package as that of your action class. For example take a look at how validation.xml file for our Insert Action As you can see the concept is very much similar to Apache Commons validation framework but it is bit more flexible. If you decide to implement your custom validation rules then you can create class implementing Validation interface and register it with validation framework.
  • Other common problem in web applications is that web is type agnostic i.e. everything is string but java is strictly typed language. So you need some mechanism to convert string data into specific type and other way round. In Struts routine type conversion such as int, double, date is transparent. For example, in our sample code date of birth field is date of type. So only thing that you should do is declare it as date type while creating contact java bean and struts framework will take care of converting string to date when setting form values and converting from date to string when displaying data to user. Another good thing about type conversion is that struts framework also takes care of locale specific formatting of date type.
  • Struts framework provides simple plug-in architecture so that developers can extend the framework by just adding a simple JAR to web applications class path. In fact most of the optional features such as Spring and Tiles integration are implemented as plug-in. If you remember even struts 1.0 has concept of plug-in, that allowed you to execute some code at application startup and shutdown. But in Struts 2.0 plug-ins are much more powerful. Struts framework defines 12 different extension points that you can extend in your plug-in. For example Spring plug-in provides its own object factory that allows you to use Spring framework for actual object creation
  • In our sample code we are using Spring framework as object factory. What that means is whenever Struts framework wants to create new object for either Action or Result class it will ask Spring framework for that. You can configure this by adding entry for struts.objectFactory in struts.properties file. For example when Struts framework gets selectAction request it will ask Spring framework for object of SelectContac class. Now we have defined selectContact as bean in spring configuration file and it has dependency on contactDAO. So Spring framework will first create object of selectAction class inject contactDAO object in it and then return that object to struts for further processing. In our sample code we talked about how you can use Spring framework for integration between View and data access layer. But Spring framework also has its own implementation of Portlet MVC framework, my friend Sean is going to talk about it in detail.
  • Mention that planned availability for Lotus Quickr is late 2Q. Therefore, some of the details in this presentation might change before then.
  • D22 portlet development with open source frameworks

    1. 1. Session Number: D22 Jump START your Portlet development with Open Source Frameworks! Sean Newby, Perficient Sunil Patil, Ascendant Technology LLC Peter Blinstrubas, IBM
    2. 2. Agenda <ul><li>IBM Portlet Development Perspective </li></ul><ul><li>Struts 2.0 </li></ul><ul><li>Spring 2.0 </li></ul>
    3. 3. <ul><li> IBM Portlet Development Perspective </li></ul><ul><li>Email: peteblin@us.ibm.com </li></ul>
    4. 4. Why Use a Framework for Portlet Development? <ul><li>Starts off small and later is enhanced. </li></ul><ul><li>Model View Controller well established. </li></ul><ul><ul><li>Provides separation between different layers </li></ul></ul><ul><li>Frameworks force developers into using a standard approach when building applications </li></ul><ul><li>Shield some of the underlying complexity of developing an application. </li></ul><ul><li>Provides interfaces or abstract classes to speed and structure your development effort. </li></ul><ul><li>Options </li></ul><ul><ul><li>Write the portlet in portal api </li></ul></ul><ul><ul><li>Develop a custom framework for portlet development. </li></ul></ul><ul><ul><ul><li>Port servlet world framework to the portlet world </li></ul></ul></ul><ul><ul><li>Use an existing framework! </li></ul></ul>
    5. 5. Frameworks for Portlet Development <ul><li>IBM Frameworks </li></ul><ul><ul><li>Just the Portlet API </li></ul></ul><ul><ul><li>WebSphere Portlet Factory </li></ul></ul><ul><ul><ul><li>AJAX based on DOJO </li></ul></ul></ul><ul><ul><li>JSF V1.1 on Portal 6.x with JWL 3.0 </li></ul></ul><ul><ul><li>JSF 1.2 for Portal running on WAS 7 (plans subject to change) </li></ul></ul><ul><ul><ul><li>AJAX </li></ul></ul></ul><ul><ul><li>Struts Apache Bridge for JSR 168 Container on WAS 6.1 </li></ul></ul><ul><ul><li>Currently Struts Portlet Framework supports Struts 1.1 and 1.29 </li></ul></ul><ul><li>Alternative Frameworks </li></ul><ul><ul><li>Custom MVC </li></ul></ul><ul><ul><li>Spring 2.0 </li></ul></ul><ul><ul><li>Struts 2.0 </li></ul></ul><ul><ul><li>Many More… (i.e. Shale, Wicket,…) </li></ul></ul>
    6. 6. What is the Struts Portlet Framework? <ul><li>Runtime </li></ul><ul><ul><li>A package that supports Struts application in Portal. </li></ul></ul><ul><ul><li>Includes a level of Struts (1.1 and 1.29) </li></ul></ul><ul><ul><li>A portlet controller and a request processor. </li></ul></ul><ul><ul><li>Implementations of some of the Struts tags </li></ul></ul><ul><li>Tool Set </li></ul><ul><ul><li>Flow Editor for creating and wiring pages and actions together </li></ul></ul><ul><ul><li>A full Struts-Config editor </li></ul></ul><ul><ul><li>Link validation and re-factoring for Struts specific links </li></ul></ul><ul><ul><li>Wizards for creating Actions, Struts JSPs and Form Beans </li></ul></ul><ul><ul><li>Page Designer Visualizations for some Struts-html tags </li></ul></ul><ul><ul><li>Full Page Designer Palette for Struts Tags </li></ul></ul>
    7. 7. What is the Struts Portlet Framework? <ul><li>Support for writing/migrating Struts applications that can be deployed in WebSphere Portal </li></ul><ul><li>Also provides samples to show how portal features can be incorporated by Struts applications </li></ul>
    8. 8. Struts 1.x in General <ul><li>Struts is: </li></ul><ul><ul><li>Controller based framework </li></ul></ul><ul><li>Struts does not have: </li></ul><ul><ul><li>A built-in UI component model </li></ul></ul><ul><ul><li>A built-in event model for UI components </li></ul></ul><ul><ul><li>A built-in state management for UI components </li></ul></ul><ul><ul><li>A built-in support of multiple renderers (Struts is </li></ul></ul><ul><ul><ul><li>more or less tied up with HTML) </li></ul></ul></ul><ul><li>Struts is Not a standard (despite its popularity) </li></ul><ul><li>IBM has no plans to continue SPF Development. </li></ul><ul><li>Recommended for Legacy Struts Applications. </li></ul><ul><ul><li>Also consider Apache Bridge </li></ul></ul>
    9. 9. IBM and JSF <ul><li>Part of Java EE 5 </li></ul><ul><ul><li>Participation in JSR Expert Group </li></ul></ul><ul><ul><li>J2EE-based Web UI and application framework </li></ul></ul><ul><li>WPLC </li></ul><ul><ul><ul><li>Underlying technology in Composite Application Designer </li></ul></ul></ul><ul><ul><ul><li>Used in Portal Workflow portlets </li></ul></ul></ul><ul><ul><ul><li>Being incorporated into Lotus Expeditor </li></ul></ul></ul><ul><ul><ul><li>Widgets in Portlet Factory </li></ul></ul></ul><ul><li>Recommended for new portlet development </li></ul><ul><li>Note: Refer to session D12 for JSF details </li></ul>
    10. 10. JSF General <ul><li>Benefits </li></ul><ul><ul><li>Reusable UI Components </li></ul></ul><ul><ul><li>Custom Tag Libraries </li></ul></ul><ul><ul><li>API for managing UI components, events, input navigation, page navigation </li></ul></ul><ul><ul><li>Addresses complex navigation requirements </li></ul></ul><ul><ul><li>Extensible - Everything is pluggable </li></ul></ul><ul><ul><li>Rich tooling support </li></ul></ul><ul><ul><li>Flexible Actions: different navigations for multiple components on a single page </li></ul></ul><ul><ul><li>Supports Expression </li></ul></ul><ul><ul><li>Built in state management </li></ul></ul><ul><li>Concerns </li></ul><ul><ul><li>Complexity </li></ul></ul><ul><ul><li>Still relatively new </li></ul></ul><ul><ul><li>Out of the box capability to meet requirements </li></ul></ul><ul><ul><li>Availability of skills </li></ul></ul>
    11. 11. <ul><li>Plug-in to Eclipse & Rational Application Developer </li></ul><ul><li>Multi-page custom portlets/applications without coding </li></ul><ul><li>Integration capabilities (SAP, Domino, PeopleSoft, Siebel, Web Services, Databases, Portal Content Repository) </li></ul><ul><li>Service-oriented development </li></ul><ul><li>Support for Portal features such as portlet communication, business user configuration, auto-deploy, single sign-on </li></ul><ul><li>“ Dynamic profiling” capability, to create multiple variations from a single set of source portlets </li></ul>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.
    12. 12. Portlet Factory General <ul><li>Benefits </li></ul><ul><ul><li>DataCentric Applications </li></ul></ul><ul><ul><li>Expand Portlet Creation to developers of all skill level </li></ul></ul><ul><ul><li>Captures Design Patterns/Standardize Development Model </li></ul></ul><ul><ul><li>Lots of adapters </li></ul></ul><ul><ul><li>Strong XML/Web Service support </li></ul></ul><ul><ul><li>Code generation technology protects investment </li></ul></ul><ul><ul><li>Protecting against backend product upgrades </li></ul></ul><ul><li>Concerns </li></ul><ul><ul><li>New vocabulary (learning curve) </li></ul></ul><ul><ul><li>Complex application, process intensive </li></ul></ul>
    13. 13. 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
    14. 14. <ul><li> Apache Struts 2.0: </li></ul><ul><li>Sunil Patil, Senior Consultant, Ascendant Technology LLC </li></ul><ul><li>Email: spatil@atech.com </li></ul>
    15. 15. Struts 2.0 Introduction <ul><li>Action Framework, allows you to map URL to Action Class </li></ul><ul><li>Missing features of Apache Struts 1.0 will be ported to Struts 2.0 </li></ul><ul><li>OpenSymphony’s WebWorks framework will cease to exists </li></ul><ul><ul><li>WebWorks will only release patches for existing versions. </li></ul></ul><ul><li>The WebWorks framework has built-in support for JSR-168 portlet development </li></ul><ul><li>Architecture based on OpenSymphony’s WebWorks Framework </li></ul><ul><ul><li>Command pattern implementation </li></ul></ul><ul><ul><ul><li>Actions are command objects </li></ul></ul></ul>
    16. 16. Client Selection Example <ul><li>At previous client using Apache Struts 2.0 framework </li></ul><ul><li>Reasons Struts 2.0 framework is considered </li></ul><ul><ul><li>Customer wanted precise control on the Portlet Screens </li></ul></ul><ul><ul><li>Customer wanted to use common look and feel across all portlets </li></ul></ul><ul><ul><li>Has considered JSF, but developing custom components in JSF requires lot of work </li></ul></ul><ul><ul><li>Developers already had experience on Apache Struts 1.0, easy migration </li></ul></ul><ul><ul><li>Provides clear separation between Action and Render phase </li></ul></ul>
    17. 17. Key Struts 2.0 objects/concepts <ul><li>Jsr168Dispatcher: </li></ul><ul><ul><li>The Controller portlet that handles all requests to the portlet application. Similar to ActionServlet in Struts 1.0 framework </li></ul></ul><ul><li>Action </li></ul><ul><ul><li>Jsr168Dispatcher passed request to configured Action to handle request. </li></ul></ul><ul><ul><li>Combines ActionForm, Action and Model class </li></ul></ul><ul><ul><li>Location in framework where you write you business logic. </li></ul></ul><ul><li>Results </li></ul><ul><ul><li>Different view technologies are supported </li></ul></ul><ul><ul><ul><li>JSP </li></ul></ul></ul><ul><ul><ul><li>Velocity </li></ul></ul></ul><ul><ul><ul><li>Freemarker </li></ul></ul></ul>
    18. 18. Key Struts 2.0 objects/concepts Cont. <ul><li>Interceptor </li></ul><ul><ul><li>Interceptors are one of the most powerful features of Struts 2.0 framework </li></ul></ul><ul><ul><li>Interceptors are similar to Filters in servlet environment, they allow you to execute your code before and after Action. </li></ul></ul><ul><ul><li>Common functionality is implemented as interceptor </li></ul></ul><ul><ul><ul><li>Setting parameters </li></ul></ul></ul><ul><ul><ul><li>Validation workflow </li></ul></ul></ul><ul><li>Value Stack </li></ul><ul><ul><li>Used for carrying data from action class to JSP page </li></ul></ul><ul><ul><li>Value Stack, Stack of objects </li></ul></ul><ul><ul><ul><li>Temporary Objects </li></ul></ul></ul><ul><ul><ul><li>Action Object </li></ul></ul></ul><ul><ul><ul><li>Named object </li></ul></ul></ul>
    19. 19. Struts : Forward to JSP request flow
    20. 20. Struts : Forward to Action request flow
    21. 21. Main Artifacts
    22. 22. Additions to portlet.xml <ul><li>Jsr168Dispatcher is portlet dispatcher class </li></ul><ul><li>viewNameSpace defines default namespace for view mode </li></ul><ul><li>defaultViewAction defines default action for view mode </li></ul>
    23. 23. Additions to web.xml <ul><li>Configure to setup PreparatorServlet </li></ul><ul><li>Configure to setup spring integration </li></ul>
    24. 24. Struts Configuration <ul><li>Import default configuration from struts-portlet-default.xml </li></ul><ul><li>Divide configuration in different namespaces </li></ul>
    25. 25. Struts Action <ul><li>Optionally extend from ActionSupport class </li></ul><ul><li>Combines Action, ActionForm and Model class </li></ul><ul><li>The execute() method does not take PortletRequest and PortletResponse object </li></ul>
    26. 26. Struts Action contd. <ul><li>Setting form values in Contact class </li></ul><ul><li>No need for contactList, model class </li></ul>
    27. 27. Struts JSP <ul><li>OGNL expressions for reading values from Value stack </li></ul><ul><li>Read i18n messages from <ActionClass>.properties file </li></ul><ul><li>Struts tags use Freemarker Template for generating markup </li></ul><ul><li>You can extend tag by overriding template </li></ul><ul><li>XHTML Theme - No HTML Markup in the JSP </li></ul>
    28. 28. Struts Tags <ul><li>The form field tag is responsible for </li></ul><ul><ul><li>Generating two column layout </li></ul></ul><ul><ul><li>Generating form elements as well as surrounding table element </li></ul></ul><ul><li>Every form field tag is responsible for generating one row in table </li></ul><ul><ul><li>Display label on the left side of the form field </li></ul></ul><ul><ul><li>Display actual form field on the right side </li></ul></ul><ul><ul><li>Display the field error above the form field </li></ul></ul><ul><li><s:textfield label=&quot;Name&quot; name=&quot;contact.firstName&quot;/> </li></ul><ul><li> rendered as (with error message) </li></ul>
    29. 29. Struts Tags contd. <ul><li>This is how datepicker form field and popup look like </li></ul><ul><li><s:datetimepicker label=&quot;DOB&quot; name=&quot;contact.dateOfBirth&quot;/> </li></ul><ul><li>rendered as </li></ul>
    30. 30. Support for Different View Technologies <ul><li>Same tag library available in JSP, Freemarker and Velocity </li></ul><ul><li>Insert.ftl </li></ul><ul><li>insert.vm </li></ul>
    31. 31. Form Validation in Struts 2.0 <ul><li>Can be use several different types </li></ul><ul><ul><li>Programmatically by implementing validate() method in Action class </li></ul></ul><ul><ul><li>Declaratively using XWork’s Validation framework </li></ul></ul><ul><ul><li>Client side validation </li></ul></ul><ul><li>Create <ActionClass>-validation.xml </li></ul><ul><li>Sample uses of XWork validation </li></ul>
    32. 32. Type Conversion <ul><li>Routine type conversion in the framework is transparent </li></ul><ul><ul><li>Boolean, boolean, Integrer, int, Float, float, Long, log, Double, double </li></ul></ul><ul><ul><li>Date with the locale associated with current request </li></ul></ul><ul><li>Register custom type conversion class </li></ul><ul><ul><li><ActionClass>-conversion.properties for class specific conversion </li></ul></ul><ul><ul><ul><li>Point=com.ascendant.converter.PointConverter </li></ul></ul></ul><ul><ul><li>Xwork-conversion-properties for global conversion </li></ul></ul><ul><li>Custom type conversion for more complex objects </li></ul><ul><ul><li>Create class extending StrutsTypeConverter class </li></ul></ul><ul><ul><ul><li>convertFromString() </li></ul></ul></ul><ul><ul><ul><li>convertToString() </li></ul></ul></ul>
    33. 33. Struts Plug-ins <ul><li>Many popular optional features of the framework are distributed as plug-ins </li></ul><ul><ul><li>Spring </li></ul></ul><ul><ul><li>Tiles </li></ul></ul><ul><ul><li>File upload </li></ul></ul><ul><li>Plug-ins are used for </li></ul><ul><ul><li>Introducing new Extension point implementation class </li></ul></ul><ul><ul><ul><li>ObjectFactory : Spring plug-in implements StrutsSpringObjectFactory </li></ul></ul></ul><ul><ul><ul><li>Mutlipart parser: Allows using Pell’s multi part parser for parsing multi-part request </li></ul></ul></ul><ul><ul><li>Define new packages with results, interceptors or actions </li></ul></ul><ul><ul><ul><li>Results: Tiles plug-in introduces Tiles Results type </li></ul></ul></ul><ul><ul><li>Override framework constants </li></ul></ul>
    34. 34. Spring Integration <ul><li>Allows Actions, Interceptors and Results to be created by Spring </li></ul><ul><li>Struts created object can be auto wired by spring after creation </li></ul><ul><li>Provides two interceptors that auto wire actions if not using the Spring Objectfactory </li></ul><ul><li>Add entry for StrutsSpringObjectFactory in struts.properties </li></ul><ul><ul><li>struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory </li></ul></ul>
    35. 35. <ul><li> Spring 2.0: </li></ul><ul><li>Sean Newby, Senior Consultant, Perficient Inc. </li></ul><ul><li>Email: sean.newby@perficient.com </li></ul>
    36. 36. Spring Intro <ul><li>Spring is a popular Java Application Framework that has gained significant momentum in the past few years. </li></ul><ul><li>Made up of several modules that can be used independently </li></ul><ul><ul><li>Inversion of Control Container (Core) </li></ul></ul><ul><ul><li>AOP module </li></ul></ul><ul><ul><li>DAO module </li></ul></ul><ul><ul><li>MVC module </li></ul></ul><ul><li>Portlet MVC became a part of the general Spring 2.0 release in November of 2006. </li></ul>
    37. 37. Client Selection Example <ul><li>At a previous client that decided to Spring MVC. </li></ul><ul><li>Reasons Spring MVC was selected. </li></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>Developers already had experience with other elements of Spring. </li></ul></ul><ul><ul><li>Confident that would be able to find answers to issues in the Spring community. </li></ul></ul><ul><ul><li>Addressed use case we needed to solve (Wizard Portlet). </li></ul></ul><ul><ul><li>Designed around portlet api, addressed Render and Action separately. </li></ul></ul>
    38. 38. Key Portlet MVC objects / concepts <ul><li>DispatcherPortlet </li></ul><ul><ul><li>The controller portlet that handles all requests to the portlet application. Similar to ActionPortlet or ActionServlet in the Struts framework. </li></ul></ul><ul><li>Controller </li></ul><ul><ul><li>Location in Framework where your code begins. Similar to Struts Action class. </li></ul></ul><ul><ul><li>DispatcherPortlet passed request to configured Controller to handle request. Separate methods for render and action phases. </li></ul></ul><ul><ul><li>Several default implementations to handle common use cases: </li></ul></ul><ul><ul><ul><li>ParameterizableViewController – Simple controller that passes to a view. </li></ul></ul></ul><ul><ul><ul><li>SimpleFormController – Controller to handle form submissions. </li></ul></ul></ul><ul><ul><ul><li>AbstractWizardController – Controller to implement wizard like form. Form that extends over several pages. </li></ul></ul></ul><ul><li>HandlerMapping </li></ul><ul><ul><li>Determines which Controller will be called. </li></ul></ul><ul><ul><li>Several out of the box implementations. </li></ul></ul>
    39. 39. Key Portlet MVC objects / concepts cont. <ul><li>ModelAndView </li></ul><ul><ul><li>Object that is returned by Controllers to DispatcherPortlet. </li></ul></ul><ul><ul><li>Contains the Model that needs to be displayed by the view. Also contains the “name” of the view. </li></ul></ul><ul><ul><li>Behavior is similar to a regular Map object. </li></ul></ul><ul><li>ViewResolver </li></ul><ul><ul><li>Maintains a clean separation of View technology. </li></ul></ul><ul><ul><li>Several Default ViewResolvers </li></ul></ul><ul><ul><ul><li>JSP </li></ul></ul></ul><ul><ul><ul><li>Velocity </li></ul></ul></ul><ul><ul><ul><li>XSLT </li></ul></ul></ul><ul><li>Command Objects </li></ul><ul><ul><li>JavaBean type objects. Object is bound to request parameters, usually from form submission. </li></ul></ul><ul><ul><li>Similar to Struts ActionForm, except is does not extend framework specific class. </li></ul></ul>
    40. 40. Simplified Render Sequence Diagram <ul><li>Some steps have been omitted for clarity. </li></ul>
    41. 41. Simplified Action Sequence Diagram <ul><li>Some steps have been omitted for clarity. </li></ul>
    42. 42. Getting Started setting up a Spring Portlet <ul><li>WEB-INF/web.xml and WEB-INF/portlet.xml </li></ul><ul><li>WEB-INF/context/applicationContext.xml – main application wide config file. Values can be overridden in portlet config. </li></ul><ul><li>WEB-INF/<PORTLETNAME>-portlet.xml – individual config file for each portlet. </li></ul><ul><li>WEB-INF/lib/spring.jar – main spring file.* </li></ul><ul><li>WEB-INF/lib/spring-portlet.jar – additions for Spring Portlet MVC </li></ul><ul><li>WEB-INF/lib/spring-modules-validation.jar – additions for validation of forms. </li></ul><ul><li>*could pick out specific Spring jar’s to reduce size of application. </li></ul>
    43. 43. Main Artifacts
    44. 44. Additions to Web.xml <ul><li>Configuration to set up Spring MVC servlet. </li></ul><ul><li>Portlet MVC leverages Servlet base code. </li></ul>
    45. 45. Portlet.xml <ul><li>portlet-class is Portlet MVC class </li></ul><ul><li>portlet-name = <PortletName>-portlet.xml Spring MVC config file. </li></ul>
    46. 46. Spring IoC Core (Dependency Injection) <ul><li>Don’t really need to understand to use Portlet MVC, but it is helpful. </li></ul><ul><li>From Spring Documentation: </li></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>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). </li></ul></ul><ul><li>Really enables “Programming to the interface” </li></ul>
    47. 47. Spring config basics <ul><li>Everything is defined as a bean. </li></ul><ul><li>Container will call methods on defined beans. </li></ul><ul><ul><li>Passes in a String </li></ul></ul><ul><ul><li>Passes in a reference to another bean </li></ul></ul>
    48. 48. Spring Config Basics - inject another bean <ul><li>Creates two beans </li></ul><ul><ul><li>locationDao = new LocationDaoHardCodedImpl() </li></ul></ul><ul><ul><li>locationService = new LocationServiceImpl() </li></ul></ul><ul><li>locationService has a locationDao “injected” into it. </li></ul><ul><ul><li>“name” is method to call (setLocationDao) </li></ul></ul><ul><ul><li>“ref” indicates pass in reference to another defined bean </li></ul></ul>
    49. 49. Spring Config Basics – inject a String <ul><li>Creates a bean referred to as “displayIndexController” </li></ul><ul><ul><li>“value” indicates pass in String </li></ul></ul><ul><ul><li>Injects a String in the first property </li></ul></ul><ul><ul><li>Injects a bean in the second property </li></ul></ul>
    50. 50. Portlet MVC application.xml entries - View <ul><li>Defines where named “views” are located. </li></ul><ul><li>Defines what view technology to use. </li></ul><ul><li>Sample uses jsp’s located in /WEB-INF/jsp/ directory </li></ul>
    51. 51. Portlet MVC application.xml entries - Messages <ul><li>Defines message bundle to use for i18n. </li></ul>
    52. 52. Portlet MVC application.xml entries - Exception <ul><li>Define default views for exceptions. </li></ul><ul><li>PortletSecurityException goes to notAuthorized view. </li></ul><ul><li>UnavailableException goes to notAvailable view. </li></ul>
    53. 53. Sample Portlets <ul><li>Spring Display </li></ul><ul><ul><li>Example of a ParameterizableViewController </li></ul></ul><ul><ul><li>Displays dynamic data </li></ul></ul><ul><li>Spring Form </li></ul><ul><ul><li>Example of a SimpleFormController </li></ul></ul><ul><ul><li>Handles a form submission </li></ul></ul><ul><li>Spring Wizard </li></ul><ul><ul><li>Example of an AbstractWizardFormController </li></ul></ul><ul><ul><li>Presents a form over several pages </li></ul></ul><ul><ul><li>Assists with navigating back and forth </li></ul></ul>
    54. 54. SpringDisplay-portlet.xml – ParameterDispatchController <ul><li>Declares controller used, referred to as displayIndexController. </li></ul><ul><li>Property viewName corresponds to jsp that will be displayed. </li></ul>
    55. 55. PortletModeParameterHandlerMapping <ul><li>Allows you to call other controller from the view. </li></ul>
    56. 56. DisplayIndexController <ul><li>ParameterizedViewController implementation </li></ul>
    57. 57. SpringForm-portlet.xml - SimpleFormController <ul><li>formView – view to display form initially </li></ul><ul><li>successView – confirmation page once form submitted </li></ul><ul><li>commandClass – form backing object, like ActionForm </li></ul><ul><li>commandName – how to refer to commandClass in view </li></ul><ul><li>validator – validation to use </li></ul>
    58. 58. FormIndexController <ul><li>SimpleFormController implementation. </li></ul>
    59. 59. formIndex.jsp <ul><li>Uses Spring 2.0 style form tags </li></ul><ul><li>commandName is form backing object </li></ul><ul><li>errors path=“*”, show validation errors for all fields </li></ul><ul><li>path=“name”, binds to name property of location object </li></ul>
    60. 60. Form Validation in Spring MVC <ul><li>Can use several different types </li></ul><ul><ul><li>Programmatically through Validator interface </li></ul></ul><ul><ul><li>Declaratively using Apache Commons Validation Framework </li></ul></ul><ul><ul><li>Declaratively using Valang </li></ul></ul><ul><li>Sample uses Valang validation </li></ul>
    61. 61. SpringWizard-portlet.xml <ul><li>pages – List of view that are available through the wizard. </li></ul><ul><ul><li>Order is significant. </li></ul></ul><ul><ul><li>wizardBasicInfo is page 0. </li></ul></ul><ul><ul><li>wizardContactInfo is page 1. </li></ul></ul><ul><ul><li>wizardBillingInfo is page 2. </li></ul></ul>
    62. 62. WizardIndexController
    63. 63. wizardContactInfo.jsp <ul><li>Middle step in wizard </li></ul><ul><li>JSP wizard navigation code, _target[PAGEINDEX] </li></ul><ul><li>Uses older Spring 1.0 form tags </li></ul><ul><li>_page1, pageAttribute from configuration file </li></ul>
    64. 64. Spring MVC Pros / Cons <ul><li>Pros </li></ul><ul><ul><li>Easy for experienced Struts Developers to transition. </li></ul></ul><ul><ul><li>Strong community, should be viable solution for some time. </li></ul></ul><ul><ul><li>Modular, pick and choose features. Entry point to use more of the Spring framework. </li></ul></ul><ul><ul><li>Design goal of the framework is to be non-evasive, your code does not depend on the framework. </li></ul></ul><ul><ul><ul><li>Class extension or Interface implementation. </li></ul></ul></ul><ul><ul><li>Good documentation for open source project. </li></ul></ul><ul><li>Cons </li></ul><ul><ul><li>Can be xml config intensive. </li></ul></ul><ul><ul><li>Can not call IBM and create a PMR for support. </li></ul></ul>
    65. 65. Additional Information and Resources <ul><li>Websphere Portal Business Solutions Catalog : </li></ul><ul><ul><li>http://catalog.lotus.com/wps/portal/portal </li></ul></ul><ul><li>Webspere Portal Product Information: </li></ul><ul><ul><li>http://www-306.ibm.com/software/genservers/portal </li></ul></ul><ul><li>WebSphere Portal Information Center Documentation: </li></ul><ul><ul><li>http://www.ibm.com/developerworks/websphere/zones/portal/proddoc.html </li></ul></ul>
    66. 66. Summary <ul><li>IBM Perspective </li></ul><ul><li>Struts 2.0 </li></ul><ul><li>Spring 2.0 </li></ul>
    67. 67. Avoid Boring People: Lessons from a Life in Science (Hardcover) by James D. Watson (Author)
    68. 68. Programming Portlets : From JSR 168 to IBM WebSphere Portal Extensions Ron Lynn , Joey Bernal , Peter Blinstrubas , Stefan Hepper , Usman Memon , Varadarajan ( Varad ) Ramamoorthy