This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Newbie Question

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 24517
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic