Results 1 to 8 of 8

Thread: Spring MVC FlashMap and RedirectAttributes request mapping

Hybrid View

  1. #1
    Join Date
    Oct 2011
    Posts
    6

    Default Spring MVC FlashMap and RedirectAttributes request mapping

    Hi all,
    Yesterday I downloaded the new Spring 3.1RC to test the just introduced support for flash scoped variables in Spring MVC. Unfortunately I could not make it working...

    I have a HTML form containing some checkboxes without spring:forms tags. Something like this:

    Code:
    <form action="/deleteaction" method="post">
    <input type="checkbox" name="itemId" value="1" />
    <input type="checkbox" name="itemId" value="2" />
    <input type="submit" name="delete" value="Delete items" />
    </form>
    Before the Flash scope support, my annotated controller looked like:

    Code:
    @RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"})
    public String deleteItems(@RequestParam(value="itemId", required=false) String itemId[]) {
    Alternatively, I could have used an HttpServletRequest instead of @RequestParam:

    Code:
    @RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"})
    public String deleteItems(HttpServletRequest request) {
        String itemIds[] = request.getParameterValues("itemId");
    Both methods were working fine. If I try to add the RedirectAttributes to the method parameters, Spring will throw an exception:

    Code:
    @RequestMapping(value = "/deleteaction", method = RequestMethod.POST, params={"delete"})
    public String deleteItems(@RequestParam(value="itemId", required=false) String itemId[], RedirectAttributes redirectAttrs) {
    Log:

    Code:
    Oct 16, 2011 11:20:37 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/App] threw
        exception [Request processing failed; nested exception is
        java.lang.IllegalArgumentException: argument type mismatch] with root cause 
        java.lang.IllegalArgumentException: argument type mismatch
    What am I doing wrong? How is it possible to get the RedirectAttribues parameter where to add flash scoped variables?

    Thanks a lot for every hint
    Andrea

  2. #2
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    To use RedirectAttributes you need to be configured with RequestMappingHandlerMapping, RequestMappingHandlerAdapter, and ExceptionHandlerExceptionResolver, which are new in Spring 3.1 and replace the existing DefaultAnnotationHandlerMapping, AnnotationMethodHandlerAdapter, and AnnotationMethodHandlerExceptionResolver. The latter are still available but do not support any new features.

    Check how your DispatcherServlet is configured. Do you use <mvc:annotation-driven/> or not? The MVC namespace uses the new classes but if you don't use the namespace you'll need to replace the old infrastructure classes as mentioned above. Of if this isn't the problem, set logging for 'org.springframework.web' to DEBUG. That should provide information about the argument it fails to resolve.
    Last edited by Rossen Stoyanchev; Oct 17th, 2011 at 01:28 PM.

  3. #3
    Join Date
    Oct 2011
    Posts
    6

    Default

    Hi Rossen, Thank you for your answer.

    In my dispatcher.xml I had both <mvc:annotation-driven/> and a DefaultAnnotationHandlerMapping, since I needed a localeChangeInterceptor.

    <bean id="annotationUrlMapping" class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping">
    <property name="order" value="0" />
    <property name="interceptors">
    <list>
    <ref local="localeChangeInterceptor" />
    </list>
    </property>
    </bean>

    <mvc:annotation-driven />
    Now I have commented out the DefaultAnnotationHandlerMapping, the RedirectAttributes object is passed to the method. Unfortunately now the localeChangeInterceptor is missing... How can I hook an interceptor in the new handler?

    Thanks a lot for the help

  4. #4
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    You can add an interceptor through the mvc namespace:

    Code:
    <mvc:interceptors>
        <ref local="localeChangeInterceptor" />
    </mvc:interceptors>
    This will apply it to all HandlerMappings, which is probably what you want with the LocalChangeInterceptor.

  5. #5
    Join Date
    Oct 2011
    Posts
    6

    Default

    Thanks again, I was not aware of the interceptors tag in the mvc namespace. Now everything is cleaner...

    Could you please tell me in which case the "addAttribute" method in the RedirectAttributes is used? Aren't only flash attributes "forwarded" to the redirect?

    Last but not least, in non sticky session environments, could flash messages stored somewhere else than in the session (For example in the DB)? How?

  6. #6
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    Could you please tell me in which case the "addAttribute" method in the RedirectAttributes is used?
    The addAttribute methods are for use in the URL either as URI variables (e.g. "redirect:/foo/{bar}") or otherwise they get appended as query parameters.

    could flash messages stored somewhere else than in the session (For example in the DB)? How?
    Yes, on startup the DispatcherServlet looks for a FlashMapManager bean by name "flashMapManager". If it doesn't find one it uses DefaultFlashMapManager. You can extend that class and configure your implementation in your DispatcherServlet configuration.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •