我知道这是一个常见的错误,并且有很多问题,我厌倦了从很多地方实施,我将介绍重大错误。
Screenshot Here 我想选择一个客户端&来自selectOneMenu的产品,并使用其他输入将它们插入到另一个表中 正如你在这里看到的客户端和产品被选中但当我尝试添加它时返回为exeption值是必需的! 我试图解决这个问题:
Validation Error: Value is not valid
这是我的jsf页面:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="Create/Edit"/></h1>
<h:panelGrid columns="2">
<h:outputLabel value="Client:" for="client" />
<h:selectOneMenu id="client" value="#{recBean.rec.client}" converter="#{clientConverter}" title="Client" required="true" >
<!-- TODO: fixed -->
<f:selectItems value="#{CliBean.lister()}" var="C" itemLabel="#{C.nomCli}" itemValue="#{C}" />
</h:selectOneMenu>
<h:outputLabel value="Produit:" for="produit" />
<h:selectOneMenu id="produit" value="#{recBean.rec.produit}" converter="#{produitConverter}" title="Produit" required="true" >
<!-- TODO: fixed -->
<f:selectItems value="#{proBean.lister()}" var="P" itemLabel="#{P.libProd}" itemValue="#{P}"/>
</h:selectOneMenu>
<h:outputLabel value="DateCreRec:" for="dateCreRec" />
<h:inputText id="dateCreRec" value="#{recBean.rec.dateCreRec}" title="DateCreRec" required="true" requiredMessage="The DateCreRec field is required.">
<f:convertDateTime pattern="MM/dd/yyyy" />
</h:inputText>
<h:outputLabel value="TypeRec:" for="typeRec" />
<h:inputText id="typeRec" value="#{recBean.rec.typeRec}" title="TypeRec" required="true" requiredMessage="The TypeRec field is required."/>
<h:outputLabel value="DescCli:" for="descCli" />
<h:inputText id="descCli" value="#{recBean.rec.descCli}" title="DescCli" required="true" requiredMessage="The DescCli field is required."/>
<h:outputLabel value="Situation:" for="situation" />
<h:inputText id="situation" value="#{recBean.rec.situation}" title="Situation" required="true" requiredMessage="The Situation field is required."/>
<h:outputLabel value="PersEstim:" for="persEstim" />
<h:inputText id="persEstim" value="#{recBean.rec.persEstim}" title="PersEstim" required="true" requiredMessage="The PersEstim field is required."/>
<h:outputLabel value="Estimation:" for="estimation" />
<h:inputText id="estimation" value="#{recBean.rec.estimation}" title="Estimation" required="true" requiredMessage="The Estimation field is required."/>
<h:outputLabel value="DegUrgence:" for="degUrgence" />
<h:inputText id="degUrgence" value="#{recBean.rec.degUrgence}" title="DegUrgence" required="true" requiredMessage="The DegUrgence field is required."/>
<h:outputLabel value="DescExpert:" for="descExpert" />
<h:inputText id="descExpert" value="#{recBean.rec.descExpert}" title="DescExpert" required="true" requiredMessage="The DescExpert field is required."/>
<h:outputLabel value="DateDebut:" for="dateDebut" />
<h:inputText id="dateDebut" value="#{recBean.rec.dateDebut}" title="DateDebut" required="true" requiredMessage="The DateDebut field is required.">
<f:convertDateTime pattern="MM/dd/yyyy" />
</h:inputText>
<h:outputLabel value="DateFin:" for="dateFin" />
<h:inputText id="dateFin" value="#{recBean.rec.dateFin}" title="DateFin" required="true" requiredMessage="The DateFin field is required.">
<f:convertDateTime pattern="MM/dd/yyyy" />
</h:inputText>
<h:commandButton value="Add" action="#{recBean.ajouter()}"/>
</h:panelGrid>
</h:form>
</f:view>
</h:body>
</html>
这是托管bean:
@ManagedBean(name="CliBean")
@SessionScoped
public class ClientBean {
ClientDAO dao = new ClientDAOImpl();
public Client cli = new Client();
private String logcli;
public Client getCli() {
return cli;
}
public void setCli(Client cli) {
this.cli = cli;
}
public List<Client> lister(){
return dao.findAll();
}
@Override
public String toString() {
return String.valueOf(cli.getIdCli());
}
public String LoginCheck(){
Session ses = HibernateUtil.getSession();
SessionFactory fac = HibernateUtil.getSessionFactory();
ses.getTransaction();
List<Client> list = ses.createSQLQuery("select * from client where login_cli='" + cli.getLoginCli() + "' and pwd_cli='" + cli.getPwdCli() + "'").list();
if (list.size() > 0) {
//servlet session part
logcli = cli.getLoginCli();
HttpSession hs = sessionUtil.getSession();
hs.setAttribute("logcli",logcli);
//servlet session part
//BootFaces
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Congratulations! You've successfully logged in.");
FacesContext.getCurrentInstance().addMessage("loginForm:password", msg);
//BootFaces
return "/success.xhtml?faces-redirect=true";
} else {
//FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "client not found", ""));
//BootFaces
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "", "client not found !");
FacesContext.getCurrentInstance().addMessage("loginForm:password", msg);
//BootFaces
}
try {
ses.getTransaction().commit();
} catch (Exception e) {
}
ses.close();
return null;
}
public String afflogin(){
return logcli;
}
public String doLogout(){
HttpSession hs = sessionUtil.getSession();
hs.invalidate();
return "/index.xhtml";
}
}
最后转换器:
@ManagedBean
@RequestScoped
@FacesConverter(forClass=Client.class)
public class ClientConverter implements Converter{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String codeString) {
if (codeString == null && codeString.trim().length() > 0) {
Integer code = Integer.valueOf(codeString);
ClientServicesImpl clientServicesImpl = new ClientServicesImpl();
return clientServicesImpl.searchForCode(code);
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object clientObject) {
if (clientObject == null) {
return "";
}
if (clientObject instanceof Client) {
Client client = (Client) clientObject;
return client.getIdCli().toString();
//return client.getIdCli() != null ? String.valueOf(client.getIdCli()) : null;
//return String.valueOf(((Client) clientObject).getIdCli());
} else {
throw new ConverterException(new FacesMessage(clientObject + " is not a valid Client"));
}
}
}
请注意: 等于()及已为实体客户端和产品生成hashCode()。
我认为主要问题来自转换器。 在发布我的问题之前,我也看到了几乎所有关于selectOneMenu的帖子。 我为此浪费了3天我不想浪费更多时间