wood burning stoves 2.0*
The moose likes JSF and the fly likes Newbie Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "Newbie Question" Watch "Newbie Question" New topic
Author

Newbie Question

Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
Requirement: There are 2 text boxes and a command button on a page. Type in first textbox and click the button, typed text should appear on the second textbox.

What I used: RAD8.0 default implementation of JSF2.0

What I did:

1)
<h:inputText styleClass="inputText" id="text1"></h:inputText>
<h:inputText styleClass="inputText" id="text2" value="#{textBean.textValue}"></h:inputText>
<h:commandButton type="submit" value="Submit"
styleClass="commandButton" id="button1" action="#{managedBean.showText}"></h:commandButton>

2) textBean is a request-scoped backing bean with textValue attribute and its setters and getters
3) managedBean is the RAD-generated, request-scoped bean with all the component getters, in which I have the showText action as foll


4) finally the navigation rule
<navigation-rule>
<navigation-case>
<from-action>#{managedBean.showText}</from-action>
<from-outcome>copyText</from-outcome>
<to-view-id>/JsfTestPage.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

Question: Is there a better (simpler) way to code for the above requirement? I am still trying to get my head around the lifecycle..so forgive this question..isn't it possible to do a simple text2.value=text1.value somewhere?


Sue

SCJP 1.2
SCWCD 1.4
SCBCD 5.0
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Sue Pillai wrote:.isn't it possible to do a simple text2.value=text1.value somewhere?

Use only one bean either RAD generated or what you have coded!
Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
True, but I meant the showText() method..getting the facesContext instance, getexternalcontext, etc..etc..till you set it back as an attribute. I was thinking my approach was long-winded and there was a shorter way
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

You didn't supply a value attribute on the text1 control. Therefore, the MVC controller has no Model to get/set with the contents of that form control. You need something more like this:


To achieve your purpose, the the showText action needs to do something like this:


You don't need a navigation rule in JSF2 if you're re-displaying the same View. If you place the showText method in the textBean instead of its own bean and are following the POJO JavaBean conventions, you can also code:


As a general rule, the only objects and classes from the javax.faces packages that most JSF code should be using are the SelectItem and DataTable model wrapper classes. When you start referencing the FacesContext and/or non-model JSF classes, there's a good chance you're not doing it right.


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

Joined: Nov 04, 2004
Posts: 89
This is exactly what I was looking for. The model is much simpler now.

Tim Holloway wrote:
When you start referencing the FacesContext and/or non-model JSF classes, there's a good chance you're not doing it right

I agree; didn't feel right doing it.

Thank you so much for the clarification
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Newbie Question