0

I have this controller's method:

@RequestMapping(value="/path", method = RequestMethod.GET )
    public String path(Model model, RedirectAttributes redirectAttributes)  {
        redirectAttributes.addFlashAttribute("attr", "valueFromPath");
        return "redirect:jspPage.jsp";//this page located in webApp folder
    }

jspPage.jsp:

...
<h1>${attr}</h1>
...

In my case this row is empty, but I want that valueFromPath will shown on this jsp.

How I can make it?

web.xml:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    ...
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
homeAccount
  • 643
  • 2
  • 6
  • 13

1 Answers1

1

I'm going to make an assumption here. Because of your URL mapping of

<url-pattern>/*</url-pattern>

The DispatcherServlet is not handling the request for jspPage.jsp. That jobs falls on the default Servlet. As such, the DispatcherServlet cannot perform the logic that adds the flash attributes from the HttpSession attributes back into the request attributes.

You need to make sure that the request for jspPage.jsp is handled by the DispatcherServlet. Either by moving the file to some other location or changing your url-pattern to / and providing a handler.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • @homeAccount I don't know if you say, but I've edited my answer. – Sotirios Delimanolis Oct 08 '13 at 22:25
  • Sotirios Delimanolis, I am rewrite mapping for / .Now I see my variable value in browser adress field but this value is not available on page as ${attr} – homeAccount Oct 09 '13 at 07:26
  • I don't want write handler. If I replace redirect by the forward - it is works how I want. –  Oct 09 '13 at 07:29
  • and use model.addAttribute instead redirectAttributes – homeAccount Oct 09 '13 at 08:02
  • 1
    @homeAccount [A `redirect` and a `forward` are two completely different things](http://stackoverflow.com/questions/6068891/difference-between-jsp-forward-and-redirect) and prove I was right. When using a `forward`, the model attributes will be added to the request attributes first, then the request will be dispatched with a forward which will immediately render the `jsp` by the `DefaultServlet`. With a `redirect`, the flash attributes are only added to the request attributes after the request, but the `DispatcherServlet` doesn't handle the next request so they aren't available. – Sotirios Delimanolis Oct 09 '13 at 12:23