JSF 页面的切换大小写替代方案
除了 c:if
或 c:choose
之外,还有其他更好的方法来实现从多个组件中选择一个组件的条件渲染吗?像 JSF 页面的 switch case 之类的东西?
Other than c:if
or c:choose
, are there any better ways to implement conditional rendering of 1 component out of several components. Something like switch case for JSF pages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,规范的 JSF 方法是使用
rendered
属性。以下是一些示例:与 JSTL 标记的区别在于
rendered
属性在视图渲染期间进行评估,而 JSTL 标记在视图构建期间执行。另请参阅 JSF2 Facelets 中的 JSTL... 有意义吗?那么,如果评估条件所需的变量的范围比视图范围(即请求范围)窄,那么您应该使用
rendered
属性。例如,根据 ajax 请求重新渲染一组组件时。虽然 JSTL 标签在这种情况下可以同样有效,但它们可能被评估“太早”(即在调用操作之前,这可能反过来改变条件)并且它们也会破坏查看范围。另请参阅@ViewScoped
中断在标签处理程序中。如果评估条件所需的变量具有更广泛的范围,例如会话范围或应用程序范围,或者在某些模板客户端中进行了硬编码,那么 JSTL 标记会更有效,因为它们仅在视图构建期间进行评估,而不是每次都进行评估。查看渲染时间。另请参阅 如何制作 JSF 复合网格组件?
The canonical JSF approach for this is using the
rendered
attribute. Here are some examples:The difference with JSTL tags is that the
rendered
attribute is evaluated during view render time, while JSTL tags are executed during view build time. See also JSTL in JSF2 Facelets... makes sense?So, if the variables which are necessary for evaluating the condition have a narrower scope than the view scope (i.e. request scope), then you should be using
rendered
attribute instead. For example, when re-rendering a group of components upon an ajax request. While the JSTL tags could work equally good in this case, they might be evaluated "too early" (i.e. before the action is invoked, which might in turn have changed the conditions) and they would also break the view scope. See also@ViewScoped
breaks in tag handlers.If the variables necessary for evaluating the condition have a broader scope, e.g. session-wide or application-wide or are been hardcoded in some template client, then JSTL tags are more efficient as they will be evaluated during view build time only and not everytime during view render time. See also How to make a grid of JSF composite component?
您可以使用
rendered
属性:它不像实际情况运算符那么清晰,但它是普通的 JSF 中的。
You can use
rendered
attribute:It's not so clear as real case operator, but it is in plain JSF.