2
votes

I have a portlet on Liferay portal where I invoke action from a .jsp page. I would like to pass a String array to another .jsp page where this array will be displayed. However, no values are being passed at all.

I am able to pass some String values using String something = (String)prefs.getValue("something", "something"); but this doesnt work for arrays.

This is my view.jsp from which I invoke the actionRequest (I will only show parts of the code, it would be too long otherwise):

<portlet:actionURL var="loadMessages" name="loadMessages">
    <portlet:param name="jspPage" value="/view.jsp" />
</portlet:actionURL>

This is the loadMessages() function in my Java class:

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

        manager.loadFromDB();   
        ArrayList<Message> messagesObject = manager.getMessages();

        // we must save our messages as strings     
        String[] messages = new String[messagesObject.size()];

        for (int i=0; i<messagesObject.size(); i++) {
            String msg = "";
            Message message = messagesObject.get(i);

            msg += message.getMsgid() + "\n"; 
            msg += message.getSender() + "\n"; 
            msg += message.getReceiver() + "\n"; 

            messages[i] = msg;
        }

        // save
        if (messages != null) {
            actionRequest.setAttribute("messages", messages);   
            System.out.println(messages.length + " messages loaded!");
        }

    }

This works fine so far: I am getting "x messages loaded!" messages.

The problem comes when I want to access this array in my display.jsp file:

<%
String[] messages = (String[])renderRequest.getAttribute("messages");

if (messages == null) {
    System.out.println("NULL MESSAGES");
} else {
    System.out.println(messages[0]);
}
%>

I am getting, that my array is NULL here? What changes do I have to take to access the array I saved in my actionRequest phase?

Second question: Is it possible to pass Java object to .jsp page? I am guessing that this works only for Strings but it would be definitely cool to work with objects!

Thanks for any answer!

3

3 Answers

4
votes

You can set whole List of message as attribute.

i.e

 actionRequest.setAttribute("messages", messagesObject);

You can render to specfic jsp using

i.e

res.setRenderParameter("mvcPath", "jspPath");

or

res.setRenderParameter("jspPage", "jspPath"); (this one is deprecated now)

In jsp part you can access this values using jstl c:foreach

i.e

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:forEach var="message" items="${messages}">
   <c:out value="${message.sender}"/>
</c:forEach>

Try this Let me know if u have any problem

0
votes

It is possible to put java object in the Portlet Session:

<% 
JavaObject javaObject = new JavaObject(); 
renderRequest.getPortletSession().setAttribute("javaObjectAttr", javaObject);
%>

Then you can get this object in another jsp page:

<%
JavaObject javaObject = (JavaObject)portletSession.getAttribute("javaObjectAttr");
pageContext.setAttribute("javaObject", javaObject);
%>

<c:out value="${javaObject.firstName}"/>

I don't know exactly is this a good way or not :)
Hope this helps.

0
votes

Give a look over this page. It is LifeRay documentation on passing information from the Action phase to the Render phase.

Essentially you can use actionResponse.setRenderParameter("key", data); to forward an object from your action method to the doView method. In your doView you can then get it with request.getParameter("key");

This is fairly standard for portlets and is, in my opinion, much preferred over using the session, despite what the link says. Using the session means you have to make sure you don't add too much to the session and cause a large memory overhead for your portlet container. For one user it may not be much memory but for a large, busy site it can add up quickly and bring your server to its knees or require you to need more hardware than your site really needs.

The article also mentions using an init param to automatically copy all action parameter over to the render action as another viable option.