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?
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.
When a dream is ending because to come true - OCPJP 6,7. OCE JPA EE6. MCTS
Joined: Jan 26, 2011
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 ":".
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.
An IDE is no substitute for an Intelligent Developer.
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
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.