【发布时间】:2016-08-11 12:05:36
【问题描述】:
我有一个简单的表单,有一些输入和一个选择菜单:
<h:panelGroup id="Client">
<h:form id="formClient">
<p:panelGrid columns="3" layout="grid"
columnClasses="labelWidth, ui-grid-col-3">
<p:outputLabel value="CIN:" for="txtCin" />
<h:panelGrid columns="2">
<p:inputText value="#{clientBean.client.identifiant}" id="txtCin"></p:inputText>
<p:commandButton value="Search" process="@parent"
styleClass="orangeButton" update="formClient"
style="margin-top: -10px;" action="#{clientBean.rechercher()}"></p:commandButton>
</h:panelGrid>
<p:message for="txtCin" />
<p:outputLabel value="Nom:" for="txtNom" />
<p:inputText id="txtNom" styleClass="form-control"
value="#{clientBean.client.nomClient}" required="true"
requiredMessage="Le nom est obligatoire" />
<p:message for="txtNom" />
<p:outputLabel value="Type de voie:" for="txtVoie" />
<p:selectOneMenu styleClass="form-control"
value="#{clientBean.client.typeVoie}" id="txtVoie">
<f:selectItem itemLabel="Selectionnez voie..." itemValue="" />
<f:selectItems value="#{clientBean.typeVoies}" var="typeVoie" itemLabel="#{typeVoie.libelle}" itemValue="#{typeVoie}" />
</p:selectOneMenu>
<p:message for="txtVoie" />
<p:outputPanel></p:outputPanel>
<p:outputPanel>
<p:commandButton value="Annuler" update="formClient"
styleClass="redButton" process="@this"
actionListener="#{clientBean.cancel()}" />
<p:commandButton ajax="false" value="Suivant"
styleClass="greenButton" action="Devis?faces-redirect=true"></p:commandButton>
</p:outputPanel>
</p:panelGrid>
</h:form>
</h:panelGroup>
正如您在此处看到的,我有一些必填字段,以及用于取消(清空)表单的按钮:
@ManagedBean(name = "clientBean")
@SessionScoped
public class ClientBean implements Serializable {
@EJB
private IClientDAO clientDAO;
@EJB
private ITypeVoieDAO typeVoieDAO;
private List<TypeVoie> typeVoies;
private static final long serialVersionUID = -3324864097586374969L;
private Client client = new Client();
@PostConstruct
public void init(){
typeVoies = typeVoieDAO.findAll();
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public void rechercher(){
if(client != null && client.getIdentifiant() != null){
System.out.println(client.getIdentifiant());
client = clientDAO.findByIdentifiant(client.getIdentifiant());
if(client == null){
cancel();
}
}
}
public void cancel(){
client = new Client();
System.out.println(typeVoies);
}
public String navigateToClientPage(){
rechercher();
return "client?faces-redirect=true";
}
public List<TypeVoie> getTypeVoies() {
return typeVoies;
}
public void setTypeVoies(List<TypeVoie> typeVoies) {
this.typeVoies = typeVoies;
}
}
在页面加载时,selectonemenu 加载完美,但是当我单击取消时,selectonemenu 被清空,即使我“搜索”客户端并更新表单,所有其他字段都正确填写,但 selectonemenu变得完全空了。
使用 JSF 2.1 和 primefaces 6.0
更新: 添加了一个转换器,但没有任何改变...
【问题讨论】:
-
您的
typeVoies列表元素属于什么类型。对于复杂对象问题,这可能是missing converter。 -
是的,它是一个具有多个字段的对象,我应该尝试为 ir 创建一个转换器吗?
-
我建议这样做。如果遇到问题以查看是否存在 JSF 错误,将
<p:growl autoUpdate="true" />添加到页面始终是一个不错的选择。不过,“selectOneMenu 完全为空” 到底是什么意思? -
但如果是转换器问题,它甚至是如何第一次加载的,并且是空的,因为您在列表中看不到任何值...我添加了咆哮,没有消息显示
-
它会在第一次加载,因为标准转换器使用Object.toString()在客户端对复杂对象进行编码。如果您在服务器上处理输入并尝试将编码对象转换回来,则会首先出现错误。您能否具体说明我们应该在哪里“看到”“列表中没有值”。
标签: jsf primefaces