• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Efrat Bar-Nahum
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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
 
Bagwan Mehrat
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • 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.
 
Abhi Raj
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Great!
Thanks a lot for the tip.

Efrat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic