将对象列表从动作传递到渲染阶段 [英] Passing object list from the action to the render phase
问题描述
我正在使用Spring MVC for portlet,但发现了一个问题.
I'm working with spring MVC for portlets, and I found a problem.
我需要将对象列表从操作阶段传递到渲染阶段.我尝试使用setRenderParameter,如下所示:
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());
这里有两种方法:
@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;
}
但是这不起作用,因为在渲染阶段,它无法将String转换为我的Object List.我该怎么办..?
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..?
起初,我在类级别使用私有List mylist,但据我所知,控制器是单例模式,因此我们不能使用这种方法.
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.
推荐答案
将ActionRequest请求对象添加到您的方法签名中,如下所示,并将对象添加为属性
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
}
}
另一个重要的事情是在 portlet.xml 中添加以下配置标签,因此无需获取并再次放入render方法,您的请求属性就可以在 JSP 上使用. /p>
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.
这篇关于将对象列表从动作传递到渲染阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!