Hi Paul,
Thanks for your insights. They are very helpful.
I should mention I’m new to java/spring mvc but not new to web development.
I come from asp.net c#.
Paul Clapham wrote:The address bar does indeed reflect the content on the page. It reflects where the page came from, that's how HTML works.
I agree. And that is what I want. Don’t want to do nothing different than what http/html standards provide.
If we use simple html this is how it would work.
Form1.html
<form action="form2.html" method="post">
<input type="submit" value="Submit"/>
Form2.html
<body>
Form2.html is open and the url displayed is /form2.html
</body>
1. User types .../form1.html at the browser and clicks enter
2. form1.html content is displayed and url correctly is /form1.html (as that was the requested url).
3. User clicks the submit button
4. Form2.html content is opened and the url /form2.html is correctly displayed
The final outuput is the form2.html content and a matching url (
http://localhost:8080/form2).
So it works just as I want.
The thing is that with Spring MVC, you add one additional step (the controller in the middle).
1. User types .../form1.html at the browser and clicks enter
2. form1.html content is displayed and url correctly is /form1.html (as that was the requested url).
3. User clicks the submit button.
4. Form action submits the form not to form2.html directly, but to a spring mvc controller on the server side. <form action="saveform1" method="post">.
5. Controller method that maps “saveform1” is called (controller method with the modelandview code I sent previously), processes code (saves data to db...), and finally calls form2.html with the following code:
ModelAndView modelAndView = new ModelAndView("form2");
6. Form2.html is correctly presented to the user, binding data from the object in the controller. But the url is what’s mapped in the controller (
http://localhost:8080/saveform1), not the html that was called inside the controller (
http://localhost:8080/form2).
So the additional spring mvc method (controller) makes the post request of the button make two calls instead of one (saveform1 first and form2 second).
And for it to have the same end result to the user(as it had on pure html) I think
http://localhost:8080/form2 should be the final displayed url to the user (as that is what was lastly requested in this process and is what is being finally displayed).
So I understand this is a Spring MVC thing and how it is implemented, not a http/html thing that would need to be changed.
For instance, if we go to asp.net this is how the flow goes.
1. User types .../form1.aspx at the browser and clicks enter
2. form1.aspx content is displayed and url correctly is /form1.aspx (as that was the requested url).
3. User clicks the submit button.
4. A button onclick event is called on the server side to execute whatever you want. Just like you would do at the spring mvc controller (for instance, saving some data on the db). Then you can redirect the user to form2 with a Response.Redirect("/form2.aspx").
5. form2.aspx content is returned to the user and the url displayed is
http://localhost:8080/form2.aspx (not a url in the middle like /saveform1).
I just want to do the same here.
And asp.net uses the same http protocol.
That’s why I understand this is more of a Spring MVC thing and how to use it, than a need to change html or http protocol.
Anyways, after some digging I’m glad to say I found out how to make this work on Spring MVC! I knew there should be a way to do it
In fact, it's pretty simple.
You just need to add a @ModelAttribute annotation to the controller method, add the saved object to that using addFlashAttribute and do a return "redirect:/form2". Then on Form2 controller retrieve the object from @ModelAttribute, bind to the form2.html and return form2. That will display form2 fields (filled with the object data) and also show form2 url (as it is supposed to since form2 is what was requested to the server). Gotcha!
Anyways, I appreciate you guys time and willingness to help.
As I said, I'm new to spring mvc and will certainly come back for more help from you guys in the future.
This was a great discussion. Appreciate anything else you have to add.
Best regards.