Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie Question

 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic