• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Initializing a useBean with some non-default value

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to JSP. In a toy project of mine, I'd like to use useBeans to present data of, say, a user. Given the user has an ID, I'd like the bean to be initialized with the given ID, and have the rest of the properties loaded from the DB. In regular Java code, I'd have a User class with a constructor that gets the ID, and loads the rest of the stuff. However, beans can't do that. I was able to come up with two ways to do such initialization:

1. Have a special setter, say "setInitialize", and then set the "initialize" attribute. This is, IMO, kind of abusive, as "initialize" isn't really an attribute of the bean...
2. Use a scriptlet to explicitly call a "loadUser" method of the bean before getters are called. Possible, but dirties the page with Java code.

Most code examples I could find first set all fields using a form, and then use that data. Is it really that uncommon to initialize the bean somehow, and then just use the getters in EL? How should I better do such initialization?
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All that sort of work should be done in the page controller before control gets sent to the JSP. Please read this article for more details.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic