【问题标题】:Action empties the selectonemenu操作清空选择菜单
【发布时间】: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 错误,将&lt;p:growl autoUpdate="true" /&gt; 添加到页面始终是一个不错的选择。不过,“selectOneMenu 完全为空” 到底是什么意思?
  • 但如果是转换器问题,它甚至是如何第一次加载的,并且是空的,因为您在列表中看不到任何值...我添加了咆哮,没有消息显示
  • 它会在第一次加载,因为标准转换器使用Object.toString()在客户端对复杂对象进行编码。如果您在服务器上处理输入并尝试将编码对象转换回来,则会首先出现错误。您能否具体说明我们应该在哪里“看到”“列表中没有值”

标签: jsf primefaces


【解决方案1】:

确保您没有在 backingbean 上的某个时间点清空 typeVoies 列表。如果你能分享你完整的java类,你可能会得到更准确的帮助。

【讨论】:

  • 我确保列表不为空,我用全班更新了我的问题
  • 是的,如果我不这样做,该操作将不起作用,因为我将一些输入设置为必需。
  • @MehmetBaşal 这是一种防止在服务器端处理其他字段的方法。使用“取消”,您不感兴趣,例如验证错误以防止表单被提交。因此,使用此属性,您可以说明应该处理的内容,在这种情况下只有按钮并跳过所有其他字段:stackoverflow.com/questions/25339056/…
猜你喜欢
  • 2016-07-30
  • 2013-10-27
  • 2021-07-25
  • 2016-07-17
  • 1970-01-01
  • 2023-01-09
  • 2014-10-31
  • 2021-09-04
  • 1970-01-01
相关资源
最近更新 更多