File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes How to bind data to a Spring form input tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to bind data to a Spring form input tag" Watch "How to bind data to a Spring form input tag" New topic
Author

How to bind data to a Spring form input tag

Soumya Rout
Ranch Hand

Joined: Aug 06, 2009
Posts: 49

Hello,
Please somebody tell me how retrieve data from a bean and display it in Spring form input tag.



Thanks in advance.




Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Did you read any basic tutorial on Spring MVC ?


[My Blog]
All roads lead to JavaRanch
Soumya Rout
Ranch Hand

Joined: Aug 06, 2009
Posts: 49

I know the basics but I need the code to bind data from bean / session with form fields.


I am using following code





I am not getting any output.
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
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.

I'd recommend getting and reading the latest Spring Reference documentation at http://www.springsource.org/documentation or the book "Spring Recipes" by Gary Mak as a starting point. I'd also recommend looking at the sample code provided by SpringSource. They have some really nice examples of how to do this. Have a look at:
http://blog.springsource.com/2010/07/22/spring-mvc-3-showcase/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Interface21TeamBlog+%28SpringSource+Team+Blog%29
Soumya Rout
Ranch Hand

Joined: Aug 06, 2009
Posts: 49

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


Thanks Soumya
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
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.

Mark
Soumya Rout
Ranch Hand

Joined: Aug 06, 2009
Posts: 49

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 :











Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to bind data to a Spring form input tag