aspose file tools*
The moose likes JSF and the fly likes Getting value from an HtmlInputText Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Getting value from an HtmlInputText" Watch "Getting value from an HtmlInputText" New topic
Author

Getting value from an HtmlInputText

Ajay Dhar
Ranch Hand

Joined: Jan 26, 2011
Posts: 30
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


OCPJP 6, OCEEJBD 6, GIAC Secure Software Programmer-Java (GSSP-Java)
Cesar Loachamin
Ranch Hand

Joined: Dec 25, 2010
Posts: 90

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

When a dream is ending because to come true - OCPJP 6,7. OCE JPA EE6. MCTS
Ajay Dhar
Ranch Hand

Joined: Jan 26, 2011
Posts: 30
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

Joined: Dec 25, 2010
Posts: 90

You're welcome, but I think is the same as I did. .
Regards
Cesar
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Cesar Loachamin
Ranch Hand

Joined: Dec 25, 2010
Posts: 90

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

Joined: Jun 25, 2001
Posts: 16019
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting value from an HtmlInputText