• 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

Help-populate object before form is loaded but before it's submitted

 
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When accessing my web page initially I have to acquire the userid of the person accessing the page from a querystring value that is passed in the request:



I get the value and it shows up in my JSP page but I want to be able to load the value in an object, which is what I thought I was doing in the instantiated USER object in this UserBean class. Unfortunately, the User object is null after I click the submit button on the form that I'm completing. My assumption was that I could set the scope on the UserBean class with a value of "session" in the faces-config.xml file and then my User object would be loaded. I also set the USER class to a scope of "session". But to no avail. I am not sure how to get the USER object populated through the point where the page is submitted.

I'm obviously missing something but just cannot find it. Please help.

Any help/direction would be greatly appreciated.

Regards.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is not the way you get hold of managed bean. You may have to ask the session to get the User object instead of using "new User()". Now it seems that the User object is created as an ordinary Java object with scope within the local method in which it is created. Try querying session for the User object and use it.

regards,
Nirvan.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Nirvan but is that via an attribute? I tried that but did not find it there. Is there another property in the HttpSession where I get that? Any example would be greatly appreciated.

Thanks for your time and help.
 
B Nirvan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use this in my current project. Define following function in any class (say Utilities)


and this is how to use it.

statesHelper is the name of my managed bean and StatesHelper is the actual class.
That said, it is always very easy to use dependency injection. The framework would take care of injecting your User bean.

cheers,
Nirvan.

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I'll give it a try.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope that you're planning to get the user ID for casual information purposes only and not as part of a do-it-yourself web application security system, because anyone over the age of 5 years could stomp a security system like that flat without half thinking.

JSF does not "load" backing beans, it instantiates them. That means, specifically, that if a View calls for a bean property, the name of the bean in looked up in the compiled faces-config.xml file, the scope is searched for that attribute, and if the bean doesn't exist, it's instantiated using the class.newInstance() method. Then, if there are any managed properties, they are injected into the newly-instantiated bean, recursing on the process if any of the referenced managed propertiy beans didn't exist previously.

If you want to load/reload specific values into a managed bean, you have to explicitly do that yourself, and prior to JSF 2, and the @PostConstruct annotation, that could be an annoyance. @PostContruct at least gives you a point where the management framework has instantiated and initialized the bean's managed properties before anything actually start to use the bean.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the valuable info Tim. At this point, the userID is only being used to populate the transaction userID field in a database. That's it.

But I appreciate very much your insight and information. Regards.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nirvan,

I used your example above and when I put the following code in my transactionBean:



I saw in the Variables window of my debug perspective in Eclipse, the userBean and it had the userID populated in my USER object as well as the userBean's userID populated as well. However, when I tried:



value was NULL. Any suggestions on how to get at the userBean.user.getUserID value?

I can see it in my Variables window in debug but I cannot get to it. What am I missing?

Thanks.
 
B Nirvan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After you get the reference to your UserBean, set its property to the new User object that you have created.

the code should look something like this.


hope that helps.

regards,
Nirvan.
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you only use the user ID value of the user then there is no need for the User object itself. So you can set the userId directly in the UserBean instead of putting it inside User object. The User object inside userBean does not get automatically created, only the userBean does.

But if you do want to create the User object too then (at least in JSF 2) you can do something like thisand then


EDIT: Nirvan Buddha already answered your question and his solution is simpler.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nirvan - I cannot get at the userID variable in this line:

user.setUserID(userID);

This is my problem. I cannot get to these values. Isn't the userID from userBean? And if I try to do a userBean.getUserID() then I go back to the request where it is currently NULL. I know it must be a simple thing but I cannot get to it. I know I'm missing something.

Thanks for your patience and help. Regards.
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nirvan is suggesting that you use the lines he said inside the method you use in your original post. That way the userBean is created by JSF but the user inside the userBean is created by you.
 
B Nirvan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought you have two different objects User and UserBean. Thats what your second last post hinted. I would suggest you use the UserBean object and define all properties of User object in it. Then use the find method to get the UserBean. Get rid of the redundant User object.

regards,
Nirvan.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nirvan - Thanks for your reply. There are 2 separate objects. The UserBean and User. They each get the userID and userName. Right now I'm not worried about userName.

I'll try what you suggested and move everything to UserBean.

Thanks.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me verify one thing, after I click on the submit button on my form, then I will use the find method in another other class, TransactionBean in my createTransaction(), to get at the UserBean, correct?

Just want some clarification. Thanks.
 
B Nirvan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Melinda,
Just use the code below.

In your TransactionBean use the same find() method and access its userId property


regards,
Nirvan
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that my UserBean class now looks like:



In my createTransaction I put as you suggsted:



This issue is when debugging, the code line



I get a NULL value because the userID in the getUserID() of the UserBean class that is getting executed is again getting the userID from the REQUEST and consequently the userID is going to be null because there is nothing in the request.

This is what I was trying, attempting, to say above. Hope this makes sense. Again, your help and time are very much appreciated.

Regards.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I think I've got it resolved.

My apologies. Thank you, Nirvan, for ALL your help and time. Regards.
 
reply
    Bookmark Topic Watch Topic
  • New Topic