3

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

4

1 回答 1

2

在表单提交的<f:setPropertyActionListener>调用操作阶段执行。因此,它预计该值在那时仍然存在。但是由于您的选择 bean 是请求范围的,因此在表单提交期间它不再存在。相反,您希望传递一个请求参数,该参数在呈现响应期间内联在输出中。您可以使用<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

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

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

于 2011-02-18T12:19:03.457 回答