Foros de discusión

How does ActionCommand and JSP dispatch work together ?

thumbnail
Charles de Courval, modificado hace 13 años.

How does ActionCommand and JSP dispatch work together ?

Junior Member Mensajes: 55 Fecha de incorporación: 31/07/10 Mensajes recientes
Hi,
In the latest version of Liferay In Action, there are a few pages on ActionCommand. A great way of grouping the actions in the same package. But there is no mention on how to redirect the user to a specific JSP once the action is done.

For example, I have a list project, project detail, a project has many bid. So from the list, I can add/edit a project and in the add/edit project page I can add/edit a number of bid.

All of this is managed by one portlet.

From what I could gather, my project Portlet would have to extend the MVCPortlet and in the render method, do the dispatching on a command. Is there an automatic workflow that would do the dispatching form me, an actionCommandPortlet that I could use ? Is the gain in using ActionCommand only to reduce the size of my portlet's implementation by removing all the action methods ?

Further more, the processCommand() method in ActionCommand returns a boolean. Why wouldn't it return a String that would be the path of the JSP. What was the purpose of the boolean ?

Thanks to all for any input on this.
cheers.
thumbnail
Luis Antonio Rodríguez González, modificado hace 11 años.

RE: How does ActionCommand and JSP dispatch work together ?

Junior Member Mensajes: 41 Fecha de incorporación: 5/06/12 Mensajes recientes
Hi,

I send you a solution, I hope It is useful for you.

public class MyActionCommand implements ActionCommand {
    protected String okJSP = "/html/ok.jsp";
    protected String errorJSP = "/html/error.jsp";

	@Override
	public boolean processCommand(PortletRequest request, PortletResponse response)
			throws PortletException
	{
		boolean valid = true;
        	ArrayList<string> errors = new ArrayList<string>();
		
		valid = MyValidator.validateParameters(request, errors);
		
		if (valid)
		{
			request.setAttribute("jspPage", okJSP);
		}
		else
		{
			for (String error : errors) {
	                        SessionErrors.add(request, error);
                        }
			request.setAttribute("jspPage", errorJSP);
		}
		
		return valid;
	}
}

public class BuscarAmigosPortlet extends MVCPortlet
{
	/* (non-Javadoc)
	 * @see com.liferay.util.bridges.mvc.MVCPortlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
	 */
	@Override
	public void processAction(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		super.processAction(actionRequest, actionResponse);
		String jspPage = (String)actionRequest.getAttribute("jspPage");
		actionRequest.removeAttribute("jspPage");
		actionResponse.setRenderParameter("jspPage", jspPage);		
	}
}
</string></string>


Best regards,

Luis