如何在jsf中基于布尔变量显示两种不同类型的组件? [英] How to display two different types of components based on a boolean variable in jsf?
问题描述
我已经考虑了一段时间-没有一个好主意.
I have thought about this for a while - not getting a good idea.
这是问题所在.我将不得不在文本框中显示文本或将其显示为输出文本.如何在JSF中使用布尔变量来做到这一点?
This is the problem. I will have to display a text either in a text box or as an output text. How do I do this using a boolean variable in JSF?
我不想将h:inputText设为只读/禁用/使其只读,它基于boolean,我想显示一个干净的标签或输出文本.
I do not want to make an h:inputText and disable/make read only, it based on the boolean , I want to display a clean label or an output text.
建议?
推荐答案
使用rendered
属性.它需要一个布尔表达式.
Make use of the rendered
attribute. It takes a boolean expression.
<h:inputText value="#{bean.input}" rendered="#{bean.editmode}" />
<h:outputText value="#{bean.input}" rendered="#{!bean.editmode}" />
如果表达式的计算结果为true
,则组件将在组件树中呈现,并在生成的HTML输出中可见.如果它评估false
,它将不会被呈现,因此不会被发送到HTML输出.
If the expression evaluates true
, the component will be rendered in the component tree and be visible in the generated HTML output. If it evaluates false
, it will not be rendered and thus not be emitted to the HTML output.
还有其他几个示例,您可以使用它们来表达布尔表达式.
Here are several other examples how you could express a boolean expression.
<h:someComponent rendered="#{bean.booleanValue}" />
<h:someComponent rendered="#{bean.intValue > 10}" />
<h:someComponent rendered="#{bean.objectValue == null}" />
<h:someComponent rendered="#{bean.stringValue != 'someValue'}" />
<h:someComponent rendered="#{!empty bean.collectionValue}" />
<h:someComponent rendered="#{!bean.booleanValue && bean.intValue != 0}" />
<h:someComponent rendered="#{bean.stringValue == 'oneValue' || bean.stringValue == 'anotherValue'}" />
这篇关于如何在jsf中基于布尔变量显示两种不同类型的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!