Tarcio Saraiva
Tarcio Saraiva

Reputation: 552

REST, Spring MVC 3 and PUT request with AJAX

I guess I'm looking more for advice than any specific coding solutions. Here's the scenario:

After completion, I want to keep the user on the same screen and have the form trigger a PUT instead of a POST. Which is fine, I can achieve that with jQuery, but as soon as I submit the form again, the client is not on the ModelMap anymore.

I have even tried adding the saved client to the ModelMap of my POST method but even that didn't work.

I'm not entirely sure if I'm doing it the right way. To be honest, every tutorial I've seen uses more or less what I'm doing but NONE of them have a PUT request - mostly deal with creation of objects, which I don't have an issue.

What I have in mind is that I might need to have a controller method mapping to /secure/clients/{clientId} with HTTP.GET and another controller method mapping /secure/clients/{clientId} with HTTP.PUT.

But I'm not sure if that makes sense, so I'm a bit lost.

Suggestions are more than appreciated. Thank you!

Upvotes: 1

Views: 1023

Answers (3)

Tarcio Saraiva
Tarcio Saraiva

Reputation: 552

@Daniel and @Japan thank you very much for your answers.

I figured what you said @Daniel and I ended up stepping out of the box and thinking differently - and it worked great.

Just to give you an idea:

  • Instead of staying on the page, when a new client is inserted I actually refresh the browser
  • After the function is called the user gets redirected to /secure/clients/{clientId}
  • From there on a new function is mapped, along with a new POST request

It worked for me. And leveraging from Knockout.JS helped a lot as well.

Thanks again!

Upvotes: 0

Japan Trivedi
Japan Trivedi

Reputation: 4483

I agree with @Daniel.

But you can use HiddenHttpMethodFilter available in Spring. That should make your work easy. You will no longer need to put the hidden filed every time.

Hope that helped. Cheers.

Upvotes: 1

Daniel Lavoie
Daniel Lavoie

Reputation: 1902

HttpMethod are not enterely support by web browsers. PUT requesr works great when two applications are working with Restfull API's, but for browser compatibility, it is better to limit yourself to POST or GET request.

You can also put a override parameter in your form to instruct the web server to handle the post as the desired method.

<input type="hidden" name="_method" value="put"/> would do the job.

Spring sf namespace takes care of that hidden field for you.

<sf:form method="put">
....
</sf:form>

In addition, you can test your controller with restclient-ui to see if the problem comes from your WebServer or your client.

Upvotes: 2

Related Questions