valueChangeListener is called after validation and before updating model (in Jsf life cycle), so value is not changed
use :
<h:selectOneMenu id="select" value="#{bean.code}">
<f:selectItems value="#{bean.list}" />
<f:ajax listener="#{bean.setAdress}" />
</h:selectOneMenu>
public void setAdress(AjaxBehaviorEvent event) {
//set value here.
}
or use getNewValue inside valueChangeListener to get new value not by using bean property.
Example:
JSF Controller:
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.event.AjaxBehaviorEvent;
public class Bean {
private String choice;
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
private String code;
private String dest;
private List<String> list;
@PostConstruct
public void init()
{
list=new ArrayList<String>();
list.add("a");
list.add("b");
}
public void setAdress(AjaxBehaviorEvent event) {
//set value here.
if (choice.equals("1")) {
dest = "rererer";
}
System.out.println("code: "+code);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public String getDest() {
return dest;
}
public void setDest(String dest) {
this.dest = dest;
}
}
JSF Page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form id="main">
<table>
<tr>
<td colspan="2"><h:selectOneMenu id="select"
value="#{bean.code}">
<f:selectItems value="#{bean.list}" />
<f:ajax listener="#{bean.setAdress}" render="dest"
execute="select adressChoice" />
</h:selectOneMenu></td>
</tr>
<tr>
<td><h:selectOneRadio id="adressChoice" value="#{bean.choice}">
<f:selectItem id="item1" itemLabel="Post adress" itemValue="1" />
<f:selectItem id="item2" itemLabel="Other" itemValue="2" />
</h:selectOneRadio></td>
</tr>
<tr>
<td><h:inputText id="dest" value="#{bean.dest}" /></td>
</tr>
</table>
</h:form>
</h:body>
</html>
when value changes setAddress will be called.