I know this is a common error, and that there is a lots of questions about it, but I must create new one.
Here is my jsf page:
<h:form id="clientCreatingForm">
<p:selectOneMenu id="city" value="#{kkmBean.client.address.city}" style="width: 263px;"
converter="cityConveter" required="true" requiredMessage="You must choose one city">
<f:selectItems value="#{kkmBean.cityList}" var="city" itemLabel="#{city.cityName}" itemValue="#{city.cityID}"/>
</p:selectOneMenu>
<p:commandButton value="Save client" type="button" onclick="confirm.show()" style="margin-top: 20px; margin-left: 30px; width: 180px;" />
<p:commandButton value="Reset fields" type="reset" style="margin-top: 20px; margin-left: 30px; width: 180px;"/>
<p:confirmDialog message="Are you sure, you want to save client?"
header="Save client" severity="info" widgetVar="confirm">
<p:commandButton value="Yes, I am sure." oncomplete="confirm.hide()"
action="#{kkmBean.saveClient}" update=":warnings" style="font-size: 12px;"/>
<p:commandButton value="No, I don't!" onclick="confirm.hide()" type="button" style="font-size: 12px;"/>
</p:confirmDialog>
</h:form>
Here is managed bean:
@ManagedBean(name = "kkmBean")
@SessionScoped
public class CreatingClientManager implements Serializable {
/** Creates a new instance of CreatingClientManager */
public KreiranjeKorisnikaManager() {
client= new Client();
client.setAddress(new Address());
cityList = new LinkedList<City>();
cityList = Communication.getInstance().getCitySessionBeanRemote().getCities();
}
private Client client;
private List<City> cityList;
public String startCreatingClent() {
return "creating-client";
}
/**
* @return the client
*/
public Klijent getClient() {
return client;
}
/**
* @param client the client to set
*/
public void setClient(Client client) {
this.Client = client;
}
/**
* @return the cityList
*/
public List<Grad> getCityList() {
return cityList;
}
/**
* @param cityList the cityList to set
*/
public void setCityList(List<City> cityList) {
this.cityList = cityList;
}
public String saveClient() {
System.out.println("i am in saveClient method");
String message = Communication.getInstance().getClientSessionBeanRemote.saveClient(client);
FacesContext context = FacesContext.getCurrentInstance();
kontekst.addMessage(message, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success: ", message));
return null;
}
}
And finally converter:
@FacesConverter(forClass=City.class, value="cityConverter")
public class CityConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Integer cityID = Integer.valueOf(value);
CreatingClientManager kkm = (CreatingClientManager) ((HttpSession)context.getExternalContext().getSession(true)).getAttribute("kkmBean");
List<Grad> cityList = kkm.getCityList();
for (int i = 0; i < cityList.size(); i++) {
City city = cityList.get(i);
if(city.getCityID().equals(cityID)) {
System.out.println(city.getCityName());
return city;
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value.toString();
}
}
Of course the problem is in selectOneMenu. When I submit form, i got this annoying error.
I was debugging converter and the line before return grad, i print grad.getImeGrada() and everything is fine, the name (Ime) of found city (grad) is printed. Am I missing something?
p.s. Sorry on my English, I hope that you understand what is my problem.
Almost forgot to mention that i override equals and hashCode methods, and i am sure it is ok.