VicMo
VicMo

Reputation: 47

Display Arraylist on jsp page

I am trying to display the following ArrayList in the .jsp page shown but I can't seem to see any values once I run my portlet, where is the problem?

code.java

 public class TestPortlet extends MVCPortlet {
    public void displayProcess(ActionRequest request, ActionResponse response) {
        ArrayList<String> process = new ArrayList<>();
        process.add("a");
        process.add("b");
        process.add("c");
        process.add("d");
        process.add("e");

        request.setAttribute("processName", process);
    }
}

The jsp page is as shown:

<%@ include file="/init.jsp"%>

<jsp:useBean id="processName" class="java.util.ArrayList" scope="request" />

<aui:select id="process" name="processitems">
    <c:forEach items="${processName}" var="process">
        <aui:option value="${process}">
            ${process}
        </aui:option>
    </c:forEach>
</aui:select>

Any help would be much appreciated.

Upvotes: 0

Views: 565

Answers (2)

VicMo
VicMo

Reputation: 47

I found the solution:

I was to use:

public void displayProcess(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {

instead of

 public void displayProcess(ActionRequest request, ActionResponse response) {

and at the bottom of my method the following:

renderRequest.setAttribute("process", process); 
    super.render(renderRequest, renderResponse);

On the jsp page, at the topmost point; receive the ArrayList that you are passing as:

<% ArrayList<String> process = (ArrayList) request.getAttribute("process"); %>

.

The results are as follows:

ArrayList on jsp, liferay

Upvotes: 0

Olaf Kock
Olaf Kock

Reputation: 48057

Not sure if this is a complete answer, but some steps to figure out what the problem is. I'm taking your question as well as some comments:

You're implementing a portlet action handler, from there, you won't forward/dispatch to a particular jsp: A portlet's ACTION phase is only good for changing the state, while displaying the result is part of the VIEW phase. The code that you've posted (though it's obviously simplified) looks like it rather wants to live in doView().

In fact, that may be all you need: If you just display the portlet, only the VIEW phase will be triggered. Just displaying the portlet will not trigger the action handler, which you can validate in a debugger.

For the JSP: All you say is you "can't seem to see" any of the results. Validate where your problem is: Is the list there? Does enumerating the list work? Is your problem with the AUI taglib? You can easily check this by removing all of the other tags and rather generate pure output by removing a bit.

When you look at the output's source and any of the JSP stuff survives (e.g. the ${processName}, or <aui:select..>, <c:forEach...>, then you'll know that this is the root cause for "not seeing" anything. You might miss a taglib or other.

Last: I've never tried this, but <aui:select> is a tag that's meant to be used within a form, and I'm not sure what it does outside of a form - you may want to surround it with <aui:form ....> and see what happens then.

Upvotes: 1

Related Questions