Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Bean default value for setProperty

 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
Guys. I will initialize it in the constructor.

ML
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i m not mistaken we can always do that. like this,

<input name="firstName" value=" ">
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic