Reputation: 552
I guess I'm looking more for advice than any specific coding solutions. Here's the scenario:
This form is accessed via a GET request through a Spring controller
@RequestMapping(value = "/secure/clients", method = RequestMethod.GET)
public String prepareNewClient(final Principal principal, final ModelMap map) {
map.addAttribute("client", new Client());
return "secure/clients";
}
The form is presented, all works fine and I submit the new client using $.ajax({})
The submission triggers a POST request using the same URL to the following method on my controller
@RequestMapping(value = "/secure/clients", method = RequestMethod.POST)
public @ResponseBody JsonResponse saveClient(
@ModelAttribute("client") final Client client,
final BindingResult result,
final Principal principal,
final ModelMap map) {
// validate input
// save client
// prepare JsonResponse object
return jsonResponse;
}
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
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:
It worked for me. And leveraging from Knockout.JS helped a lot as well.
Thanks again!
Upvotes: 0
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
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