allow for writing the response directly in a Portlet @ExceptionHandler method (like in the Servlet equivalent)
This commit is contained in:
parent
66b4499973
commit
45d9b0cb2d
|
|
@ -74,16 +74,18 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a custom ArgumentResolvers to use for special method parameter types. Such a custom ArgumentResolver will kick
|
* Set a custom ArgumentResolvers to use for special method parameter types.
|
||||||
* in first, having a chance to resolve an argument value before the standard argument handling kicks in.
|
* <p>Such a custom ArgumentResolver will kick in first, having a chance to resolve
|
||||||
|
* an argument value before the standard argument handling kicks in.
|
||||||
*/
|
*/
|
||||||
public void setCustomArgumentResolver(WebArgumentResolver argumentResolver) {
|
public void setCustomArgumentResolver(WebArgumentResolver argumentResolver) {
|
||||||
this.customArgumentResolvers = new WebArgumentResolver[]{argumentResolver};
|
this.customArgumentResolvers = new WebArgumentResolver[]{argumentResolver};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set one or more custom ArgumentResolvers to use for special method parameter types. Any such custom ArgumentResolver
|
* Set one or more custom ArgumentResolvers to use for special method parameter types.
|
||||||
* will kick in first, having a chance to resolve an argument value before the standard argument handling kicks in.
|
* <p>Any such custom ArgumentResolver will kick in first, having a chance to resolve
|
||||||
|
* an argument value before the standard argument handling kicks in.
|
||||||
*/
|
*/
|
||||||
public void setCustomArgumentResolvers(WebArgumentResolver[] argumentResolvers) {
|
public void setCustomArgumentResolvers(WebArgumentResolver[] argumentResolvers) {
|
||||||
this.customArgumentResolvers = argumentResolvers;
|
this.customArgumentResolvers = argumentResolvers;
|
||||||
|
|
@ -366,7 +368,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
|
||||||
return new ModelAndView((String) returnValue);
|
return new ModelAndView((String) returnValue);
|
||||||
}
|
}
|
||||||
else if (returnValue == null) {
|
else if (returnValue == null) {
|
||||||
return null;
|
return new ModelAndView();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
|
throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,6 @@ import org.springframework.web.context.request.WebRequest;
|
||||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||||
import org.springframework.web.portlet.DispatcherPortlet;
|
import org.springframework.web.portlet.DispatcherPortlet;
|
||||||
import org.springframework.web.portlet.ModelAndView;
|
import org.springframework.web.portlet.ModelAndView;
|
||||||
import org.springframework.web.portlet.bind.MissingPortletRequestParameterException;
|
|
||||||
import org.springframework.web.portlet.bind.annotation.ActionMapping;
|
import org.springframework.web.portlet.bind.annotation.ActionMapping;
|
||||||
import org.springframework.web.portlet.bind.annotation.EventMapping;
|
import org.springframework.web.portlet.bind.annotation.EventMapping;
|
||||||
import org.springframework.web.portlet.bind.annotation.RenderMapping;
|
import org.springframework.web.portlet.bind.annotation.RenderMapping;
|
||||||
|
|
@ -519,6 +518,17 @@ public class Portlet20AnnotationControllerTests {
|
||||||
portlet.render(request, response);
|
portlet.render(request, response);
|
||||||
assertEquals("myLargeView-value2", response.getContentAsString());
|
assertEquals("myLargeView-value2", response.getContentAsString());
|
||||||
|
|
||||||
|
actionRequest = new MockActionRequest("error");
|
||||||
|
actionResponse = new MockActionResponse();
|
||||||
|
portlet.processAction(actionRequest, actionResponse);
|
||||||
|
|
||||||
|
request = new MockRenderRequest(PortletMode.VIEW, WindowState.MAXIMIZED);
|
||||||
|
request.setParameters(actionResponse.getRenderParameterMap());
|
||||||
|
request.setSession(actionRequest.getPortletSession());
|
||||||
|
response = new MockRenderResponse();
|
||||||
|
portlet.render(request, response);
|
||||||
|
assertEquals("XXX", response.getContentAsString());
|
||||||
|
|
||||||
MockEventRequest eventRequest = new MockEventRequest(new MockEvent("event1"));
|
MockEventRequest eventRequest = new MockEventRequest(new MockEvent("event1"));
|
||||||
MockEventResponse eventResponse = new MockEventResponse();
|
MockEventResponse eventResponse = new MockEventResponse();
|
||||||
portlet.processEvent(eventRequest, eventResponse);
|
portlet.processEvent(eventRequest, eventResponse);
|
||||||
|
|
@ -1029,6 +1039,11 @@ public class Portlet20AnnotationControllerTests {
|
||||||
response.setRenderParameter("test", "value2");
|
response.setRenderParameter("test", "value2");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ActionMapping("error")
|
||||||
|
public void myError(StateAwareResponse response) {
|
||||||
|
throw new IllegalStateException("XXX");
|
||||||
|
}
|
||||||
|
|
||||||
@EventMapping("event1")
|
@EventMapping("event1")
|
||||||
public void myHandle(EventResponse response) throws IOException {
|
public void myHandle(EventResponse response) throws IOException {
|
||||||
response.setRenderParameter("test", "value3");
|
response.setRenderParameter("test", "value3");
|
||||||
|
|
@ -1049,6 +1064,11 @@ public class Portlet20AnnotationControllerTests {
|
||||||
writer.write("myView");
|
writer.write("myView");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
public void handleException(Exception ex, Writer writer) throws IOException {
|
||||||
|
writer.write(ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
@ResourceMapping("resource1")
|
@ResourceMapping("resource1")
|
||||||
public void myResource(Writer writer) throws IOException {
|
public void myResource(Writer writer) throws IOException {
|
||||||
writer.write("myResource");
|
writer.write("myResource");
|
||||||
|
|
|
||||||
|
|
@ -114,6 +114,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
||||||
import org.springframework.web.bind.support.WebArgumentResolver;
|
import org.springframework.web.bind.support.WebArgumentResolver;
|
||||||
import org.springframework.web.bind.support.WebBindingInitializer;
|
import org.springframework.web.bind.support.WebBindingInitializer;
|
||||||
|
|
@ -1769,13 +1770,18 @@ public class ServletAnnotationControllerTests {
|
||||||
|
|
||||||
@RequestMapping("")
|
@RequestMapping("")
|
||||||
public void myPath2(HttpServletResponse response) throws IOException {
|
public void myPath2(HttpServletResponse response) throws IOException {
|
||||||
response.getWriter().write("test");
|
throw new IllegalStateException("test");
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/bar")
|
@RequestMapping("/bar")
|
||||||
public void myPath3(HttpServletResponse response) throws IOException {
|
public void myPath3(HttpServletResponse response) throws IOException {
|
||||||
response.getWriter().write("testX");
|
response.getWriter().write("testX");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
public void myPath2(Exception ex, HttpServletResponse response) throws IOException {
|
||||||
|
response.getWriter().write(ex.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue