Reputation: 3290
Do ViewScope beans allow ajax calls?
For example:
Here is a xhtml page:
<h:body>
<h:selectOneMenu value="#{test.selected}">
<f:selectItem itemValue="2" itemLabel="2" />
<f:selectItem itemValue="3" itemLabel="3" />
<f:ajax listener="#{test.updateData()}" />
</h:selectOneMenu>
</h:body>
Here is the ViewScoped Bean:
@ViewScoped
@Named
public class test implements Serializable{
private String selected;
public void updateData(){
System.out.println("Ajax call successful.");
}
public String getSelected() {
return selected;
}
public void setTest(String test) {
this.selected = selected;
}
}
When I tried this, it didn't work.
Upvotes: 1
Views: 2291
Reputation: 1108732
Actually, you've 3 severe problems in the code posted so far:
The <h:form>
is missing. Not sure though if this is careless preparation of the question or the actual code. The solution is obvious: put grouped UIInput
and UICommand
components in an independent <h:form>
.
In the current JSF version, JSF's scope annotation @javax.faces.bean.ViewScoped
doesn't work on CDI's bean management annotation @Named
.
There are basically 3 solutions:
@ConversationScoped
instead. See also How to replace @ManagedBean / @ViewScope by CDI in JSF 2.0/2.1 for a concrete example. This has however the nasty side effect that it appends a cid
request parameter to every outcome URL.@ViewScoped
to CDI. This has however the nasty side effect that it appends a windowId
request parameter to every single outcome URL.@javax.faces.view.ViewScoped
annotation which ties it with the JSF view state.
This is however hardly the cause of your concrete problem as the bean action should just be invoked, but the bean will just behave like a @RequestScoped
.
The setter method is missing. This should however have thrown a rather self-explaining PropertyNotWritableException
to the server logs when the form is successfully submitted.
There is a fourth possible cause which can't be for sure be confirmed based on the information provided so far: a missing <h:head>
would cause the <f:ajax>
to fail because the required jsf.js
couldn't be auto-included.
Upvotes: 4
Reputation: 5619
You have a wrong setter which causes the error
public void setTest(String test) {
this.selected = selected;
}
This must be
public void setSelected(String test) {
this.selected = test;
}
As a side not, this setter is called for h:selectOneMenu value="#{test.selected}" which is fired when an item is selected on your combo box.
Upvotes: 1