A detailed description of the Portlet communication process

Source: Internet
Author: User

A detailed description of the Portlet communication process

During the development of the Portal, the communication between Theme and Portlets, and the communication between Portlets, is a common problem that developers often encounter. Usually portlets need to be able to communicate with each other, that is, the state of a portlet changes, to notify other portlets, the portlet status of the notifications will be changed accordingly. According to the JSR 168 specification, portlets are managed by containers and are independent of each other and do not share Session objects. So how do you pass parameters between portlets? In this article, the way parameters are passed is described in terms of different scenarios.

IBM Rational Application Developer V7.0 and IBM Rational Software Architect V7.0 are the first products in the IBM Rational Software delivery platform product family, which includes the IBM Full support for WebSphere Portal V6.0 development. In the following example, we introduce the parameter passing of the Portlet using the IBM Rational software Architect V7.0 as the development platform.

Concept Introduction

First, let's first introduce some basic concepts of portal.

Definition of Portal

Portal is a web-based application that mainly provides personalization, single sign-on, content integration from different sources, and the presentation layer for storing information systems.

Definition of Portlets

A portlet is a Java-based Web component that manages and processes requests by the Portlet container and dynamically generates output content. Just like Servlets is designed to bring together the content of a composite page. Typically requesting a portal page will cause multiple portlets to be called. Each portlet generates marker segments and is combined with other portlets generated markup segments to be embedded within the markup of the portal page.

Portal specification

A JSR168 was developed for the specification Portal,sun at the end of 2003, which defines the portlet standards and gives an implementation interface.

Portlet composition

Each portlet page consists of one or more portlet windows, and each portlet window is divided into two parts: one is the theme appearance (Theme), which determines the style of the Portlet window's title bar, control, and bounds, and the other is the Portlet segment, which is made up of portle T apply the Fill.

Communication between different portlets within the same page

For portlets placed on the same page, if one of the Portlets commits the portal refresh, each portlet in the page will be render. There are typically 3 steps to implementing a parameter's delivery: Defining the source Portlet, defining the target portlet, associating the source portlet, and the target portlet.

Creating a send-side Portlet

First we build a Portlet Project, click Next;

Figure 1. New Portlet Project

In Project name, enter Myprojectproj,target runtime to select Websphere Portal v6.0, select the JSR 168 portlet in the Portlet API, select Create a portle T, enter Senderportlet in Project name, select the Basic portlet in the portlet type, select Show Advanced Settings, and click Next;

Figure 2. Fill out Portlet Project basic properties

Can't you see the picture clearly? Please click here to view the original (larger image).

In the Project facets panel, select Java, select 1.4 in version, the other items remain the same, click Next;

Figure 3. Configuring the Facets Property

Can't you see the picture clearly? Please click here to view the original (larger image).

In the Portlet Settings dialog box, modify the package Prefx to Com.ibm.myportlet, and other options remain default, click Finish.

Figure 4. modifying Portlet configuration parameters

Can't you see the picture clearly? Please click here to view the original (larger image).

At this point, we have created a new Portlet project that contains a portlet called Senderportlet.

Creating a Receive-side Portlet

Right-click in project and click New->portlet to bring up the Portlet Creation Wizard dialog box;

Figure 5. New Receiverportlet

Sample code or footage download for this article

Select Myportletproj in Project, select the Baisc Portlet in Project name, enter Receiverportlet,portlet, and click Next;

Figure 6 Portlet Properties

In the Portlet Settings dialog box, modify the package Prefx to Com.ibm.myportlet, and other options remain default, click Finish.

Figure 7. modifying Portlet configuration parameters

So we created the Receiverportlet and added it to the Myportletproj project.

Right click on Senderportlet, click coorperative-> Enable this protlet to Send Data (Source) ...

Figure 8. Portlet collaboration-send-side configuration

In the Coorperative dialog box, enter//myportletproj#mydatatype in the input box Data type URI, enter java.lang.String in Java type, and click Next.

Figure 9. Define the type of data sent

Can't you see the picture clearly? Please click here to view the original (larger image).

Leave the default Value Outputproperty property in property caption, leave the default value sourceaction action in action caption, and click Next;

Figure 10. Define the Action name and parameter name of the sending side

Can't you see the picture clearly? Please click here to view the original (larger image).

Click Finish.

Figure 11. Enable to send data

Can't you see the picture clearly? Please click here to view the original (larger image).

Processaction code fragment in Senderportlet. We have defined the name of the sent parameter as Outputproperty

Listing 1. Send-side code Snippets

String Value=request.getparameter (Form_text); Request.setattribute ("Outputproperty", "Get the Parameter from same page:" +value);

Right-click on the Receiverportlet and click Coorperative->enable this protlet to Receive Data (Target) ...

Figure 12. Portlet collaboration-Receive-side configuration

In the Coorperative dialog box, enter//myportletproj#mydatatype in the input box Data type URI, enter java.lang.String in Java type, and click Next.

Figure 13. Define the type of data received

Can't you see the picture clearly? Please click here to view the original (larger image).

Enter Form_text,action value input targetaction in Action parameter, select Request Attribute in location, enter in PropertyName Inputproperty, click Next;

Figure 14. Define the Action name and parameter name of the receiving end

Can't you see the picture clearly? Please click here to view the original (larger image).

Leave the default Value Inputproperty property in property caption, leave the default value targetaction action in action caption, and click Next;

Figure 15. Define the parameter names on the receive side

Can't you see the picture clearly? Please click here to view the original (larger image).

Click Finish.

Figure 16. Enable to receive data

Can't you see the picture clearly? Please click here to view the original (larger image).

Partial Code implementation

Add the following code to the JSP file senderportletview.jsp on the sender side

Listing 2 send-side UI code Snippets

<div > <%/******** Start of sample code ********/%> <form method= "POST" action= "<portlet:actionurl /> "> <input name=" wireaction "value=" Form_text "type=" hidden "/> <input id=" <%= Com.ibm.myportlet.senderportlet.action_type%> "Name=" <%=com.ibm.myportlet.SenderPortlet.ACTION_TYPE%> " Type= "hidden"/> <label for= "<%=com.ibm.myportlet.SenderPortlet.FORM_TEXT%>" > Enter parameter Value: & lt;/label><br> <input name= "<%=com.ibm.myportlet.SenderPortlet.FORM_TEXT%>" type= "TEXT" value= "   <%=formText%> "/> <input name=" <%=com.ibm.myportlet.SenderPortlet.FORM_SUBMIT%> "type=" SUBMIT " onclick= "Javascript:document.getElementById (' <%=com.ibm.myportlet.SenderPortlet.ACTION_TYPE%> '). value=0;   return true; " Value= "Pass parameter to same page"/> </FORM> <%/******** End of sample code *********/%> </DIV>

Sample code or footage download for this article

Processaction code fragment in Receiverportlet. We have defined the name of the sent parameter inputproperty, after receiving the parameter value, put it in the session in order to use when the page is displayed.

Listing 3 receive-side code snippets

Sessionbean Sessionbean = Getsessionbean (request);    String inputproperty = (string) request.getattribute ("Inputproperty");   Inputproperty = Request.getparameter ("Inputproperty"); if (inputproperty!=null) Sessionbean.setformtext (Inputproperty);

Portlet Publishing and Configuration

Log in to the console of the portal and deploy the exported Myportletproj.war to the Portal server. To do this, click the Portlet Management->web moudles->install, select the path where the Myportletproj.war is located, and click Next.

Figure 17. Update Portlet Project

Can't you see the picture clearly? Please click here to view the original (larger image).

Click Finish directly.

Figure 18. Portlet List

Can't you see the picture clearly? Please click here to view the original (larger image).

In the portal console, click Portal User interface-> Manage pages->content root->home, click New Page, enter senderpage in title, click OK.

Figure 19. New Senderpage

Can't you see the picture clearly? Please click here to view the original (larger image).

In the pages in Home page, click on the Senderpage right side, click on the Add Portlet, select Senderportlet, click Addportlet again, select Receiverportlet,:

Figure 20. Modify the Senderpage page layout

Can't you see the picture clearly? Please click here to view the original (larger image).

Click Wires, Associate Senderportlet and Receiverportlet. Make the following settings:

Source Portlet:senderportlet Sending:outputproperty Property target page:senderpage Target Portlet:receiverpo Rtlet receiving:targetaction Action,inputproperty Property wire Type:P ublic

Figure 21. Connecting Senderportlet and Receiverportlet

Can't you see the picture clearly? Please click here to view the original (larger image).

Tap, and then click Done.

Communication between different pages of the Portlet

The portlet parameter passing of different pages mainly uses the property Broker's API implementation, in principle, it adopts the Publish/subscribe (publish/subscribe) mode. In the source portlet, define the parameter name, the parameter type, and publish to the property broker, and the target portlet takes the parameters from the property broker and converts them to the appropriate type.

We still use the above Senderportlet as the source Portlet, adding the following code to the Processaction method.

Portlet that the manifest source portlet transmits parameters to other pages

Partial Code implementation

Listing 4. Send-side code Snippets

  PropertyValue propertyvalues[] = new PROPERTYVALUE[1];    Context CTX = new InitialContext (); Propertyfactory service = (propertyfactory) ((portletservicehome) ctx. Lookup ("portletservice/ Com.ibm.portal.propertybroker. Service. Propertyfactory ")). Getportletservice (Com.ibm.portal.propertybroker.service.  Propertyfactory.class);  Propertycontroller PropertyKey = service.createproperty (request);  Propertykey.setname (Form_text);  Propertykey.setclassname ("java.lang.String");  Propertykey.setdirection (direction.out);  String Value=request.getparameter (Form_text);  Propertyvalues[0] = service.createpropertyvalue (Request, PropertyKey, "Get the Parameter from Sender page:" +value);  Name uniqueName = new Compositename ("Portal:uniquename");  Uniquename.add ("Page.myreceiverpage");  Ctx=new InitialContext ();  ObjectID thispage = (ObjectID) ctx.lookup (uniqueName);  Ctx=new InitialContext ();  Propertybrokercontextpassingservice Service1 = (propertybrokercontextpassingservice)  ((portletservicehome) ctx.lookup ("Portletservice/com.ibm.portal.propertybroker. Service. Propertybrokercontextpassingservice ")). Getportletservice (com.ibm.portal.propertybroker. Service.  Propertybrokercontextpassingservice.class);  Service1.changedproperties (request,response,thispage,propertyvalues);

Sample code or footage download for this article

In the JSP file senderportletview.jsp of the sender, we add a button to pass the parameters to different pages

Listing 5. Send-side UI code modification

<input name= "<%=com.ibm.myportlet.SenderPortlet.FORM_SUBMIT%>" type= "SUBMIT" onclick= " document.getElementById (' <%=com.ibm.myportlet. Senderportlet.action_type%> '). Value=1;return true "value=" Pass parameter to another page "/>

Next we create the portlet on the receiving end, as in the method of creating the Portlet. We name this Portlet receiver1portlet. Open the Portlet.xml file and add the following code in the Receiver1portlet section to enable the target Portlet to access the PageContext and receive the parameters.

Listing 6. Portlet.xml parameter Configuration

<portlet-preferences> <preference> <name>com.ibm.portal.pagecontext.enable</name> <val Ue>true</value> </preference> </portlet-preferences>

Add the following code to the Receiver1portlet Processaction method, which takes the Com.ibm.portantext object from request and resolves the parameter form_text:

Listing 7. Receive-Side code snippets

Sessionbean Sessionbean = Getsessionbean (request);  String Form_text=null;  String specialaction = Request.getparameter ("com.ibm.portal.action");    Map map = null;; if ((specialaction!=null) && (specialaction.equals ("com.ibm.portal.pagecontext.receive")) {map = (ja    VA.UTIL.MAP) Request.getattribute ("Com.ibm.portantext");   } if (map!=null) {form_text = (String) map.get (Form_text); } if (Sessionbean! = null) Sessionbean.setformtext (Form_text);

We export Myportletproj.war again, open the Portal console, click Portlet Management->web moudles->update, select the path where Myportletproj.war is located, click Next;

Portlet Publishing and Configuration

Figure 22. Update Portlet Project

Can't you see the picture clearly? Please click here to view the original (larger image).

Click Finish directly.

Click Portal User interface-> Manage pages->content root->home in the Portal console, click New Page, enter receiverpage in title, click OK 。

Figure 23. New Receiverpage

Can't you see the picture clearly? Please click here to view the original (larger image).

On the pages in home page, click Receiverpage on the right, click the Add Portlet, select Receiver1portlet, and click Done. :

Figure 24. Modify the Receivepage page layout

Can't you see the picture clearly? Please click here to view the original (larger image).

In the portal console, click Portal settings-> Custom Unique Names, in Resource type, click Pages, click Receiverpage to the right, and enter page in Unique name. Myreceiverpage, click OK. Note that the unique name entered here is identical to the unique name in Listing 4 above.

Figure 25. Specifies that the page uniquely identifies

Theme Communication with the Portlet

In real-world development, it is also common to pass parameters from theme to Portlets. Usually we use HttpRequest as the medium of the parameter.

Modify Theme

Usually the root directory of Theme is located in C:ibmwebsphereprofileswp_profileinstalledapps%host_name%wps.ear Wps.warthemes, in this article we use IBM's default Theme , whose path is located in Htmlibm, in BANNER_TOOLBAR.JSPF <div class= "Toolbar" > below, we add the following code:

Sample code or footage download for this article

Listing 8. UI Code Snippets in Theme

<%if (Request.getparameter ("Hello")!=null) {request.setattribute ("Hello", Request.getparameter ("Hello")); }%> <div class= "Toolbarlink" > <portal-navigation:urlgeneration contentnode= "page.mythemepage" layou Tnode= "Portlet.theme" > <form name= "myForm" method= "post" action= "<% wpsurl.write (out); %> "> <input type=" hidden "id=" Hello "name=" hello "value=" Hello World, I am from Theme. " /> </form> <a class= "Toolbarlink" href= "#" onclick= "Myform.submit ();" > </portal-navigation: Urlgeneration>say hello</a></div>

Partial Code implementation

Below we create a new Portlet to receive the arguments from the Theme. The method for creating portlets is the same as above. We name this Portlet themeportlet. In the Doview method, add the following code:

Listing 9. Themeportlet Receive parameter Code snippet

Check if portlet session exists Sessionbean Sessionbean = Getsessionbean (request);   if (sessionbean==null) {response.getwriter (). println ("<b>no PORTLET SESSION yet</b>");  Return  } portletrequest portletrequest= (portletrequest) request;  HttpServletRequest (portletrequest). Gethttpservletrequest ();  String param = null;    if (("Hello")!=null) param = ("Hello"). toString ();   if (param!=null) {System.out.println ("get the Parameter:" +param);   Sessionbean.setthemeparam (param);  ("Hello"); }

After the parameter is received, we want to remove the Hello property from HttpRequest, otherwise the portlet refresh that is caused by each page commit will receive the parameter, and usually we want to receive it only once.

We export Myportletproj.war again, open the Portal console, click Portlet Management->web moudles->update, select the path where Myportletproj.war is located, click Next;

Portlet Publishing and Configuration

Figure 26. Update Portlet Project

Can't you see the picture clearly? Please click here to view the original (larger image).

Click Finish directly.

In the portal console, click Portal User interface-> Manage pages->content root->home, click New Page, enter Themepage in Title, click OK.

Figure 27. New Themepage

Can't you see the picture clearly? Please click here to view the original (larger image).

On the pages in Home page, click Themepage on the right, click the Add Portlet, select Themeportlet, and click Done. :

32 Figure 28. Modify the Themepage page layout

Can't you see the picture clearly? Please click here to view the original (larger image).

At this point, our code writing and the Portlet configuration are complete. You will need to restart the Portal Server before verifying the results.

Validation results

After restarting the portal server, go to the Portal's Admin console and we'll be able to see the sernderpage,receiverpage,themepage we just created and the "Say Hello" hyperlink we added in theme. As shown in the following:

Figure 29. New pages and modified Theme

Can't you see the picture clearly? Please click here to view the original (larger image).

First click on the Say Hello hyperlink, you can jump to the page to Themepage, while in Themeportlet received from the theme parameters.

Figure 30. Verifying parameter passing between the Theme and the Portlet

A detailed description of the Portlet communication process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.