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

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?

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:
How can I pass selected row to commandLink inside dataTable?
ViewParam vs #ManagedProperty(value = "#{param.id}")

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

Related

Setting Managed Bean attribute's value from JSF page and using it in bean's methods

I have a ViewScoped Managed Bean. In my .xhtml page I want to set bean's attribute's value and use it in methods in the same bean.
I managed to set the value from jsf page, but when i want to use it in some method the value of an attribute is not the value i have set before.
Description (xhtml):
In this form there is a command link which sets the value of an attribute. And it is working fine. Also, as command link is clicked, second form is being showed.
<h:form>
<h:commandLink value="Set" >
<f:setPropertyActionListener target="#{bean.attribute}" value="true" />
<f:ajax execute="#this" />
</h:commandLink>
</h:form>
This form executes method that uses attribute's value set before, but the value is not true, its false.
<h:form>
<h:commandButton id="submit" value="Execute" action="#{bean.execute}" />
</h:form>
Bean:
public void execute(){
if(isAttribute())
---do something---
}
The question is: Why execute() is not reading attribute's value right?
When I use one form, it's working fine. But I need them to be in separated forms.
The scope of your bean is incorrect. ViewScoped means that the minute the view is changed, the bean is discarded and re-created for the next view. So, in your case, the original data you had for the first view is lost.
I'm going to refer you to BalusC's blog:
http://balusc.blogspot.co.uk/2010/06/benefits-and-pitfalls-of-viewscoped.html
which states:
A #ViewScoped bean will live as long as you're submitting the form to the same view again and again. In other words, as long as when the action method(s) returns null or even void, the bean will be there in the next request. Once you navigate to a different view, then the bean will be trashed
I can't determine of you stay on the same page with both requests. If you do, viewScope should work even in two different forms. If you are navigating from 1 view to another, another viewScope will be created and you will loose the current one.
You could set the value in the sessionScope with java or by annotating the backingNean. But then everything in your backingBean becomes sessionScoped and that might not be needed.
You could also use a spring-like flow scope.
Example to do it with java:
public void callThisAfterFirstClick() {
Faces.setSessionAttribute(attribute, true)
}
public void callThisAfterSecondClick() {
Faces.getSessionAttribute(attribute);
}

JSF pass Object to another page

I'm trying to pass an object to another site. So i have my Gallery.xhtml where I set an object as current and redirect to another page:
<h:form>
<a4j:commandButton value="Edit Skin"
action="#{helloBean.setCurrentSkin(skin)}"
onclick="window.location.href = 'resources/html/Editor.xhtml';" />
</h:form>
But when the getter on the second page is called, the current object is null again.
Is the bean generated for each page? How could I achieve this?
My Problem was that I declared my bean as #ViewScoped, but the proper Tag is #SessionScoped. This makes sure that the Bean holds its values for the whole session and not only one view.

h:commandButton action method not called on request scoped bean

I have a h:commandButton on a jsf page. And it needs to be rendered based on a condition. The property on the condition is a hidden input to the page. The action method on the button is not called when the rendering condition is specified.
Any ideas?
here is the sample code:
<h:commandButton value="Button"
action="#{bean.method}"
rendered="#{bean.conditon}"
type="submit"/>
<h:inputHidden value="#{bean.condition}" />
I understand that your bean is request scoped, otherwise you wouldn't have this problem. This is a timing problem.
The rendered attribute is also determined during "apply request values" phase of JSF lifecycle. However, the submitted values are only been set in the model during "update model values" phase of JSF lifecycle, which is later. Thus, when rendered attribute is evaluated, it doesn't get the submitted value from the hidden input, but instead the property's default value.
If it's not an option to change the request scope to the view scope, then you'd need to salvage this problem differently. One of the simplest ways changing the <h:inputHidden> to be a <f:param> and inject the value via #ManagedProperty on #{param} map:
<h:commandButton value="Button"
action="#{bean.method}"
rendered="#{bean.conditon}"
>
<f:param name="condition" value="#{bean.condition}" />
</h:commandButton>
(note that I omitted type="submit" as it's the default already)
with
#ManagedProperty("#{param.condition}")
private boolean condition;
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated - point 6

How to get p:inputText value and set it as f:param value?

I have an xhtml page as follows
<p:inputText id="inputFilterKey" name="inputFilterKey" value="#{key}" />
<p:commandButton id="filterByKey" action="searchByKey" value="Search" ajax="false">
<f:param name="filterKey" value=? />
</p:commandButton>
The parameter 'filterKey' should have the value which is provided by user in the inputText. Value '#{key}' is the flow scope variable which is defined in spring webflow. That is, it is not taken from a back bean. How should I get the value of the inputText? Here is the flow definition in case it is need.
<transition on="searchByKey" to="editTexts" >
<set name="flowScope.key" value="requestParameters.filterKey"/>
<evaluate expression="textManager.searchByKey(key)" result="viewScope.textsByKey" result-type="dataModel"/>
</transition>
Thanks
That isn't possible. The <f:param value> is evaluated when the form is rendered/displayed, not when the form is submitted/processed.
I'm not familiar with Spring Webflow, but this is IMO a really strange design. You might want to confirm with the SWF guys if you're doing things the right way. Perhaps you should rather inject the SWF variable as a managed bean property during its construction/initialization or something?
Anyway, there are ways to get the submitted value without binding the input component's value to a managed bean property. One of them is just getting it straight from the request parameter map by #ManagedProperty:
#ManagedProperty("#{param['formId:inputFilterKey']}")
private String key; // +setter
or when the managed bean has a broader scope than the request scope:
String key = externalContext.getRequestParameterMap().get("formId:inputFilterKey");
The formId:inputFilterKey is just the name of the generated HTML <input> element representation of the <p:inputText> component.

h:inputText value not set in bean

The <h:inputText> value is not been set in a request scoped bean. If the value is preserved from DB, it works fine.
view:
<h:inputText id="receipient" size="90" styleClass="text" readonly="#{!bean.enable}" value="#{bean.recipient}" />
bean:
public class Bean {
private String recipient;
Bean(){
recipient = //load from db
}
//set
//get
}
What is the problem and how can I solve this?
Ensure that readonly="#{!bean.enable}" evaluates the same as it was in initial view. If it evaluates true during apply request values phase of the form submit, then the component's value won't be processed (because it is marked as read only).
An easy test is to put the bean in session scope. If that fixes it, then you know the culprit. If you're using JSF 2.0, just put bean in view scope by using #ViewScoped instead of #RequestScoped. If you're still on JSF 1.x, use Tomahawk's <t:saveState> component to let the bean act like the JSF 2.0 view scoped one.
<t:saveState value="#{bean}" />

Resources