有条件地显示 JSF 组件 [英] Conditionally displaying JSF components

查看:32
本文介绍了有条件地显示 JSF 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是 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运算符的重要性,例如gtgelelt 代替 >>=<=< 作为尖括号 <> 被保留XML 中的字符.另请参阅此相关问答:解析 XHTML 时出错:元素的内容必须由格式正确的字符数据或标记组成.

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)

这篇关于有条件地显示 JSF 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆