This question already has answers here:
Validation Error: Value is not valid

(3个答案)


5年前关闭。





我知道这是一个常见的错误,对此有很多疑问,但是我必须创建一个新的错误。

这是我的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)。我想念什么吗?

ps对不起,我的英语,希望您能理解我的问题。

几乎忘了提到我重写了equals和hashCode方法,并且我确定它是可以的。

最佳答案

指定itemValue<f:selectItems>和实现转换器的getAsString()的方式不正确。
提交选定的值后,JSF将通过转换器的cityIDCity转换为getAsObject()。然后,JSF将对照CityitemValue验证<f:selectItems>来查看它是否确实在列表中(这是为了避免黑客修改/篡改具有不同值的请求)。但是,由于itemValuecityID,即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;
}

总结:itemValue的类型必须与下拉列表value的类型完全相同,转换器必须在该类型和字符串之间进行转换。
也可以看看:

Validation Error: Value is not valid

07-26 00:49