f:setPropertyActionListener 设置空值而不是预期值

发布于 2024-12-15 22:19:13 字数 872 浏览 2 评论 0原文

我的观点是:

<h:commandLink value="BookFlight" action="#{bookSeatController.doLoginOrCC}">
   <f:setPropertyActionListener target="#{bookSeatController.flightNumber}" 
                  value="#{flightInfoController.flight.number}" />
</h:commandLink>

我的 setter 是:

public void setFlightNumber(String flightNumber) {
   this.flightNumber = flightNumber;
}

当我使用调试器时,我在 setter 中得到 flightNumbernull 。但是,如果我将视图更改为以下内容:

<h:commandLink value="BookFlight" action="#{bookSeatController.doLoginOrCC}">
   <f:setPropertyActionListener target="#{bookSeatController.flightNumber}" 
                  value="122334" />
</h:commandLink>

flightNumber 属性设置为 122334。这是如何引起的以及如何解决它以设置预期值而不是 null

My view is:

<h:commandLink value="BookFlight" action="#{bookSeatController.doLoginOrCC}">
   <f:setPropertyActionListener target="#{bookSeatController.flightNumber}" 
                  value="#{flightInfoController.flight.number}" />
</h:commandLink>

My setter is:

public void setFlightNumber(String flightNumber) {
   this.flightNumber = flightNumber;
}

When I use the debugger I get a flightNumber of null in the setter. However, if I change the view to the following:

<h:commandLink value="BookFlight" action="#{bookSeatController.doLoginOrCC}">
   <f:setPropertyActionListener target="#{bookSeatController.flightNumber}" 
                  value="122334" />
</h:commandLink>

The flightNumber property is set to 122334. How is this caused and how can I solve it to set the intended value instead of null?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-12-22 22:19:13

如果 #{flightInfoController.flight.number} 是请求范围的,那么它必须在处理表单提交的请求期间保留完全相同的 flight显示表单的请求。这必须发生在 bean 的(后)构造函数中。

如果这不是一个选项,因为它取决于一些基于请求的变量,那么您最好的选择是将 bean 放在视图范围中(但是我仍然假设您的 bean 设计正确,它不做任何业务/ getter 中的预加载工作)。

如果将 bean 放入视图范围又不是一个选项,那么您需要将其作为完整的请求参数传递。您可以通过 来做到这一点。

<h:commandLink value="BookFlight" action="#{bookSeatController.doLoginOrCC}">
   <f:param name="flightNumber" value="#{flightInfoController.flight.number}" />
</h:commandLink>

您可以让 JSF 通过 BookSeatController 中的 @ManagedProperty 或当前视图中的 来设置它。

另请参阅:

If the #{flightInfoController.flight.number} is request scoped, then it has to preserve exactly the same flight in during the request of processing the form submit as it was during the request of displaying the form. This has to happen in the bean's (post)constructor.

If that is not an option, because it depends on some request based variables, then your best bet is to put the bean in the view scope instead (I however still assume that your bean is properly designed that it doesn't do any business/preloading job in getters).

If putting the bean in the view scope is in turn not an option, then you'd need to pass it as a fullworthy request parameter instead. You can do that by <f:param>.

<h:commandLink value="BookFlight" action="#{bookSeatController.doLoginOrCC}">
   <f:param name="flightNumber" value="#{flightInfoController.flight.number}" />
</h:commandLink>

You can let JSF set it by @ManagedProperty in the BookSeatController or by <f:viewParam> in the current view.

See also:

两人的回忆 2024-12-22 22:19:13

如果它在分配“122334”时工作,但在分配flightInfoController.flight.number时它是“null”并且由于您没有收到任何异常,那么这意味着您的flightInfoController可能是未正确初始化(关于其字段 flight 以及 flight 中的 number)。

只需确保 bean 已正确初始化(或使用 bean 代码更新您的 OP)。

If it's working when assigning "122334" but when assigning flightInfoController.flight.number it's "null" and since you are not receiving any exception, then it means probably your flightInfoController is not properly initialized (regarding it's field flight and hence number in the flight).

Just make sure the bean is properly initialized (or update your OP with the bean code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文