In this step, you will use Page Flows to create one portlet that listens to another portlet. This particular example shows two ways of passing information from one portlet to the next: using a Form or using the Request Parameter. They both use the Page Flow as the means of connecting two portlets. By configuring the portlets to listen to one another, forms and requests instantiated by a JSP in one Page Flow can be sent to both Page Flows. This allows the listening portlet to update itself.
The tasks in this step are:
Note: Case sensitivity in naming directories and files in this step is important, especially if you are copying and pasting the code samples from this step.
In this task, two Page Flows are created within the portlets directory; one called j1, the other called j2.
NOTE: WebLogic Workshop automatically creates the Page Flow controller class, prepending the name of the Page Flow to the Controller.jpf filename.
This example requires modifying the Page Flows so that they can be notified by the listening portlet. The j1 Page Flow will receive form data from its simpleForm.jsp. The j2 Page Flow (in a listening portlet) will also receive notifications, and store the data, so its portlet can retrieve and display it.
Note: Use Flow View to set up actions, then touch up the code using the Source View.
In the following steps, you will add a reference to a JSP that does not yet exist. You will create this JSP later in this topic.
package portlets.j1;
/** * @jpf:action * @jpf:forward name="simpleForm" path="simpleForm.jsp" */ protected Forward begin() { return new Forward( "simpleForm" ); }
/** * @jpf:action * @jpf:forward name="simpleForm" path="simpleForm.jsp" */ protected Forward passString1( Form form ) { String passedText = form.getText(); return new Forward( "simpleForm" ); } /** * @jpf:action * @jpf:forward name="simpleForm" path="simpleForm.jsp" */ protected Forward passString2() { return new Forward( "simpleForm" ); } /** * @jpf:action * @jpf:forward name="simpleForm" path="simpleForm.jsp" */ protected Forward passString3() { return new Forward( "simpleForm" ); }
public static class Form extends FormData { private String text; public void setText( String text ) { this.text = text; } public String getText() { return this.text; } }
In the following steps, you will add a reference to a JSP that does not yet exist. You will create this JSP later in this topic.
package portlets.j2;
public String thePassedText = "";
/** * @jpf:action * @jpf:forward name="listening" path="listening.jsp" */ public Forward begin() { return new Forward( "listening" ); }
/** * @jpf:action * @jpf:forward name="listening" path="listening.jsp" */ public Forward passString1(portlets.j1.J1Controller.Form form) { thePassedText = form.getText(); return new Forward( "listening" ); }
/** * @jpf:action * @jpf:forward name="listening" path="listening.jsp" */ public Forward passString2() { thePassedText = getRequest().getParameter("string2"); return new Forward( "listening" ); } /** * @jpf:action * @jpf:forward name="listening" path="listening.jsp" */ public Forward passString3() { HttpServletRequest request = getRequest(); String attribValue = request.getParameter("string3"); request.setAttribute("string3", attribValue); return new Forward( "listening" ); }
Create Additional JSPs
In this step, we'll create the "From" JSP for the j1 portlet, and the "To" JSP used by the j2 portlet.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="netui-tags-html.tld" prefix="netui" %> <h3>"To" JSP</h3> Get text from jpf form = <b><netui:label value="{pageFlow.thePassedText}"/></b> <br/> <br/> Using {request.string3} databinding = <b> <netui:label value="{request.string3}"/> </b> <br/> <% String attribName = "string3"; %> Using request.getAttribute() = <%=request.getAttribute(attribName)%> <br/> <br/>
<netui:form action="none"> <netui:textBox></netui:textBox> <netui:button>Submit</netui:button> </netui:form>
<netui:form action="passString1"> <netui:textBox dataSource="text"/> <netui:button>Submit</netui:button> </netui:form>
Note: You can click in each tag and type the action and dataSource attribute values in the Property Editor window.
<netui:anchor action="passString2">Pass String 2 <netui:parameter name="string2" value="STRING 2"/> </netui:anchor>
<netui:anchor action="passString3">Pass String 3 <netui:parameter name="string3" value="STRING 3"/> </netui:anchor>
When a portlet is placed inside a portal, it is assigned an instanceLabel which the framework uses to keep track of individual portlet placement. In order to make this sample work, the listenTo attribute on portlet j2 needs to be set to the instanceLabel of the j1 portlet on your portal.