JSF属性从后备bean A转移到后备bean B [英] JSF property transfer from backing bean A to backing bean B

查看:109
本文介绍了JSF属性从后备bean A转移到后备bean B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我对JSF 2.0有了更深入的了解,并且对托管bean属性从一个视图到另一个视图的传输"缺乏一点了解.我进行了一些搜索,但没有找到一个很好的例子,因此,如果有人可以将我指向教程或对它们进行一些解释,我将不胜感激.

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.

这是我的情况:

我正在开发一个小型游乐场日历应用程序.第一个视图select.xhtml包含日历选择器,用户可以在其中选择特定日期:

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

我相应的支持bean看起来像这样:

My corresponding backing bean looks like this:

@ManagedBean(name="calendarSelect")
@RequestScoped
public class CalendarSelectComponent {

  private Date date = null;

  ... // Getters and setters

现在,当我从select.xhtml提交表单时,我将被转发到day.xhtml

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;

  ....

我现在正在尝试解决问题:如何将date参数从选择器传输到编辑器?

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>

调试器确实显示calendarEditdate属性填充了calendarSelect中的值,但是由于day.xhtml是新视图,因此正在创建新的CalendarEditComponent支持bean而不是我从select视图中的选择器填充日期的那个.

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.

我已经读过一种解决方案是创建一个SessionScoped支持bean,该bean确实保留其所有值.但这不是我认为应该起作用的方式,因为我真的不需要 session 中的信息,我只是希望它从A到B旅行".基于会话的方法是每个会话只能使用一个选择器和一个编辑器-如果您想到多窗口浏览等等,我认为这是不可接受的.

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.

我真的不认为我是遇到这种情况的第一个人,并且我敢肯定JSF为此提供了一个优雅的解决方案,但是我找不到该解决方案.

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! ;-)

推荐答案

<f:setPropertyActionListener>在表单提交的调用操作阶段执行.因此,它希望那时的价值仍然存在.但是由于您的选择bean是请求范围的,因此在表单提交期间不再存在.相反,您希望传递一个请求参数,该参数在渲染响应期间内联到输出中.您可以使用<f:param>进行此操作.

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

<p:commandButton value="Show entries for date" action="day" />
  <f:param name="date" value="#{calendarSelect.dateAsString}" />
</p:commandButton>

它将作为请求参数使用(请注意,由于HTTP的特性,它只能理解字符串).您可以让JSF将请求参数设置为托管属性,但是由于您的编辑bean是视图作用域的,因此@ManagedProperty不可能做到这一点.您必须通过ExternalContext自己收集它.

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 by ExternalContext.

String dateAsString = externalContext.getRequestParameterMap().get("date");


是的,这很笨拙.我只需要使用相同的bean和视图,并按rendered属性切换选择/编辑表单的可见性.毕竟,编辑视图不能通过简单的GET直接打开/添加书签,不是吗? ;)


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? ;)

这篇关于JSF属性从后备bean A转移到后备bean B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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