f:setPropertyActionListener 设置空值而不是预期值
我的观点是:
<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 中得到 flightNumber
为 null
。但是,如果我将视图更改为以下内容:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
#{flightInfoController.flight.number}
是请求范围的,那么它必须在处理表单提交的请求期间保留完全相同的flight
显示表单的请求。这必须发生在 bean 的(后)构造函数中。如果这不是一个选项,因为它取决于一些基于请求的变量,那么您最好的选择是将 bean 放在视图范围中(但是我仍然假设您的 bean 设计正确,它不做任何业务/ getter 中的预加载工作)。
如果将 bean 放入视图范围又不是一个选项,那么您需要将其作为完整的请求参数传递。您可以通过
来做到这一点。您可以让 JSF 通过
BookSeatController
中的@ManagedProperty
或当前视图中的
来设置它。另请参阅:
If the
#{flightInfoController.flight.number}
is request scoped, then it has to preserve exactly the sameflight
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>
.You can let JSF set it by
@ManagedProperty
in theBookSeatController
or by<f:viewParam>
in the current view.See also:
如果它在分配“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 yourflightInfoController
is not properly initialized (regarding it's fieldflight
and hencenumber
in theflight
).Just make sure the bean is properly initialized (or update your OP with the bean code).