有条件地显示JSF组件 [英] Conditionally displaying JSF components
问题描述
首先,我是Java EE的新手,来自强大的ASP .NET开发背景. 我已经上网了,可能会错过这一点,但是似乎没有关于如何将后备bean类连接到JSF组件的简单直接的教程.
First, I am new to Java EE, came from a strong ASP .NET development background. I have gone through the net, and I might miss this but it seems like there is no simple and straight-to-the-point tutorials on how I could connect backing bean class to a JSF components.
一个很好的例子是这样的,当前我正在尝试创建一个JSF页面,其中有一组链接,如菜单栏和一组表单.我打算做的是,当单击链接时,将呈现一个特定的表单.
A good example is like this, currently I am trying to create a JSF page where there is a set of links as menu bar and a set of forms. What I am planning to do is, when clicking a link, a particular form will be rendered.
在ASP.NET中,我可以轻松地检索元素,然后将属性设置为可显示.我想知道在JSF中是否有简单的方法(麻烦,甚至任何方法)来做到这一点.
In ASP.NET, I could easily retrieve the element and then set the attribute to be displayable. I am wondering if there is easy way (heck, even any way) to do this in JSF.
表单已经在页面中,只需在单击特定链接时将"render"属性设置为true即可.
The forms is already in the page, it is just a matter of setting the "render" attribute to true when I click a particular link.
推荐答案
是的,请使用rendered
属性.
<h:form rendered="#{some boolean condition}">
通常将其绑定到模型,而不是让模型抓住组件并对其进行操作.
You usually tie it to the model rather than letting the model grab the component and manipulate it.
例如
<h:form rendered="#{bean.booleanValue}" />
<h:form rendered="#{bean.intValue gt 10}" />
<h:form rendered="#{bean.objectValue eq null}" />
<h:form rendered="#{bean.stringValue ne 'someValue'}" />
<h:form rendered="#{not empty bean.collectionValue}" />
<h:form rendered="#{not bean.booleanValue and bean.intValue ne 0}" />
<h:form rendered="#{bean.enumValue eq 'ONE' or bean.enumValue eq 'TWO'}" />
请注意基于关键字的EL操作符的重要性例如gt
,ge
,le
和lt
,而不是>
,>=
,<=
和<
,因为尖括号<
和>
是XML中的保留字符.另请参阅以下相关问答:
Note the importance of keyword based EL operators such as gt
, ge
, le
and lt
instead of >
, >=
, <=
and <
as angle brackets <
and >
are reserved characters in XML. See also this related Q&A: Error parsing XHTML: The content of elements must consist of well-formed character data or markup.
对于您的特定用例,我们假设链接传递的参数如下:
As to your specific use case, let's assume that the link is passing a parameter like below:
<a href="page.xhtml?form=1">link</a>
然后您可以显示如下表格:
You can then show the form as below:
<h:form rendered="#{param.form eq '1'}">
(#{param}
是一个隐式EL对象,指向表示请求参数的Map
)
(the #{param}
is an implicit EL object referring to a Map
representing the request parameters)
- Java EE 6 tutorial - Expression Language
- How to show JSF components if list is not null and has size() > 0
- Why do I need to nest a component with rendered="#{some}" in another component when I want to ajax-update it?
这篇关于有条件地显示JSF组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!