• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

bringing up simple form in spring

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to bring up a simple form in Spring. When I enter a url in the browser, it calls my Controller which has the following code:



PhoneNumber class is just a class that has one String attribute called phoneNumber. In my jsp, I use the following :



I thought that the first time that the page was displayed, the bind to a phoneNumber object would just be blank and the form would come up with an empty text field where the user could type in the phone number. instead, I get an error that says

javax.servlet.ServletException: Neither Errors instance nor plain target object for bean name 'phoneNumber' available as request attribute

Hmm. It looks like I am using the wrong value for the path attribute in the bind tag. What is supposed to go there? It looks like some of the samples use ${command.new}. What is that? Evidently I am missing something. I am just starting to learn Spring. Any direction would be appreciated. Thanks in advance

Brian
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian,

"command" is the default name for the bound object that is used in the path attribute of the spring:bind tag. If you wish to use a name other than "command", i.e. "phoneNumber", you should call setCommandName("phoneNumber") in your constructor. I usually just use "command" as there is more potential for reuse of jsp fragments.
 
Brian Nice
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use setCommandName("phoneNumber") in the controller, what links that particular name to a particular class? Is it the bean name in the bean xml file? If I use the command as the path in the bind tag, and that command was set as my phone number class, then I can reference attributes of it just like ${command.areaCode} and ${command.number}?

What I want to do is when the user types in a URL, it brings up this form page. They type in a phone number, press submit, if there are errors, then it redisplays the form with an error message. It should know the first time they bring up the form that there is no information and the form should be blank. I don't have to check if ${command.new} right? Otherwise all the forms would have

if ${command.new} display empty form...
else display form with values ${command.number}

Thanks
Brian
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you use setCommandName("phoneNumber") in the controller, what links that particular name to a particular class? Is it the bean name in the bean xml file? If I use the command as the path in the bind tag, and that command was set as my phone number class, then I can reference attributes of it just like ${command.areaCode} and ${command.number}?



You will still want to call setCommandClass to make the association. The call to setCommandName just changes the binding name. So if you don't set the comand name, you use ${command.areaCode} and ${command.number}. If you set it to "phoneNumber", you use ${phoneNumber.areaCode} and ${phoneNumber.number}.

There are 2 ways to associate your controller with the class you are working with.

1. Use setCommandClass() in the constructor. This is more useful if you are not editing an exisiting item.

2. Override formBackingObject(). This allows you to do a more complex initialization, such as pulling in an exisiting item from the db.

Take a look at the Petclinic sample app and observe the differences between an "edit" vs. an "add" form controller.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic