I'm a little confused. Is the first part servlet code? ...Or is this whole listing a JSP with a Java scriptlet? The basic process would be something like the following:
You have a controller class that you would call initially to set up any required data and possibly handle the submit from your form. In this model, you should define a class that represents your form backing object. Note that there are a myriad of ways to define a controller. This is just one of the simpler ways to do it.
Then you could initialize it in a controller
Now in the JSP that is your form page, you just need the following snippet
When you define things this way, Spring does most of the work of binding the data from the form backing object onto the form and back into the object on submission.
If you need more complex binding, then you should consider a property editor. It's possible to actually bind a custom object to the form using custom property editors, which is where the power of Spring and MVC are realized.
In your situation, it might be that the UserSession might be your form backing object that would be set from the controller. All you need to do is set it in the model with a name that you will use to reference it by in your form. Behind the scenes, the framework is setting this in either a request attribute or a session attribute. By the way, I demonstrated using a request attribute. You can also make this a session attribute by adding one annotation.
Thanks Mark I got the solution.
I have one more problem. I am trying to get some data from database through Service and DAO classes and store them in form backing object. And return that object to the JSP . But its not working. Isn't it a right process to do ??
Please help me out
Joined: Jul 01, 2003
Generally, yes that's the way you'd do it. However without more detail, it's hard to know why it isn't working. Some things to check:
1. Are you actually getting the data back?
2. Do you have access to the data at the service layer?
3. If you are using JPA/Hibernate and LazyLoading, you can have problems with accessing lazily loaded items unless you keep your hibernate session open.
I am not getting any sort of data back. When the following code excuted it throws exception and server says resource not available.
The SVC method
The getUserDta method :
Joined: Jul 01, 2003
Can you verify where you stop getting data back? In other words, can you verify that you are receiving the data at the service layer? I have a hunch it's probably something simple like a bean not being wired in properly or a data source not set up properly. It would also be helpful to see the exception.
subject: How to bind data to a Spring form input tag