Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jsf - f:setPropertyActionListener sets null value instead of intended value

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...