如何在 JSF 中正确使用组件绑定?(会话作用域 bean 中的请求作用域组件) [英] How to use component binding in JSF right ? (request-scoped component in session scoped bean)

查看:29
本文介绍了如何在 JSF 中正确使用组件绑定?(会话作用域 bean 中的请求作用域组件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mojara 2.1.21

Mojara 2.1.21

我已经根据评论更新了我的问题.我有两种情况,其中一个组件绑定到服务器会话 bean.(包含信息的附加链接:绑定属性导致在视图中发现重复的组件 IDhttps://stackoverflow.com/a/12512672/2692917)

I've updated my question based on comments. I have two situation where a component is bound to server session bean. (Additional links with information: Binding attribute causes duplicate component ID found in the view and https://stackoverflow.com/a/12512672/2692917)

版本 1:

single.xhtml:

single.xhtml:

 <h:outputText value=... binding="#{mysessionbean.out}" />

Java:

 @SessionScoped @Named public class Mysessionbean {
    UIOutput out;
    //getter and setter ....
 }

版本 2:

template.xhtml:

template.xhtml:

 <h:outputText value=... binding="#{mysessionbean.out}"

view1.xhtml:

view1.xhtml:

 <ui:composition template="template.xhtml" />

view2.xhtml:

view2.xhtml:

 <ui:composition template="template.xhtml" />

Java:

 @SessionScoped @Named public class Mysessionbean {
    UIOutput out;
    //getter and setter ....
 }

版本 1 没问题.(至少到目前为止我还没有遇到任何错误).但是在版本 2 中,如果我从一个页面导航到另一个页面,则会发生重复的 id 错误.为什么会发生?将(请求范围的)组件(在版本 1 中)与会话范围的绑定一起使用是否安全?还有其他用例需要考虑吗?

Version 1 is ok. (At least I've not encounter any errors so far). But in version 2 the duplicate id error is occured if I navigate from one page to another. Why does it happen ? Is it safe to use (request-scoped) component (in version 1) with session scoped binding ? Are there another use cases to consider ?

功能要求 1:

我想在视图中使用 Primefaces 数据表.我需要这个数据表中的一些信息.(如选定行或行索引).所以绑定数据表可以帮助我检索这些信息.

I want to use Primefaces datatable in a view. I need some info from this datatable. (Such as selected row or row index). So binding the datatable helps me to retrieve this info.

功能要求 2:

复合组件中的组件绑定.它们将绑定到会话范围的 bean.(并且主要在一个页面上使用,但如果我在另一个页面上使用它呢?

Components binding in composite components. They will be bound to session scoped bean. (And used mainly on one page, but what if I used it on another page ?

要求 3

版本 2"中的情况.带有primefaces 菜单和会话范围绑定的模板.为此,我使用了 EL 绑定.

The situation as in "Version 2". Template with primefaces menu and session scoped binding. For this I've used the EL-Binding.

推荐答案

在 JSF 2.x 中,除非您想以编程方式操作组件(这本身也相当可疑),否则没有合理的现实世界用例来将组件绑定到支持 bean.如果它们没有在支持 bean 本身中进一步使用,或者仅仅是它们的属性被扁平化,则肯定不会.

In JSF 2.x, unless you want to manipulate components programmatically (which is at its own also rather fishy), there is no sensible real world use case to bind components to a backing bean. For sure not if they are further not been used in the backing bean itself, or if it are solely their attributes which are been flattened out.

关于获取数据表当前行的功能需求,这里列举了很多更好的方法,如何将选定的行传递给 dataTable 内的 commandLink?,例如,如果您的环境支持 EL 2.2:

As to the functional requirement of getting the current row of the data table, there are much better ways listed here, How can I pass selected row to commandLink inside dataTable?, for example if your environment supports EL 2.2:

<h:dataTable value="#{bean.items}" var="item">
    <h:column>
        <h:commandLink value="Foo" action="#{bean.foo(item)}" />

<小时>

最后两个要求完全不清楚.至少,如果您正在执行以下操作:


The two last requirements are totally unclear. At least, if you're doing something like:

<x:someComponent binding="#{bean.someComponent}" />

with in bean

with in bean

someComponent.setSomeAttribute(someAttribute);
someComponent.setOtherAttribute(otherAttribute);

那你应该去做

<x:someComponent someAttribute="#{bean.someAttribute}" otherAttribute="#{bean.otherAttribute}" />

<小时>

或者,如果您打算像这样在视图中的其他地方使用该组件


Or, if you intend to be able to use the component somewhere else in the view like so

<h:inputText ... required="#{not empty param[bean.save.clientId]}" />
...
<h:commandButton binding="#{bean.save}" ... />

并且实例在 bean 中进一步没有被使用,然后完全摆脱不必要的属性:

and the instance is further nowhere been used in the bean, then just get rid of the unnecessary property altogether:

<h:inputText ... required="#{not empty param[save.clientId]}" />
...
<h:commandButton binding="#{save}" ... />

<小时>

如果由于某些不清楚的原因真的,真的没有办法,那么将会话范围 bean 的所有请求范围属性拆分为一个单独的请求范围 bean,您又将其绑定到表单操作.会话范围的可以作为请求范围的 @ManagedProperty 注入.

这篇关于如何在 JSF 中正确使用组件绑定?(会话作用域 bean 中的请求作用域组件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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