3
votes

I have made a portlet in Liferay which displays a form. I would like to process the form data after submit, but the data "disappears" somewhere.

This is my form code in jsp:

<portlet:actionURL windowState="normal" var="filterURL">
</portlet:actionURL>
    <form action="<portlet:actionURL />" method="post">
    Industry: <input type="text" name="<portlet:namespace />industry" value="<%= industryFilter %>"/>
    <input type="submit" value="Filter" />
</form>

The data should be passed to my portlet class, but it won't. This is my class code:

public void processAction(
        ActionRequest actionRequest, ActionResponse actionResponse)
    throws IOException, PortletException {

    String industryFilter = actionRequest.getParameter("industry");
    if(industryFilter == null) {
        industryFilter = "no-param";
    }
    actionResponse.setRenderParameter("industry", industryFilter);

    super.processAction(actionRequest, actionResponse);
}

This class is intended to pass the data back to the jsp. The setRenderParameter() method works fine, as in jsp I can read the value using request.getParameter("industry"); However, it returns always "no-param", which means, the actionRequest.getParameter() return null.

So, it seems my processAction methods gets properly called, but it does not receive the form data. Where is the error, what did I wrong?

Update:

I downloaded the portlet to my local machine, deployed to a local demo Liferay installation, and it worked! So the code should be ok, it must be some server setting/problem. One difference I realized that in the network tab of the Chrome developer tools, the local server has only one POST call with status 200 while on the remote server there is a POST with 302 Moved Temporarily and a GET 200 with the same URL! Could it cause the problem?

5
works for me. Do you have <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> on your jsp as well? Check if the HTML contains fragments of the <portlet:*> tags. The namespacing should be resolved automatically...Olaf Kock
Yes, I have the taglib, the HTML is properly resolved. I also tried aui:form and aui:input and ParamUtil as suggested below, but without success. actionRequest.getParameterNames() return an empty set, is this which should store the form values?ttamas
Could it be that you're working on a different portlet than you expect on that remote server? Add some log output, redeploy and check again if the expected log output is coming up. If it doesn't, I'm afraid you'll have to dig deeper into server configuration etc. Are your remote and local Liferay running on the same appserver?Olaf Kock
The other parts of the portlet are working as expected (it lists useres, and I would like to use the form as a filter to narrow the list). Both instances use Tomcat, however, the server runs Linux while my local machine is Windows. I copied the server's portal-ext.properties but it made no difference.ttamas
Please refer to issues.liferay.com/browse/LPS-44604 . (This is already cross-linked from there. Will try to help drive-forward a resolution on the underlying issue from there.)ziesemer

5 Answers

6
votes

You should either put a namespace prefix to your name value like this:

<input type="text" name="<portlet:namespace />inputTextName" />

or you can set the requires-namespaced-parameters setting to false in your liferay-portlet.xml.

This is a change since Liferay 6.2: https://github.com/liferay/liferay-aui-upgrade-tool/issues/30

1
votes

It is always better to use liferay's utility methods where ever possible, like:

String industryFilter = ParamUtil.getString(actionRequest, "industry");

This way you don't have to worry about prepending namespace and moreover ParamUtil class comes with a number of other helpful methods like getLong, getLongValues (which return an array of Longs instead of using request.getParameterValues and then converting to Long), getInteger, getIntegerValues, ... etc.

Also it would be helpful to use <aui:input /> tags in the JSP as liferay does:

<aui:input type="text" name="industry" value="<%= industryFilter %>" />

since that would take care of the namespace in the name attribute of the generated html <input /> tag.

But if you don't want to use any of the above than the solution provided by @NickRoth should work without any issues:

String industryFilter = actionRequest.getParameter(actionResponse.getNamespace()+"industry");
1
votes

The name of your form element is "<portlet:namespace />industry", not just "industry". Try this line of code instead.

String industryFilter = 
    actionRequest.getParameter(actionResponse.getNamespace()+"industry");

EDIT: I wasn't aware that the portlet namespace was stripped from parameters as they come in. I assume this must be a Liferay thing. I'll offer some other options in that case.

If you're still having trouble getting the parameter off the request it may worth doing some debugging. You can use actionRequest.getParameterMap() to get all the parameter and see if some odd naming is happening.

Also, consider if you have any filters in between the browser and your portlet that might be manipulating the request. For me, we've had to implement a portlet filter that stripped any parameter that matched a regex for SQL injection attacks and that caused us some problems later that weren't a bit troubling to debug.

1
votes

OK, I have finally find the solution myself. At some point, I added the portlet to another page, and surprisingly, it worked! So I deleted the original page and re-generated step by step, setting permissions etc. I realized that the problem was the name (friendly URL) of the page! It was set to members which seems to caused the problem. I changed it to alumni-members at it works now.

0
votes

This will give you parameters <input type="text" name="<portlet:namespace />inputTextName" />

This will not <aui:input type="text" name="<portlet:namespace />inputTextName" />

This will <aui:input type="text" name="inputTextName" />