/** * Sets the {@linkplain #setOrder(int) order} to {@link #LOWEST_PRECEDENCE}. */ public DefaultHandlerExceptionResolver() { setOrder(Ordered.LOWEST_PRECEDENCE); setWarnLogCategory(getClass().getName()); }
/** * {@inheritDoc} * <p>Each value is formatted as a String before being added. */ @Override public void putAll(@Nullable Map<? extends String, ? extends Object> map) { if (map != null) { map.forEach((key, value) -> put(key, formatValue(value))); } }
/** * {@inheritDoc} * <p>Each attribute value is formatted as a String before being merged. */ @Override public RedirectAttributesModelMap mergeAttributes(@Nullable Map<String, ?> attributes) { if (attributes != null) { attributes.forEach((key, attribute) -> { if (!containsKey(key)) { addAttribute(key, attribute); } }); } return this; }
@PostMapping("/messages") public String sendMessage(TestBean testBean, BindingResult result, RedirectAttributes attributes) { if (result.hasErrors()) { return "messages/new"; } attributes.addAttribute("id", "1").addAttribute("name", "value"); attributes.addFlashAttribute("successMessage", "yay!"); return "redirect:/messages/{id}"; }
@RequestMapping(value="/persons", method=RequestMethod.POST) public String save(RedirectAttributes redirectAttrs) throws Exception { redirectAttrs.addFlashAttribute("one", "1"); redirectAttrs.addFlashAttribute("two", 2.222); redirectAttrs.addFlashAttribute("three", new URL("http://example.com")); return "redirect:/person/1"; } }
@Test public void addAttributeToString() { String attrName = "person"; RedirectAttributesModelMap model = new RedirectAttributesModelMap(); model.addAttribute(attrName, new TestBean("Fred")); assertEquals("toString() should have been used", "Fred", model.get(attrName)); }
@Override public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { exceptionResolvers.add(new DefaultHandlerExceptionResolver()); } });
@Test public void addAttributePrimitiveType() { this.redirectAttributes.addAttribute("speed", 65); assertEquals("65", this.redirectAttributes.get("speed")); }
@Test public void addAttributeCustomType() { String attrName = "person"; this.redirectAttributes.addAttribute(attrName, new TestBean("Fred")); assertEquals("ConversionService should have invoked toString()", "Fred", this.redirectAttributes.get(attrName)); this.conversionService.addConverter(new TestBeanConverter()); this.redirectAttributes.addAttribute(attrName, new TestBean("Fred")); assertEquals("Type converter should have been used", "[Fred]", this.redirectAttributes.get(attrName)); }
@Before public void setup() { exceptionResolver.setWarnLogCategory(exceptionResolver.getClass().getName()); }
@Before public void setup() { this.conversionService = new DefaultFormattingConversionService(); DataBinder dataBinder = new DataBinder(null); dataBinder.setConversionService(conversionService); this.redirectAttributes = new RedirectAttributesModelMap(dataBinder); }
/** * {@inheritDoc} * <p>Formats the attribute value as a String before adding it. */ @Override public RedirectAttributesModelMap addAttribute(String attributeName, @Nullable Object attributeValue) { super.addAttribute(attributeName, formatValue(attributeValue)); return this; }
/** * Handle the case when a {@link org.springframework.web.bind.WebDataBinder} conversion cannot occur. * <p>The default implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}. * Alternatively, a fallback view could be chosen, or the ConversionNotSupportedException could be * rethrown as-is. * @param ex the ConversionNotSupportedException to be handled * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler * @return an empty ModelAndView indicating the exception was handled * @throws IOException potentially thrown from {@link HttpServletResponse#sendError} */ protected ModelAndView handleConversionNotSupported(ConversionNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { sendServerError(ex, request, response); return new ModelAndView(); }
@PostMapping(path="/persons") public String save(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) { if (errors.hasErrors()) { return "person/add"; } redirectAttrs.addAttribute("id", "1"); redirectAttrs.addFlashAttribute("message", "success!"); return "redirect:/person/{id}"; }
@ExceptionHandler public String handleException(Exception ex, RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("exceptionClassName", ClassUtils.getShortName(ex.getClass())); return "redirect:/"; } }
@Test public void addAttributeValue() { this.redirectAttributes.addAttribute(new TestBean("Fred")); assertEquals("Fred", this.redirectAttributes.get("testBean")); }
/** * {@inheritDoc} * <p>The value is formatted as a String before being added. */ @Override public Object put(String key, @Nullable Object value) { return super.put(key, formatValue(value)); }
/** * Handle the case where a * {@linkplain org.springframework.http.converter.HttpMessageConverter message converter} * cannot write to a HTTP request. * <p>The default implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}. * Alternatively, a fallback view could be chosen, or the HttpMessageNotWritableException could * be rethrown as-is. * @param ex the HttpMessageNotWritableException to be handled * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler * @return an empty ModelAndView indicating the exception was handled * @throws IOException potentially thrown from {@link HttpServletResponse#sendError} */ protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { sendServerError(ex, request, response); return new ModelAndView(); }
@PostMapping public String save(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) { if (errors.hasErrors()) { return "persons/add"; } redirectAttrs.addAttribute("name", "Joe"); redirectAttrs.addFlashAttribute("message", "success!"); return "redirect:/persons/{name}"; }