Easy Java Portlets

  Portlet is generated fragment (compliance with the norms of specific markup language (such as HTML, XML) fragment) Web Component.    Another fragment of a complete document.    This paper introduces the Java Portlet on the Java Specification Request (JSR) 168 standard.    It explains how to use BEAWebLogic Workshop 8.1 Java Portlet SP2 to create, and how will these portlet deployment to BEA WebLogicPortal 8.1Sp2 on.    I will introduce some of the key concepts, such as portals, desktop and portlet, and described in detail multiple portlet window mode and the state.    I will also explain how to use Workshop to design, implementation, configuration and implementation of portlet. 


  JSR 168 defines the JavaPortlet norms.    Portal is a Web application and a portlet polymerization.    Portlet containers running portlet, and manage their life cycle.    JSR168 portlet and the definition of a contract between the portlet containers, it does not define portlet containers with a contract between the portal.    Portal for the Implementation of the portal provider. 

  BEA WebLogic portal BEA WebLogic Portal (8.1 SP2) to support the current version of the different types of portlet: JSP / HTML portlet, JavaPageFlow portlet, Struts portlet and Java portlet, and the future will also support other portlet, such as WebServices for Remote Portlets (WSRP).    We will focus on Java portlet. 

  WebLogic Portal JSR 168 provides a description of the gateway functions, including but not limited to: books and pages portlet organization, multi-channel support and the use of skin and skeleton and shell customization. 

  In order to be able to continue, in the next part, please complete the following: 
  Use WebLogic Domain Configuration Wizard to create a portal domain (such as JSR168PortalDomain). 
  Use WebLogic Workshop to create a domain established by the use of the above gateway applications (such as JSR168PortalApp). 
  Application procedures in the portal to create a Web portal (such as JSR168PortalWebProject). 
  Web portal in the project to create a WebLogic Portal. Portal document (such as JSR168.portal). 
  Start the server examples. 

  Create your first Java Portlet 
  The following steps describe how to create your first JSR 168 portlet. 
  Web portal in the project (such as JSR168PortalWebProject), the use of WebLogic Workshop for portlet (all FirstPortlet) to create a new folder. 
  In the new folder using Wizard through the creation of the corresponding. Portlet creation of a new portlet documents (such as Firstportlet). 
  Choose the portlet types Java Portlet. 
  Heading designated (eg First). 
  Designated definition labels (such as first). 
  Designated class name (such as com.malani.examples.portlets.jsr168.FirstPortlet). 
  Open portal (such as JSR168.portal). 
  The portlet (such as Firstportlet) drop to the portal page (such as JSR168.portal). 
  Run. Portal document to be tested. 

  Your first JSR 168 portlet have successfully run!    However, a wizard behind what? 
  It created a specific WebLogic Workshop and in the WebLogic Portal. Portlet document.    . Portlet document with a specific to the Workshop and WebLogic Portal. Portal lease documents. 
  Creation of a wizard. Java documents (such as com.malani.examples.portlets.jsr168.FirstPortlet.java), which placed in the WEB-INF/src directory. 
  WEB-INF/portlet.xml wizard to create a configuration file, as well as portlet inserted in the document as a single entry.    The portlet entry looks as follows: 

<portlet>
  <description> Description goes here </ description> 
  <portlet-name> First </ portlet-name> 
  <portlet-class> Com.malani.examples.portlets.jsr168.FirstPortlet 
  </ Portlet-class> 
<portlet-info>
  <title> First </ title> 
  </ Portlet-info> 
  </ Portlet> 

  Java Portlet type in the example, the wizard generated PortletJava javax.portlet.GenericPortlet file extension of the category.    GenericPortlet class implements javax.portlet.Portlet interface.    Figure 1 is a Unified Modeling Language (UML) class diagram, described these relations.    Portlet through direct interface to achieve, can prepare a portlet.    However, the creation of portlet GenericPortlet is a more convenient method.    First, we look at the life cycle of portlet, portlet window mode and the state. 


  Figure 1 

  Portlet life cycle in order to successfully create an portlet, you must comply with the portlet lifecycle.    Javax.portlet.Portlet interface definition of the method of the life cycle, the life cycle approach is init (), render (), processAction () and destroy ().    When deployed portlet example, calls init () method.    It used to obtain any required expensive resources (such as background connection), and the implementation of other one-time activities.    When the case was withdrawn portlet deployment, the use destroy () method to the release of these resources. 

  Portlet norms render a clear distinction between the request and action request.    Figure 2 describes the portlet requests and the response of a UML class diagrams.    Portal on the page render the request would lead to the pages of each portlet on call render () method when the user calls on a specific portlet action (usually HTML form submitted), will be called the portlet The processAction () method.    In this way, users switch to the action request processAction () method call and a render () method calls several times. 


  Figure 2 

  Figure 3 shows a sequence diagram shows that call processAction () method results, as well as portlet on the same page follow-up render () method call.    For more information, please refer to dealing with a movement. 


  Figure 3 

  There are two overloaded init () method, a no parameters, there is a javax.portlet.PortletConfig another instance of the class.    NOTE: on the init (PortletConfig) have a special caveat.    Call super.init (aPortletConfig) failure will lead to a NullPointerException.    Contained in the source code examples illustrate the behavior Initportlet (source code can be found in www.sys-con.com/weblogic/source.cfm). 

  Portlet model JSR168 Portlet definition of the three modes: VIEW, EDIT and HELP.    In an example portlet can happen at any time in a portlet model.    Other custom portlet modes (such as configuration, and source) are possible.    VIEW mode is the default mode.    Portlet norms proposed EDIT mode allows the user to customize the portlet portlet example, as well as portlet HELP pattern display on the use of information.    Portlet must support VIEW model, but in the portlet model and the EDIT HELP mode support is optional.    For example, portlet First portlet example EDIT mode and does not support HELP model. 

  JSR168 state definition window three Window Status: NORMAL, MINIMIZED and MAXIMIZED.    Portlet example any time can be a window happens to be the state.    Other custom window state (such as half a page) is also possible.    In NORMAL mode, the screen area of portlet a small part.    Portlet screen sharing with other states.    In MINIMIZED state, the content of portlet been hidden.    MAXIMIZED in the state, portlet content of the majority of the screen area.    Other shared the same page in MAXIMIZED state portlet been hidden.    For example, portlet First examples to support all three window state. 

  GenericPortlet categories you create the most portlet javax.portlet.GenericPortlet category will be extended, rather than directly to javax.portlet.Portlet interface.    GenericPortlet class implements render () method.    If portlet state of the window was minimized, then render () method can not do anything.    If portlet state of the window is not minimized, then render () method installed in portlet.xml specified in the document title, and call doDispatch () method.    According Portlet model doDispatch () method appropriate to call doView (), doEdit () and doHelp () method.    This category because GenericPortlet help achieve render () method, and provide doView (), doEdit () and doHelp () method to cover, GenericPortlet Portlet analog interface more convenient for extension. 

  First portlet consider examples.    FirstPortlet category expanded GenericPortlet, FirstPortlet rewritten doView () method. 

  Public void doView (RenderRequest request, RenderResponse response) 
  Throws PortletException, IOException 
  ( 
  Response.setContentType ( "text / html"); 
  Response.getWriter (). Write ( "<p> Hello World </ p>"); 
  ) 

  NOTE: Call setContentType () method before calling getWriter () method will lead to java.lang.IllegalStateException. 

  Implementation Portlet model VIEW mode is mandatory, but the EDIT and HELP mode is optional.    In order to achieve EDIT HELPportlet model and the need to achieve in the portlet category appropriate doEdit () and doHelp () method.    Please refer to the source code included in the sample (This article's source code can be found in www.sys-con.com/wldj/sourcec.cfm) in the portletMode.    In addition, the following must be portlet.xml configuration models: 

<supports>
  <mime-type> Text / html </ mime-type> 
  <portlet-mode> Edit </ portlet-mode> 
  <portlet-mode> Help </ portlet-mode> 
  </ Supports> 

  NOTE: Laws portlet.xml configuration file, but does not realize portlet in the corresponding category, will lead javax.portlet.PortletException. 

  JSR 168 window to achieve state did not describe prohibited state support of the window method.    However, the realization of the WebLogic Portal on their banned.    To disable portlet window on the state of the support required in the document weblogic-portlet.xml exclude window Status: 

<portlet>
  <portlet-name> State </ portlet-name> 
<supports>
  <mime-type> Text / html </ mime-type> 
  <excluded-window-state> Minimized </ excluded-window-state> 
  <excluded-window-state> Maximized </ excluded-window-state> 
  </ Supports> 
  </ Portlet> 

  Please refer to the source code examples in the portlet State. 

  Includes JavaServer Pages (JSPs) 
  PortletFirst consider the doView () method, the method obtained Writer examples and direct output HTML fragment.    For a variety of reasons (such as Java to achieve logic and HTML view of the performance of Separation), often do not recommend direct output HTML fragment.    The recommended method is to demonstrate the use of JSP view.    Portlet in the way of the implementation of business logic, render parameters and settings include JSP.    In order to contain a specific JSP, should first obtain PortletContext.    From PortletContext example, by calling getRequestDispatcher () method was a PortletRequestDispatcher examples.    By calling the include () method contains JSP.    For example: 

  / / Execute the necessary logic here … 
  PortletRequestDispatcher aDispatcher = 
  GetPortletContext (). GetRequestDispatcher ( 
  "/ IncludePortlet / includeView.jsp" 
);
  ADispatcher.include (aRequest, aResponse); 

  Note: in the implementation render () method, may use only one portlet PortletRequestDispatcher object. 
  Please refer to the source code included in the portletInclude.    JSP pages (such as includeView.jsp) does not contain root HTML tags (eg <html>, and <title> <body "), because these labels provided by the portal framework.    JSP pages contain only show the necessary HTML portlet fragment. 

  Action in dealing with a standard Web application, an HTML form will lead to the implementation of some business logic.    Deal with the results of operations, or as an attribute has been set up in the conversation and request or transmitted or contained to a JSP. 

  In a JSR 168 portlet, a URL HTML form of action should be what?    JSR 168 defines a JSP tag library called portlettaglib.    HTML form of action URL can be used actionURLportlet label generation.    For example (please refer to favoriteColorEdit.jsp): 

  <Form action = "<portlet:actionURL/>" method = "post"> 

  </ Form> 

  Submit the HTML form will lead the call portlet processAction (ActionRequest aRequest, ActionResponseaResponse) method.    As usual, can be the object by calling the request getParameter () method to obtain the form parameters.    NOTE: Calling moves through the submission of the form, but has no portlet processAction () method will lead to javax.portlet.PortletException. 

  ProcessAction () method set up in response target value.    Do not use or ActionResponse ActionRequest object setAttribute () method.    Value will not processAction () transfer to render () method, but in JSP is not used.    Contrary to use ActionResponse object setRenderParameter () method.    These parameters will render all the follow-up request render available, and this is typical of Web application requests very different attributes.    Typical Webapplication request property can be used only for a request.    On the other hand, render request parameters for many follow-up requests can render.    Render parameters can be maintained until the value of the re-implementation of the action Explicit modify or delete. 

  Consider portletFavoriteColor.    It VIEW model shows the color of a user preference, but in EDIT mode changes.    EDIT mode in preference to the color of choice will be called processAction () method.    The method was preferred color request parameters, and is set to render its parameters.    Thus, the preference parameters render the color in all the follow-up will render in the request are available. 

  Presented by the parameters of what is shown in the JSP?    Portlet from the label should be used for the definition of portlet defineObjects labels to target.    The labels enable renderRequest, renderResponse and portletConfigportlet object in the pages available.    RenderRequest parameters by calling the object getParameter () method to display.    Please refer to the source code contained in the example in favoriteColorView.jsp. 

  PortletFavoriteColor also demonstrated the other concepts.    The first one is how to processAction () method used to change the method portlet programming model.    Call ActionResponse object setPortletMode () method to modify portlet model.    The second concept is the use of an HTML link to amend the portlet model.    The portlet links to the tag library from the renderURL label generation.    According to hope that the portlet model designated portletMode the value of the attribute.    Please refer to the source code examples in the category and favoriteColorView.jsp FavoriteColorPortlet pages. 

  Portlet Preferences 
  PortletPreferences (Portlet first option) is the basic portlet configuration data.    A preference is a "name and value" right.    The name of the type is a string, and the type of value is a string or string array.    Portlet Preference unsuitable for storage of arbitrary data.    Portletpreferences portlet containers to provide sustainability.    In WebLogic Portal, the preference of a lasting only in the following two conditions are true only for the role: 
  Portal running on the desktop, rather than DOT portal model. 
  Users already logged. 

  DOT gateway to the desktop and create a model in WebLogicWorkshop. Portal documents, like books, pages and portlet can be towed into等项. Portal document,. Portal documents can run directly from the Workshop.    However, some features, such as the storage preferences, in which DOT gateway mode operation is not used (DOT also known as single mode portal document mode (Single File Mode)). 

  Other models as desktop models.    Create a portal using PortalAdministrator.    In the portal, a desktop been created.    Like books, pages and portlet等项been created and placed in the desktop.    In this mode, certain functions, such as preferences of storage is available (also known as desktop mode mode (Streamed Mode)). 

  In continuation of the discussion prior to the creation of a desktop: 
  Start Portal Administration (for example, http://localhost:7001/JSR168PortalAppAdmin/).    Portal Administration launched a direct approach from the start in the Workshop.    Portal menu choice, selected Portal Administration menu item. 
  Sign-Portal Administration. 
  Creating a new portal (for example, JSR168). 
  In the portal, create a new desktop (d1). 
  Will be added to the desktop LoginPortlet a page. 
  ContactPortlet will be added to the desktop in a single document. 

  Contact portlet Portlet Preferences examples demonstrating Portlet Preferences.    Portlet Preferences can be static or dynamic.    Portlet together with the static preferences in portal.xml specified in the document.    For example, ContactPortlet into contact with a preference-the preferences.    Contact-preference has been the default value specified: 

<portlet-preferences>
<preference>
  <name> Contact-preference </ name> 
  <value> Email </ value> 
  </ Preference> 
  </ Portlet-preferences> 

  Portlet.xml dynamic preferences is not in the pre-defined configuration files.    When running portlet, these preferences are stored and read.    During the operation, a javax.portlet.PortletPreferences interface contains examples of these preferences.    The examples of the object by calling PortletRequest getPreferences () method was.    The value of specific preferences preferences example by calling on the getValue () method to obtain. 

  Call preferences examples setValue () updates the value of a preferences.    However, additional steps need to submit these amendments.    Examples of preferences store () method is called to make a lasting and preferences.    Preferences only processAction () method changes.    If processAction () method does not call store () method, any preferences examples of changes will be discarded.    NOTE: As mentioned earlier, if you are not logged in DOT portal or gateway mode, then call store () method will lead to a run-time anomaly. 

  Servlet in the portlet and there are many similarities between the points.    However, they are also an important distinction.    Portlet in the establishment of norms on standardized servlet.    Portlet containers exist in servlet container.    Like servlet deployment in a Web application, portlet as well.    Servlet and Web applications using portlet.xml configuration files.    Explicit is a servlet life cycle: init (), doGet (), doPost ().    Similarly, a portlet also have significant life-cycle: doView (), doEdit (), processAction ().    Portlet servlet class and the method must be encoded security threads. 

  However, there are also important differences.    Servlet be permitted to include, forward and redirect operation; However portlet include only be permitted to operate.    Servlet can show a full page, but only to the pages portlet fragment.    Portlet a strict definition of the modalities and Window portlet state, which unlike servlet.    Portlet a more formal request to render the request and action request processing, they also have preferences.    Portlet not servlet! 

  CONCLUSION By using a simple wizard and the creation of portlet description, and describes the life cycle of portlet portlet category, as well as realization of the internal working methods, and describes the portlet.xml configuration file and the corresponding weblogic-portlet.xml configuration file structure and semantics.    Of the various concepts, such as portlet window mode and the state, the paper also explained.    This paper demonstrates the use of portlet tag library and portlet in the form handling.    Finally, I described how to use portletpreferences.    This paper introduced the understanding of these concepts and knowledge, you can create and deploy their own powerful portlet on the road forward. 

  Thanks to thank Subbu Allamaraju, Max Cooper, Steve Ditlinger, David Lu, Roshni Malani and Alex Toussaint, they carefully read the article, and provided valuable feedback. 

  References to discuss the article and questions from here: www.bartssandbox.com.    For free membership. 
  Download, read JSR 168: www.jcp.org/en/jsr/detail?id=168 
  WebLogic Portal documents starting point: e-docs.bea.com/wlp/docs81/index.html 
  Workshop Help build the Java Portlet: e-docs.bea.com/workshop/docs81/doc/en/core/index.html 
  WebLogic Portal 8.1 with the development of JSR 168 Portlet: dev2dev.bea.com/products/wlportal81/articles/JSR168.jsp 
  Web Services for Remote Portlets (WSRP) specification: www.oasis-open.org/committees/tc_home.php?wg_abbrev=wsrp 
  Try WSRP: dev2dev.bea.com/codelibrary/code/wsrp_supportkit.jsp 
  Single File Mode and Streamed Rendering Mode: Click here! 
  Portlet norms of the article: 
  — Introduction Portlet norms, Part 1: 
  Www.javaworld.com/javaworld/jw-08-2003/jw-0801-portlet_p.html 
  — Introduction Portlet norms, Part 2: 
  Www.javaworld.com/javaworld/jw-09-2003/jw-0905-portlet2_p.html 
  JSR 168 of the White Paper description: Click here! 
  Java Passion Portlet speech Notes: www.javapassion.com/j2eeadvanced/Portlet4.pdf 

  Source code - Zip files 
  English Original: http://www.sys-con.com/story/?storyid=45565&DE=1 

  About the author 
  PrakashMalani in the structure, design and software development experience.    He has many applications in the field of software development, such as entertainment, finance, retail, medical, communications and interactive television.    He likes to practice and study advanced technology, such as the Java 2 Enterprise Edition (J2EE), the Unified Modeling Language (UML) and eXtensible Markup Language (XML).    He also California State Polytechnic University, Pomona campus, and other agencies teach best practices and design patterns.    He's well-known in the industry publications published many articles, such as JavaWorld and WebLogic Developers Journal.    At present, he is helping to organize Los Angeles BEA Users Group (LABEAUG). 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Recommend Articles

Comments

Leave a Reply