JSF 属性从支持 bean A 转移到支持 bean B
目前,我正在深入研究 JSF 2.0,但对托管 bean 属性从一个视图到另一个视图的“传输”缺乏一点了解。我进行了一些搜索,但没有找到一个真正好的例子,所以如果有人能给我指出一个教程或解释一下这些事情,我将非常感激。
这是我的场景:
我正在开发一个小型游乐场日历应用程序。第一个视图 select.xhtml
包含日历选择器,用户可以在其中选择特定日期:
<html>
...
<h:form>
<!-- Calendar selector from primefaces -->
<p:calendar value="#{calendarSelect.date}" mode="inline" navigator="true" />
<p:commandButton value="Show entries for date" action="day" />
...
我相应的支持 bean 如下所示:
@ManagedBean(name="calendarSelect")
@RequestScoped
public class CalendarSelectComponent {
private Date date = null;
... // Getters and setters
现在,当我从 select.xhtml
提交表单时code> 我被转发到 day.xhtml
<html>
...
<h:form>
The current day ist:
<h:outputText value="#{calendarEdit.date}">
<f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
支持 bean 现在看起来像这样:
@ManagedBean(name="calendarEdit")
@ViewScoped
public class CalendarEditComponent implements Serializable {
private Date date = null;
private CalendarEntryBean currentEntry = null;
private List<CalendarEntryBean> allEntries = null;
....
我现在正在尝试解决问题:如何从选择器到编辑器?
我尝试了多种选项,其中之一是:
<p:commandButton value="Show entries for date" action="day" />
<f:setPropertyActionListener target="#{calendarEdit.date}" value="#{calendarSelect.date}" />
</p:commandButton>
调试器显示,事实上,calendarEdit
的 date
属性是用 calendarSelect 中的值填充的
,但由于 day.xhtml
是一个新视图,因此正在创建一个新的 CalendarEditComponent
支持 bean,而不是我用来自select
视图中的选择器。
我读到,一种解决方案是创建一个保留所有值的 SessionScoped
支持 bean。但这不是我认为它应该工作的方式,因为我并不真正需要会话中的信息,我只是希望它从 A“旅行”到 B。基于会话的方法是我每个会话只能使用一个选择器和一个编辑器 - 如果您考虑多窗口浏览等,我认为这是不可接受的。
我真的不认为我是第一个遇到这种情况的人,并且我确信 JSF 为此提供了一个优雅的解决方案,但我一直无法找到该解决方案。
所以再一次,如果有人知道如何解决这个问题 - 我正在听! ;-)
I'm getting deeper into JSF 2.0 at the moment and lacking a bit of understanding about the "transport" of managed bean properties from one view to the other. I searched a bit but haven't found a really good example, so if anyone could point me to a tutorial or explain the things a little bit I'd really grateful.
So here is my scenario:
I'm developing a small playground calendar application. The first view select.xhtml
contains the calendar selector, where the user can pick a specific date:
<html>
...
<h:form>
<!-- Calendar selector from primefaces -->
<p:calendar value="#{calendarSelect.date}" mode="inline" navigator="true" />
<p:commandButton value="Show entries for date" action="day" />
...
My corresponding backing bean looks like this:
@ManagedBean(name="calendarSelect")
@RequestScoped
public class CalendarSelectComponent {
private Date date = null;
... // Getters and setters
Now when I submit the form from select.xhtml
I'm forwarded to day.xhtml
<html>
...
<h:form>
The current day ist:
<h:outputText value="#{calendarEdit.date}">
<f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
The backing bean now looks like this:
@ManagedBean(name="calendarEdit")
@ViewScoped
public class CalendarEditComponent implements Serializable {
private Date date = null;
private CalendarEntryBean currentEntry = null;
private List<CalendarEntryBean> allEntries = null;
....
I am now trying to solve the problem: How do I transfer the date
parameter from the selector to the editor?
I've tried a number of options, one was this:
<p:commandButton value="Show entries for date" action="day" />
<f:setPropertyActionListener target="#{calendarEdit.date}" value="#{calendarSelect.date}" />
</p:commandButton>
A debugger shows, that indeed, the date
property of the calendarEdit
is populated with the value from calendarSelect
, but since day.xhtml
is a new view, a new CalendarEditComponent
backing bean is being created and not the one I've populated with the date from the selector in the select
view.
I've read that one solution would be to create a SessionScoped
backing bean that does retain all it's values. But this is not the way I think it's supposed to work, because I don't really need the information in the session, I simply want it to "travel" from A to B. Another downside with the session based approach is that I can only use one selector and one editor per session - which I think isn't acceptible if you think of multi window browsing and so on.
I really don't think I'm the first one encountering such a scenario and I'm sure that JSF provides an elegant solution for this but I haven't been able to find that solution.
So once again, if anyone knows how to approach this - I'm listening! ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在表单提交的调用操作阶段执行。因此,它预计此时该值仍然存在。但由于您的选择 bean 是请求范围的,因此在表单提交期间它不再存在。您希望传递一个请求参数,该参数在渲染响应期间内联到输出中。您可以使用
来完成此操作。它将作为请求参数提供(请注意,由于 HTTP 的性质,它只理解字符串)。您可以让 JSF 将请求参数设置为托管属性,但由于您的编辑 bean 是视图范围的,因此使用
@ManagedProperty
无法实现这一点。您必须通过ExternalContext
自行收集它。确实,这很笨拙。我将为此使用相同的 bean 和视图,并通过
rendered
属性切换选择/编辑表单的可见性。毕竟,编辑视图不能通过简单的 GET 直接打开/添加书签,不是吗? ;)The
<f:setPropertyActionListener>
is executed during invoke action phase of the form submit. So it expects that the value is still there at that point. But since your select bean is request scoped, it isn't there during form submit anymore. You want instead to pass a request parameter which get inlined in the output during render response. You can do this with<f:param>
.It'll be available as request parameter (note that it only understands Strings, due to the nature of HTTP). You could let JSF set request parameters as managed properties, but since your edit bean is view scoped, this isn't possible with
@ManagedProperty
. You've got to gather it yourself byExternalContext
.True, that's clumsy. I would just have used the same bean and view for this and toggle visibility of select/edit forms by
rendered
attribute. The edit view is after all not directly openable/bookmarkable by a simple GET, isn't it? ;)