1 16 17 package org.springframework.web.portlet.mvc; 18 19 import javax.portlet.ActionRequest; 20 import javax.portlet.ActionResponse; 21 import javax.portlet.RenderRequest; 22 import javax.portlet.RenderResponse; 23 24 import org.springframework.validation.BindException; 25 import org.springframework.web.portlet.ModelAndView; 26 import org.springframework.web.portlet.bind.PortletRequestDataBinder; 27 28 52 public abstract class AbstractCommandController extends BaseCommandController { 53 54 58 private static final String COMMAND_IN_SESSION_PARAMETER = "command-in-session"; 59 60 private static final String TRUE = Boolean.TRUE.toString(); 61 62 63 66 public AbstractCommandController() { 67 } 68 69 73 public AbstractCommandController(Class commandClass) { 74 setCommandClass(commandClass); 75 } 76 77 82 public AbstractCommandController(Class commandClass, String commandName) { 83 setCommandClass(commandClass); 84 setCommandName(commandName); 85 } 86 87 88 protected final void handleActionRequestInternal(ActionRequest request, ActionResponse response) 89 throws Exception { 90 91 Object command = getCommand(request); 93 94 PortletRequestDataBinder binder = bindAndValidate(request, command); 96 BindException errors = new BindException(binder.getBindingResult()); 97 98 handleAction(request, response, command, errors); 100 101 setRenderCommandAndErrors(request, command, errors); 103 setCommandInSession(response); 104 } 105 106 protected final ModelAndView handleRenderRequestInternal( 107 RenderRequest request, RenderResponse response) throws Exception { 108 109 Object command = null; 110 BindException errors = null; 111 112 if (isCommandInSession(request)) { 114 logger.debug("Render phase obtaining command and errors objects from session"); 115 command = getRenderCommand(request); 116 errors = getRenderErrors(request); 117 } 118 else { 119 logger.debug("Render phase creating new command and errors objects"); 120 } 121 122 if (command == null) { 124 command = getCommand(request); 125 } 126 127 if (errors == null) { 129 PortletRequestDataBinder binder = bindAndValidate(request, command); 130 errors = new BindException(binder.getBindingResult()); 131 } 132 133 return handleRender(request, response, command, errors); 134 } 135 136 137 150 protected abstract void handleAction( 151 ActionRequest request, ActionResponse response, Object command, BindException errors) 152 throws Exception ; 153 154 168 protected abstract ModelAndView handleRender( 169 RenderRequest request, RenderResponse response, Object command, BindException errors) 170 throws Exception ; 171 172 173 179 protected String getCommandInSessionParameterName() { 180 return COMMAND_IN_SESSION_PARAMETER; 181 } 182 183 190 protected final void setCommandInSession(ActionResponse response) { 191 if (logger.isDebugEnabled()) { 192 logger.debug("Setting render parameter [" + getCommandInSessionParameterName() + 193 "] to indicate a valid command (and errors) object are in the session"); 194 } 195 try { 196 response.setRenderParameter(getCommandInSessionParameterName(), TRUE); 197 } 198 catch (IllegalStateException ex) { 199 } 201 } 202 203 211 protected final boolean isCommandInSession(RenderRequest request) { 212 return TRUE.equals(request.getParameter(getCommandInSessionParameterName())); 213 } 214 215 } 216 | Popular Tags |