简体   繁体   English

如何在JSF中使用组件绑定? (会话范围bean中的请求范围的组件)

[英]How to use component binding in JSF right ? (request-scoped component in session scoped bean)

Mojara 2.1.21 Mojara 2.1.21

I've updated my question based on comments. 我根据评论更新了我的问题。 I have two situation where a component is bound to server session bean. 我有两种情况,组件绑定到服务器会话bean。 (Additional links with information: Binding attribute causes duplicate component ID found in the view and https://stackoverflow.com/a/12512672/2692917 ) (附加链接信息: 绑定属性导致在视图中找到重复的组件ID,并且https://stackoverflow.com/a/12512672/2692917

Version 1: 版本1:

single.xhtml: single.xhtml:

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

java: Java的:

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

Version 2: 版本2:

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: Java的:

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

Version 1 is ok. 版本1没问题。 (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. 但是在版本2中,如果我从一个页面导航到另一个页面,则会出现重复ID错误。 Why does it happen ? 为什么会这样? Is it safe to use (request-scoped) component (in version 1) with session scoped binding ? 使用(请求范围的)组件(在版本1中)与会话作用域绑定是否安全? Are there another use cases to consider ? 还有其他用例需要考虑吗?

Edit: Functional requirement 1: 编辑: 功能要求1:

I want to use Primefaces datatable in a view. 我想在视图中使用Primefaces数据表。 I need some info from this datatable. 我需要来自这个数据表的一些信息。 (Such as selected row or row index). (例如选定的行或行索引)。 So binding the datatable helps me to retrieve this info. 因此绑定数据表有助于我检索此信息。

Functional requirement 2: 功能要求2:

Components binding in composite components. 组件在复合组件中的绑定。 They will be bound to session scoped bean. 它们将绑定到会话范围的bean。 (And used mainly on one page, but what if I used it on another page ? (主要用在一个页面上,但是如果我在另一个页面上使用它会怎么样?

Requirements 3 要求3

The situation as in "Version 2". “版本2”中的情况。 Template with primefaces menu and session scoped binding. 带有primefaces菜单和会话作用域绑定的模板。 For this I've used the EL-Binding. 为此,我使用了EL-Binding。

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. 在JSF 2.x中,除非你想以编程方式操作组件(这本身也很狡猾),没有合理的现实世界用例将组件绑定到支持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. 当然,如果它们未被进一步用于支持bean本身,或者仅仅是它们的属性已被展平,那么肯定不会。


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? 至于获取数据表的当前行的功能要求,这里列出了更好的方法, 如何将选定的行传递给dataTable中的commandLink? , for example if your environment supports EL 2.2: 例如,如果您的环境支持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 在豆里

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

then you should instead be doing 然后你应该做

<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}" ... />

and the instance is further nowhere been used in the bean, then just get rid of the unnecessary property altogether: 并且实例在bean中进一步使用,然后完全摆脱不必要的属性:

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

If there is really, really no way for some unclear reason, then split all request scoped properties of the session scoped bean out into a separate request scoped bean which you in turn bind to form actions. 如果确实存在某些不明原因的确无法解决,那么将会话范围bean的所有请求范围属性拆分为单独的请求范围bean,然后将其绑定到表单操作。 The session scoped one can just be injected as a @ManagedProperty of the request scoped one. 会话作用域可以只作为请求作用域的@ManagedProperty注入。


See also: 也可以看看:

We ran into a similar problem and I just want to share our solution: 我们遇到了类似的问题,我只想分享我们的解决方案:

Problem: In a view there was a (extended largely customized) datatable. 问题:在视图中有一个(扩展的大部分定制)数据表。

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

After navigating to another page and back we wanted the datatable to have the exact same state. 导航到另一个页面后,我们希望数据表具有完全相同的状态。 Previously we solved that by binding the datatable to to backing bean. 以前我们通过将数据表绑定到支持bean来解决这个问题。 This worked fine with JSPs. 这适用于JSP。 With Facelets we could not do that (Duplicate ID errors). 使用Facelets,我们无法做到这一点(重复ID错误)。 So we used the binding, but only saved/restored the state of the datatable component. 所以我们使用了绑定,但只保存/恢复了数据表组件的状态。

public HtmlDataTable getSomeDataTable()
{
 HtmlDataTable htmlDataTable = new HtmlDataTable();
 if (tableState != null)
   htmlDataTable.restoreState(FacesContext.getCurrentInstance(), tableState);
 return htmlDataTable;
}

public void setSomeDataTable(HtmlDataTable table)
{
  tableState = table.saveState(FacesContext.getCurrentInstance());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM