|
|
Hello All,
I want to persist form data and I am using a simple form controller. The scenario is that users will fill out a form and then do a search. When they go back to the form page to do another search, I want it to remember their previous search criteria.
Is the simple form controller the best thing to use? Should I use the abstract form controller? If anyone has any advice or links to code examples, I would be grateful. As you can tell I am a newbie and just recently got my feet wet with the Spring MVC tutorial and the Pro Spring book.
Thanks!
Yogesh
You have two options to quot;rememberingquot; the previous search, either post the search criteria every time via hidden fields/ucl, or stick the form backing object in the session.
But yes, either way SimpleFormController will be just fine
second option suggested is better than hidden fields. u can make command object and store it in a session and when request comes for the form to open, and since its simpleFormController, it will go in formBackingObject. there u can check if it exists in session,return that one else create new one.
Originally Posted by harry_msecond option suggested is better than hidden fields. u can make command object and store it in a session and when request comes for the form to open, and since its simpleFormController, it will go in formBackingObject. there u can check if it exists in session,return that one else create new one.
If you set sessionForm to true there is no need to do any of that SimpleFormController will work exactly as you describe.
Hello All,
Thanks for the speedy help. I am knee deep in packing today to move into a new house but I will try out the different methods described in this thread. Looks like there are many options to accomplish what I am looking to do!
Thanks,
Yc
Originally Posted by ychawlaHello All,
Thanks for the speedy help. I am knee deep in packing today to move into a new house but I will try out the different methods described in this thread. Looks like there are many options to accomplish what I am looking to do!
Thanks,
Yc
Oh come on Yc, prioirities man! New house versus Spring, and you chose the new house? I don't know, you just cannot get the developers nowadays
Hello All,
Thanks for the help thus far. I have another question. In the form backing object I can explicitly set the values and everything works fine:
Person prsn = new Person(); prsn.setLastName(quot;chawlaquot;); return prsn;
When I pull up the jsp with the form the last name is then set to 'chawla'. How do I get this last name out of the command object? I am assuming that I have to stuff the command object in a session variable in the onSubmit method. Can anyone could point me towards a code example of how to store the command object in the session in the form controller onSubmit method?
Thanks for helping out a newbie. If there are some tutorials you think I should read prior to posting these types of questions, let me know.
-Yogesh
Hello All,
I am making some progress on this and anticipate posting a complete solution soon.
For now in the onSubmit I changed the method to accept the request object:
public ModelAndView onSubmit(fromServletRequest request, fromServletResponse response, Object command, BindException errors)
then I set a session var:
session.setAttribute(quot;lastNamequot;, ((Person) command).getLastName());Then in the formBackingObject I did this:
Person prsn = new Person(); fromSession session = request.getSession(true); String lastName = quot;quot;; lastName = (String) session.getAttribute(quot;lastNamequot;); if (lastName != null) { prsn.setLastName(lastName); } return prsn;rather than just assign strings to the session, i am going to try to attach the command object to the session.
More to come..
Here is the onSubmit snippet public ModelAndView onSubmit(fromServletRequest request, fromServletResponse response, Object command, BindException errors)throws ServletException { fromSession session = request.getSession(true); session.setAttribute(quot;personCommandquot;, command);
Here is the form backing snippet:
Person prsn = new Person(); Person prsnSession = new Person(); fromSession session = request.getSession(true); String lastName = quot;quot;; String middleName = quot;quot;; prsnSession = (Person) session.getAttribute(quot;personCommandquot;); if (prsnSession != null) { middleName = prsnSession.getMiddleName(); lastName = prsnSession.getLastName(); advancedSearch = prsnSession.getAdvancedSearch(); } if (lastName != null) { prsn.setLastName(lastName); }
if (middleName != null) { prsn.setMiddleName(middleName); }
Rather than get all those strings individually, you can just return the command object as follows:
Person prsnSession; fromSession session = request.getSession(true); prsnSession = (Person) session.getAttribute(quot;personCommandquot;);
if (prsnSession == null) { prsnSession = new Person(); } return prsnSession;You learn something new every hour |
|