In ActionCommand derived class to get ActionRequest, ActionResponse

来源:互联网 发布:小鹰电视直播软件 编辑:程序博客网 时间:2024/04/27 23:05

When I refactor my controller, I separate CRUD in different classes by implementing ActionCommand, we need to do some preparations:

1. To take advantage of ActionCommands, add the following initialization parameter to our portlet.xml file:
<init-param>
<name>action.package.prefix</name>
<value>com.book.action</value>
</init-param>

2. create class, the class's name must end with ActionCommand, for example: UpdateBookActionCommand:

public class UpdateBookActionCommand implements ActionCommand {
BookManager bm = BookManager.getBookManager();
@Override
public boolean processCommand(PortletRequest actionRequest, PortletResponse portletResponse)
throws PortletException {
String id = actionRequest.getParameter("bookId");
Book book = bm.getBook(Integer.parseInt(id));
actionRequest.setAttribute("book", book);
ActionResponse actionResponse = (ActionResponse) portletResponse;
actionResponse.setRenderParameter("jspPage", "/update.jsp");
return true;
}
}

In the example, I need to redirect to update.jsp. Previously, the parameters in method are ActionRequest and ActionResponse, but in processCommand method, we just get PortletRequest, PortletResponse.

In my case, there's no setRenderParameter(String,String) method in PortletResponse. What I do is casting portletResponse to ActionResponse. It works well.

The difference between them are:

The PortletRequest is the parent of both. An ActionRequest and a RenderRequest are both different types of PortletRequest objects.

An ActionRequest is valid during the action processing phase of the portlet. During this phase, the portlet hasn't completely decided how it is going to render itself, be it minimized, maximized, in edit mode or in veiw mode, etc.

On the other hand, the RenderRequest is valid during the rendering phase of the portlet. At this point, the portlet knows how it is going to render itself, and certain changes such as window state, are not allowed.


原创粉丝点击