Using an action to pass form parameters

This recipe will extend the portlet created in the previous recipe by adding a form for the user to enter their name, and then responding with a welcome message that includes their name.

Getting ready

The following are required for this recipe:

  • Apache Maven
  • An IDE of your choice
  • GateIn-3.2.0.Final
  • HelloWorldPortlet project from the Creating a portlet with the Portlet 2.0 Specification recipe

How to do it...

Passing form parameters and acting on that input involves the following:

  1. Create welcome.jsp in the src/main/webapp folder of the project.
  2. Add the following to welcome.jsp:
    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    
    <div class="portlet-section-header">Welcome to the HelloWorld portlet</div>
    <br/>
    <div class="portlet-section-body">
      <form action='<portlet:actionURL name="nameAction"/> method="post"'>
        <span class="portlet-form-label">Name:</span>
        <input class="portlet-form-input-field" type="text" name="yourname"/>
        <input class="portlet-form-button" type="Submit" value="Say Hello"/>
      </form>
    </div>
    <br/>
  3. Change the code of display() to the following:
        if (null == request.getParameter("yourname") || "".equals(request.getParameter("yourname"))) { getPortletContext().getRequestDispatcher("/welcome.jsp").include(request, response);
        } else 
        { getPortletContext().getRequestDispatcher("/helloWorld.jsp").include(request, response);
        }
  4. Create a new method in HelloWorldPortlet that has the following code:
    @ProcessAction(name = "nameAction")
    public void nameAction(ActionRequest request, ActionResponse response) throws PortletException {
        response.setRenderParameter("yourname", request.getParameter("yourname"));
    }
  5. Change the JSP code in helloWorld.jsp to the following:
    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    
    <portlet:defineObjects/>
    
    <div class="portlet-section-header">Hello!</div>
    <br/>
    <div class="portlet-section-body">Hello <%= renderRequest.getParameter("yourname") %> from your first portlet!</div>
    <br/>
  6. Run the following in the root of the project directory to build the web archive:
    > mvn clean package
    
  7. Copy the generated web archive, chapter6-1.0.0.SNAPSHOT.war, from the target folder into the deployment folder where you unpacked the GateIn installation.
  8. Start the server and access the portal page created in the Creating a portlet with the Portlet 2.0 Specification recipe.
  9. The portlet should now look like the following screenshot:
    How to do it...
  10. In the Name field, enter some text and click on Say Hello. An example result from the submission can be seen in the following screenshot:
    How to do it...

How it works...

In welcome.jsp we added the tag library from the Portlet 2.0 Specification so that we can use portlet:actionURL and portlet:defineObjects. A simple form was all that was needed to capture some text, in this case a name, that can be passed as a parameter to the nameAction method of the HelloWorldPortlet by using the portlet:actionURL tag.

In HelloWorldPortlet nameAction() was added to process the ActionRequest from the portlet container. It simply takes the request parameter containing the form parameter that was submitted, and adds it as a render parameter onto the response, which makes it available to the process of rendering a portlet.

The portlet:defineObjects tag implicitly adds request and response objects based on the portlet phase, portlet config, portlet session, and portlet preferences.

Note

Use of the render parameter in nameAction() is referred to as a private render parameter. A private render parameter is only available to the portlet that added it while rendering, in this case the HelloWorldPortlet. In Communicating between portlets using Public Render Parameters, render parameters that are available to all portlets while rendering will be covered.

The HelloWorldPortlet display() was altered to check for the presence of the form parameter on the request. Rendering of the portlet content is dispatched to the welcome.jsp page if the form parameter is not present, and to the helloWorld.jsp page if it was.

Finally, helloWorld.jsp was modified to retrieve the form parameter from the request and use it as part of a message to display.

There's more...

Next we'll describe some additional enhancements such as using render parameters and resetting the form content.

Passing parameters instead of rendering via an Action URL

In this recipe, we were capturing dynamic text for use in an Action URL. If the text was not dynamic, or did not require user input in a form, then the following code in welcome.jsp could have been used instead:

<a href="<portlet:renderURL><portlet:param name='yourname' value='John Doe'/></portlet:renderURL>">John Doe</a>

In the above approach there is no Action being processed, hence nameAction() would not be required on HelloWorldPortlet. The request parameter we need, yourname, is being directly passed to the RenderRequest, without the need for an intermediate ActionRequest.

Adding a link to reset user input

Once the User has entered some text and submitted the form, there is currently no way for them to return to the initial state of the portlet that shows the form again. In most situations that would be fine, but if it is necessary to return to a "blank slate", then it can be easily accomplished by adding the following link code into helloWorld.jsp in an appropriate location on the page:

<a href="<portlet:renderURL portletMode='view' />">Reset</a>

Using the renderURL portlet tag, we inform the portlet container that we want a URL created that will cause the portlet to be rendered with the portletMode of view, which will generate a URL that does not include the state generated as part of the form submit action. Clicking on the link will return the user to welcome.jsp and a blank form.

The following screenshot shows an example of what this could look like in the portlet:

Adding a link to reset user input

See also

  • The Creating a portlet with the Portlet 2.0 Specification recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.10