aspose file tools*
The moose likes Other Application Frameworks and the fly likes bringing up simple form in spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "bringing up simple form in spring" Watch "bringing up simple form in spring" New topic
Author

bringing up simple form in spring

Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
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
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
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.


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
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

Joined: Nov 27, 2002
Posts: 451
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.
 
 
subject: bringing up simple form in spring
 
Similar Threads
Help me about html:select
Problem in JSTL
Spring JasperException
[SPRING] Java Bean doesn't get prepared
Getting bean property values using jstl in jsp page