• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Getting value from an HtmlInputText

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a form with two input text boxes, one for name and one for age. I have bound these text boxes to attributes of a managed bean. For the submit button I have set the immediate attribute to true so that it runs after the apply values to component tree phase (phase 2) and have bound it to an action method. However, I need the values from the text boxes to do some processing in the action method. How do I get the values of the input text boxes from within the action method. I can't get the values from the managed bean because I'm executing the action method before the values are stored to the managed bean. I tried the following code:



Both methods for getting the value of the input text field cause a null pointer exception. Can anyone tell me how to get the value of an input text box from within the backing bean without having to wait for a managed bean to be populated?

Thanks,
Ajay
 
Ranch Hand
Posts: 90
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajay

Is strange what you need, but one way you can do is getting the value of the inputText from the request values. I give you an example:

and in the testAction method you can get the value as follows:

Referring to the code you post are you sure that your bind correctly the attributes of your inputText, Are you use the binding attribute to bind the HtmlInputText so???:
where componentProperty would come to be the getter for your HtmlInputText name.
I hope this can help you to solve your problem.
Regards
Cesar
 
Ajay Dhar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your approach would work but I did it in a slightly different way. I got the HttpServletRequest object from Faces Context and then did getParameter(form+":"+id) on this request object. Like you suggested, I needed to pass the form name and the component id to the getParameter method. The key thing was determining the correct separator ":".

Thanks for your help!
 
Cesar Loachamin
Ranch Hand
Posts: 90
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, but I think is the same as I did. .
Regards
Cesar
 
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What???



You are NOT executing an action method if the form values aren't already updated in the backing bean. That's because JSF won't fire the action method until all the input controls on the form have been validated and the bean's properties have been updated. JSF has a very definite lifecycle that mandates this. Please don't try to force it to do things the hard way. JSF is designed to be simple, easy to use, and to be able to use POJO objects and methods in preference to framework-dependent objects and methods. Too many people are trying to make complex solutions to things that JSF was created to make simple.
 
Cesar Loachamin
Ranch Hand
Posts: 90
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I agree with Tim, due I said that Ajay wants to do something strange, becuase he tries to force the normal JSF Lifecycle, he must be aware of because he wants to do that, but the way I do not agree with you about the action method, it will be executed beacuase we're setting the attribute immediate="true", and for that it's invoked in the Apply Request Values phase before the Process Validation phase. Cesar
 
Tim Holloway
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cesar Loachamin wrote:Yes, I agree with Tim, due I said that Ajay wants to do something strange, becuase he tries to force the normal JSF Lifecycle, he must be aware of because he wants to do that, but the way I do not agree with you about the action method, it will be executed beacuase we're setting the attribute immediate="true", and for that it's invoked in the Apply Request Values phase before the Process Validation phase. Cesar



Actually, 'immediate="true"' doesn't mean before. If you specify immediate, the entire bean validation and update phases are bypassed altogether. The only way you could get the input field data would be by wholesale rape of the JSF internal resources, and the whole point of JSF is to make things simpler, not to generate a lot of esoteric framework/version-dependent code.
 
Maybe he went home and went to bed. And took this tiny ad with him:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic