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.
The following are required for this recipe:
HelloWorldPortlet
project from the Creating a portlet with the Portlet 2.0 Specification recipePassing form parameters and acting on that input involves the following:
welcome.jsp
in the src/main/webapp
folder of the project.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/>
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); }
HelloWorldPortlet
that has the following code:@ProcessAction(name = "nameAction") public void nameAction(ActionRequest request, ActionResponse response) throws PortletException { response.setRenderParameter("yourname", request.getParameter("yourname")); }
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/>
> mvn clean package
chapter6-1.0.0.SNAPSHOT.war
, from the target
folder into the deployment folder where you unpacked the GateIn installation.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.
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.
Next we'll describe some additional enhancements such as using render parameters and resetting the form content.
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
.
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:
3.137.223.10