【问题标题】:JSF form validation and updateJSF 表单验证和更新
【发布时间】:2015-12-10 13:40:58
【问题描述】:

我有一个复杂的表单,其中包含一个 selectonemenu,我从中选择一个用于添加到表中的值。问题是,当验证所有字段的验证时,我无法使其工作。我尝试了多种解决方案,但无法使其发挥作用。如果有人可以指导我,我将不胜感激,因为我不是很有经验,但非常渴望学习。 thx in advaned

<h:form class="add-form">
            <div class="row">                   
                <fieldset class="col-sm-3 col-md-3"><!-- personal info fields -->   
                <legend>Personal Information</legend>               
                <div class="required"> 
                    <label>First Name</label>
                    <h:inputText value="#{candidateBean.firstName}"></h:inputText>
                </div>

                <div class="col-sm-9 col-md-9">             
                    <div class="required"> 
                        <label>Select Working City</label>
                        <h:selectOneMenu value="#{candidateBean.city}" converter="omnifaces.SelectItemsConverter">
                            <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
                            <f:selectItems value="#{settingsBean.settings.citiesListVO.workingCityVO}" var="cities" itemValue="#{cities}" itemLabel="#{cities.name}"/>
                        </h:selectOneMenu>
                    </div>      
                </div>
                <div class="col-sm-3 col-md-3"> 
                    <div class="required">                
                        <label>Add</label>         
                        <h:commandLink type="button" class="btn btn-primary form-control input-sm"
                            title="Add"
                            ajax="false"
                            href="#!"
                            actionListener="#{candidateBean.addWorkingCity}">
                            <span class="glyphicon glyphicon-plus"></span>
                        </h:commandLink>                
                    </div>
                </div>
                </div><!-- working cities inputs -->                    
                <div class="row">
                    <div class="col-sm-12 col-md-12 required">
                    <label>Current Working Cities:</label><br></br> 
                    <h:dataTable
                        class="table table-condensed"
                        value="#{candidateBean.workingCities}" var="currentCity">
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="City Name" />
                            </f:facet>
                            <h:outputText value="#{currentCity.name}" />
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="Options"></h:outputText>
                            </f:facet>
                                <h:commandLink type="button" class="btn btn-primary btn-xs"
                                    title="Remove"
                                    ajax="false"
                                    actionListener="#{candidateBean.removeWorkingCity}">
                                        <f:attribute name="selected" value="#{currentCity}"/>
                                    <span class="glyphicon glyphicon-remove"></span>
                                </h:commandLink>
                        </h:column>
                    </h:dataTable>
                </div><!-- column -->
                </div><!-- workingCity table row -->

这是初始代码。我已经尝试了各种解决方案,例如 ayax 更新,但我没有成功。如果我解释得不够,我很抱歉。请询问更多信息

【问题讨论】:

  • -1 因为你需要花更多的时间来解释你的代码和你试图做什么。您所说的只是您在验证方面遇到了问题,并且您尝试了一些解决方案。有什么解决方案?验证问题是什么意思?

标签: jsf-2


【解决方案1】:

用下面的代码替换您的添加链接

<h:commandLink actionListener="#{candidateBean.addWorkingCity}"
           styleClass="btn btn-primary form-control input-sm"
           title="Add">
    <span class="glyphicon glyphicon-plus"></span> Add
</h:commandLink>  

标签 commandLink 没有 classhref 等属性。使用 styleClass 代替属性 class,使用 action 代替 href。更多信息请阅读commandLink documentation

要从表中删除项目,我会使用 action 而不是 actionListener:

<h:commandLink title="Remove"
           styleClass="btn btn-primary btn-xs"
           action="#{candidateBean.removeWorkingCity(currentCity)}"
           >
    <span class="glyphicon glyphicon-remove"></span> Remove
</h:commandLink>

在你的 CandidateBean 中:

public void removeWorkingCity(City c) {
    workingCities.remove(c);
}

确保 CandidateBean 是 ViewScoped 或更宽的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 2016-11-21
    • 2019-07-15
    • 1970-01-01
    相关资源
    最近更新 更多