wood burning stoves 2.0*
The moose likes JSP and the fly likes Java Bean default value for setProperty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Java Bean default value for setProperty" Watch "Java Bean default value for setProperty" New topic
Author

Java Bean default value for setProperty

Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
Hello,
I have a jsp page with a form. On submit I call an action page that has


My problem is, there are many fields in the form that are left blank. For all these fields the myBean.propertyName = null. Is there a way I can initialize to empty String("").

Thanks,
Martin
Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
Thanks,
Guys. I will initialize it in the constructor.

ML
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
if i m not mistaken we can always do that. like this,

<input name="firstName" value=" ">
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
You could always use instance initializers or the constructor to initialize the form bean properties to empty strings (or whatever you want). If a request parameter has an empty or null value, the corresponding Bean property is not set, so your initial value would still remain in the form.

Sheldon Fernandes
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
dont know wht your real problem is,

you can initialize your attribute at the time of declaration or in constructor of your bean class.
Prathiba Kalirengan
Greenhorn

Joined: Aug 30, 2004
Posts: 12
U have one more option...
inside setXXX methods check for null ...if it is true set to ""
so the " " will be automatically assigned if the value is null..
U can have data validators also inside..


SCJP 1.4<br />SCJA BETA
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

inside setXXX methods check for null ..


That would not work. The setter is never called to set it to null. Now, the accessor could perform such a check, but not the mutator.

And Prathiba,

JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.

I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.

thanks,
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Bean default value for setProperty
 
Similar Threads
fields not displaying in a jsp
javax.servlet.jsp.JspException: Cannot retrieve definition for form bean null
Multiple forms in JSP
after refreshing values should remain same
how to get reset to work after form submit in JSP?