File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Display the selected value within an <h:inputText> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Display the selected value within an <h:inputText>" Watch "Display the selected value within an <h:inputText>" New topic
Author

Display the selected value within an <h:inputText>

michael nik
Greenhorn

Joined: Mar 11, 2012
Posts: 15
I have an <h:selectOneListbox> in my xhtml file and when the user select a value I would like to display the selected value within an <h:inputText> below the page, or using a pop up or to another xhtml. I know that <h:inputText> component is used for retrieving data from the users, however i want to display it some way near to a <h:inputText> in order to give the opportunity to the users to perform some predicates e.g child > 12 (child is the selected value). But I didn't find a way to achieve that yet.

I was thinking that the reason is: UI components for User interface are in the Client side whereas the selected value after the selection of the value are in the Server side (myBean). Is it correct ?

So the only way to accomplish that is using Javascript?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

UIComponent objects reside only on the server. They are rendered into HTML to provide the client View.

Because the inputText control is a JSF control, it will normally reflect its value property as it was when the page (or page fragment) was last rendered. Of course, since it's an input control, it can then be updated either by human or by client-side javascript inputs.

So you have 2 options.

1. Use AJAX so that when the parent control (selectOneListBox) is changed, an AJAX request updates the backing bean and fires an action that causes the backing property value of the inputText to change (you also have to inform the AJAX mechanism to re-render the inputText).

or

2. Tie JavaScript directly to the selectOneMenu so that its onchange handler will post the updated value to the inputText control's underlying HTML INPUT control element. This is nothing major, but you do have to remember that the HTML ID of that control is not 100% identical to its JSF ID and code accordingly.


Customer surveys are for companies who didn't pay proper attention to begin with.
michael nik
Greenhorn

Joined: Mar 11, 2012
Posts: 15


In my java I have



I have 2 problems here,


that my listener="#{myBean.setNewValue()}" setNewValue is unknown method and
<f:ajax> contains an unknown id 'input' - cannot locate it in the context of the component key


Please help me since I am beginner or provide me an example how to combine ajax within jsf to achieve that.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

setNewValue is an unfortunate choice for a Listener method name - it can be confused with legitimate property mutators, since it begins with "set". A better name would be something more like "selectItemsListener". Do not include "()" on the EL. JSF already knows that there's a listener method call and I think you're confusing it, since you've coded it as a no-argument method call.

Why the item ID "input" isn't resolvable I'm not sure. I have been shirking my studies on the f:ajax tag, although I can't do that much longer.

michael nik
Greenhorn

Joined: Mar 11, 2012
Posts: 15
I tried a lot but I have the same problem, My xhtml file does not recognise the selectItemsListener. Always there is an unknown method selectItemsListener. Have I forgotten something else?

I have <h:form> tags in my xhtml and I included the import javax.faces.event.AjaxBehaviorEvent; in my bean. I am using @SessionScoped


michael nik
Greenhorn

Joined: Mar 11, 2012
Posts: 15
It works, I had a mistake in myBean, I didn't define correct the getter method of my listenerItems.

Unfortunately, it is displayed properly only using <h: outputText> tag. If I used <h:inputText> tag I get an error of Conversion Error setting value.

Are you sure that it can be worked using an input control ?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

I'm sure.

Converters are used to translate between binary and text formats. They are required because HTML is a text-only representation and therefore any binary data must be converted. Likewise, when you enter text into an HTML form control and JSF is backing that control with a binary property such as an "int", the converter manages that task as well.

If the selected converter cannot convert - for example, you typed the value "Fred" into a textbox mapped to a numeric property, a conversion error gets thrown.
 
 
subject: Display the selected value within an <h:inputText>