我知道这是一个常见错误,并且有很多关于它的问题,但我必须创建一个新错误。
这是我的 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() 一切都很好,打印找到的城市 (grad) 的名称 (Ime)。我错过了什么吗?
附言对不起我的英语,我希望你能理解我的问题。
差点忘了说我重写了equals和hashCode方法,我确定没问题。
最佳答案
你指定itemValue
的<f:selectItems>
和实现转换器的getAsString()
的方式不对。
提交选定的值时,JSF将通过转换器的cityID
将Ojit_code转换为Ojit_code。然后,JSF 将根据 City
的 getAsObject()
验证 City
以查看它是否真的是列表的一部分(这是为了避免黑客修改/篡改具有不同值的请求)。但是,由于 itemValue
是 <f:selectItems>
,它是一个 itemValue
,因此列表中的每个项目的比较都失败了。因此出现 cityID
错误。
您需要将 Integer
改为 Value not valid
并更改 itemValue
以返回 City
:
<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;
}
总结:getAsString()
的类型必须与下拉 cityID
的类型完全相同,并且转换器必须在该类型和字符串之间进行完全转换。
另见:
关于jsf - 验证错误值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7096632/