JSF表单验证和更新

时间:2015-09-14 13:59:38

标签: jsf-2

我有一个复杂的表单,其中包含一个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 个答案:

答案 0 :(得分:0)

将您的添加链接替换为以下代码

<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 没有 href 等属性。使用样式代替属性,而不是 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或更宽。