Passing object list from the action to the render phase

一曲冷凌霜 提交于 2019-12-22 10:54:16

问题


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.


回答1:


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.




回答2:


You could try to use JSR 286 Action-scoped Request Attributes. See http://www.theserverside.com/news/1363818/JSR-286-Portlets-Action-scoped-Request-Attributes.

Also, it might have been a typo but annotate your doAction with @ActionMapping(params = ACTION_MYACTION)




回答3:


I've found the answer!

I need to use

PortletUtils.setSessionAttribute(actionrequest, "mylist", mylist);

instead of the actionresponse.setRenderParameter.

It works!



来源:https://stackoverflow.com/questions/13416157/passing-object-list-from-the-action-to-the-render-phase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!