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

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

问题描述

Mojara 2.1.21

Mojara 2.1.21

我已根据评论更新了我的问题.我有两种情况,其中一个组件绑定到服务器会话bean. (具有以下信息的附加链接:绑定属性会导致在视图中找到重复的组件ID

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-Binding.

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.

关于获取数据表当前行的功能要求,这里列出了许多更好的方法,

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

在豆子中

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,然后将您绑定到表单动作.可以将范围为1的会话作为请求范围1的请求的@ManagedProperty注入.


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. The session scoped one can just be injected as a @ManagedProperty of the request scoped one.

  • Binding attribute causes duplicate component ID found in the view
  • How does the 'binding' attribute work in JSF? When and how should it be used?

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

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