我知道这是一个常见的错误,并且有很多问题,但我必须创建一个新错误。
这是我的jsf页面:
<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>
这是托管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;
}
}
最后转换器:
@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();
}
}
当然问题出在selectOneMenu上。当我提交表格时,我遇到了这个恼人的错误。
我正在调试转换器和返回grad之前的行,我打印grad.getImeGrada(),一切都很好,找到找到的城市(毕业)的名称(Ime)。我错过了什么吗?
P.S。抱歉我的英语,我希望你明白我的问题是什么。
几乎忘了提到我重写了equals和hashCode方法,我确信它没问题。
答案 0 :(得分:10)
您指定itemValue
的{{1}}并实施转换器<f:selectItems>
的方式不正确。
提交所选值后,JSF会将getAsString()
转换为cityID
转换器的City
。然后,JSF将针对getAsObject()
的{{1}}验证City
,看看它是否真的是列表的一部分(这是为了避免黑客修改/篡改具有不同值的请求)。但是,由于itemValue
是<f:selectItems>
itemValue
,因此对于列表中的每个项目,比较都会失败。因此cityID
错误。
您需要将Integer
更改为Value not valid
,然后更改itemValue
以返回City
:
getAsString()
和
cityID
总结:<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}"/>
</p:selectOneMenu>
的类型必须与下拉列表@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
City city = (City) value;
return city.getCityID() != null ? String.valueOf(city.getCityID()) : null;
}
完全相同,转换器必须在该类型和字符串之间进行转换。< / p>