问题描述
我有一个称为UserSearchHandler
的托管bean,它具有一个doSearch
方法,该方法填充UserSearchHandler.searchResults
,这些方法显示在userSearch.xhtml
页面上的表中.
I have a managed bean called UserSearchHandler
, it has a doSearch
method that populates UserSearchHandler.searchResults
which are displayed in a table on the userSearch.xhtml
page.
我还有一个名为UserHandler
的托管bean,它具有showUser
方法,依此类推.
I have another managed bean called UserHandler
, it has a showUser
method and so on.
在搜索结果表中,用户名是一个链接,当单击该链接时,该链接应显示在userView.xhtml
页面上的用户详细信息.表格和链接如下所示:
In the search results table, the user name is a link that, when clicked, is supposed to show user details on a userView.xhtml
page. The table and link looks like this:
<p:dataTable var="user" value="#{userSearchHandler.searchResults" >
// ... and so on ... then
<h:commandLink value="#{user.firstName}" action="#{userHandler.showUser}">
<f:setPropertyActionListener target="#{userHandler.userIdToShow}" value="#{profile.id}"/>
</h:commandLink>
将托管bean设置为session
范围时,一切正常.
Everything works fine when the managed beans are set to session
scope.
但是,当我将bean的作用域更改为request
时,搜索将起作用并且表将被填充,但是当我单击名称链接时,什么也没有发生.我在userHandler.showUser
方法上设置了一个断点,并且当userSearchHandler
设置为"request"范围时,它从未被击中.
However, when I change the scope on the beans to request
, the search works and the table gets populated, but when I click on the name link nothing happens. I put a break point on the userHandler.showUser
method and it never gets hit when the userSearchHandler
is set to "request" scope.
任何人都可以帮助解释这是为什么还是我做错了吗?
Can anyone help explain why this is, or what I'm doing wrong?
推荐答案
这是因为在新请求期间#{userSearchHandler.searchResults}
为空,因此JSF无法找到相关联的行,在该行中为了执行操作而调用了命令链接(以及传递/设置属性(如果有)).
That's because the #{userSearchHandler.searchResults}
is empty during the new request and therefore JSF is unable to locate the associated row where the commandlink is been invoked in order invoke the action (and to pass/set properties if any).
您需要确保在bean的构造/初始化过程中预先创建相同的#{userSearchHandler.searchResults}
.如果要基于一组特定的参数创建它,那么您还必须将它们与表单一起传递.
You need to ensure that the same #{userSearchHandler.searchResults}
is precreated during bean's construction/initialization. If it's to be created based on a specific set of parameters, then you've to pass them along with the form as well.
这正是为什么存在诸如Tomahawk的<t:saveState />
和新的JSF 2.0视图范围的解决方案的原因.
That's exactly the reason why solutions like Tomahawk's <t:saveState />
and new JSF 2.0 view scope exist.
这篇关于JSF:bean作用域;会话与请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!