Search code examples
javaspringportlet

Passing object list from the action to the render phase


I'm working with spring MVC for portlets, and I found a problem.

I need to pass an Object List from the action phase to the render phase. I've tried to use the setRenderParameter, something like this:

actionresponse.setRenderParameter(String string, String[] strings);
actionresponse.setRenderParameter("myList",myList.toString());

Here we have the two methods:

@RequestMapping(params = ACTION_MYACTION)
public final void doAction(MyBean search, Errors errors, ActionRequest actionrequest, ActionResponse actionresponse) {
    String processName = UtilLog.getProcessName(CLASS_NAME, "doAction");
    successMessage.clear();
    justlist = null;

    validateBean(consulta, errors);

    if (!errors.hasErrors()) {
        try {

            mylist = myBpelImpl.getList(search);
    actionresponse.setRenderParameter("myList",myList.toString());

        } catch (Exception ex) {
            LOG.error(processName, ex);
            processError(actionrequest, null, ex);
        }
    }

    informSuccessMessage(actionrequest, errors, status);

}

@RequestMapping(params = ACTION_MYACTION)
public final String doRender(@ModelAttribute(value = "myBean") MyBean search, Errors errors, RenderRequest renderrequest) {

List<otherBean> mylist =   renderrequest.getParameter("myList");

    renderrequest.setAttribute(ServletContextKeys.SC_JUSTIFICANTE_LIST, myList);

    return ServletContextKeys.SC_CONSULTA_JUSTIFICANTES;

}

But this is not working, because in the render phase, it can't convert the String to my Object List. How could I do this..?

At first, I was using a private List mylist at class level, but as far as I know, a controller is a singleton pattern, so we can't use this approach.


Solution

  • Add ActionRequest request object into your method signature like as following and add objects as attribute

    @ActionMapping(params = "doAction=searchDeviceResults")
    public void searchResults(@ModelAttribute(value = "searchForm") SearchForm searchForm,
                              BindingResult bindingResult, 
                              ActionRequest request, 
                              ActionResponse response, 
                              SessionStatus sessionStatus) {
    
        searchFormValidator.validate(searchForm, bindingResult);
    
        if (!bindingResult.hasErrors()) {
            response.setRenderParameter("doAction", "showDeviceResults");
            sessionStatus.setComplete();    
            List<AccountDetail> accList = accountService.getAccountDetail(adp);
            request.setAttribute("accountList", accList); // here we go
        }
    
    }
    

    Another important thing is to add below config tags in portlet.xml so without getting and putting again into render method your request attribute will be available on the JSP.

    <container-runtime-option>
        <name>javax.portlet.actionScopedRequestAttributes</name>
        <value>true</value>
    </container-runtime-option>
    <container-runtime-option>
        <name>javax.portlet.renderHeaders</name>
        <value>true</value>
    </container-runtime-option>
    <container-runtime-option>
        <name>javax.portlet.escapeXml</name>
        <value>false</value>
    </container-runtime-option>
    

    Let me know if any issue occurs.