Graham Lea
Graham Lea

Reputation: 6343

Why isn't Spring Web/MVC adding my model attributes to the URL of my redirect view?

Doing a redirect to GET after POST. Spring Web should be adding the 'clientId' model attribute below onto my URL, but it's not....

@Override
protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {
    ClientID clientId = ((MyCommand) command).getClientId();
    return new ModelAndView(new RedirectView(getSuccessView()), "clientId", clientId);
}

The same problem occurs if I just pass getSuccessView() to the ModelAndView and put redirect:<success_view_url> as the success view in my config.

Upvotes: 1

Views: 1028

Answers (1)

Graham Lea
Graham Lea

Reputation: 6343

The problem is that, by default, RedirectView only commutes what it calls 'Simple Value Types' to the URL. c.f. RedirectView.isEligibleValue().

The solution is to add the ClientId to the model as a String rather than the more custom type:

@Override
protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {
    ClientID clientId = ((MyCommand) command).getClientId();
    return new ModelAndView(new RedirectView(getSuccessView()), "clientId", clientId.toString());
}

Note .toString() on the last line.

You can override isEligibleValue() (on your own RedirectView subclass) is you really want to, but it's easier to just toString everything you put in there.

Upvotes: 1

Related Questions