Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to bind data to a Spring form input tag

 
Soumya Rout
Ranch Hand
Posts: 49
Java Java ME Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read any basic tutorial on Spring MVC ?
 
Soumya Rout
Ranch Hand
Posts: 49
Java Java ME Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic