• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How to get a value of a text field or a bean member in a JSP tag?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a JSF page with 2 outputText fields.
I want the second one to appear only if the first has a certain value.

It should be something like:



But I don't know how to get a value of a JSF field, or a value of a java bean member in a JSP tag (that is not bind to a field in the page), like the example above.

Can anyone help me?
Thanks,
Efrat
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might not be the best practice when using JSF, but in that situation, I know you can do something like:

<jsp:useBean id="alarm" class="com.mycompany.ModuleBean" scope="session"/>

That will make the JSP look up the JSF managed bean object and give you direct access to it. You'd need to set the class and scope parameters to match what you have in your faces-config file. Or, you can even do it the messier way by writing a scriptlet that does the equivalent of the above.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:


<h outputText value="#{msgs.alarmState}:"/
<h outputText id="alarmState" value="#{ModuleBean.alarmState}"/>

<h outputText value="#{msgs.mostSevereAlarm}:"
rendered=#{alarmState.value != 'OK'}" />
<h outputText id="mostSevereAlarm" value="#ModuleBean.mostSevereAlarm}"
rendered=#{alarmState.value != 'OK'}" />


I think it should work.
 
Efrat Bar-Nahum
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, The rendering idea worked!
I rendered the bean value as follows:



Thanks again for the help,
Efrat
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you're controling more than one component you might want to look into the <f:subview>

For example

<h outputText value="#{msgs.alarmState}:"/
<h outputText id="alarmState" value="#{ModuleBean.alarmState}"/>

<f:subview id="onAction" rendered="#{alarmState.value != 'OK'}">
<h outputText value="#{msgs.mostSevereAlarm}" />
<h outputText id="mostSevereAlarm" value="#ModuleBean.mostSevereAlarm}" />

</f:subview>

That way you don't need to put a render tag on ever item.
 
Efrat Bar-Nahum
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great!
Thanks a lot for the tip.

Efrat
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic