commandLink无法在JSF中执行backing bean方法?

时间:2017-10-26 12:24:03

标签: sorting jsf xhtml datamodel commandlink

我正在尝试对自定义创建的SortableDataModel进行排序,但它不起作用。有一个commandLink,当点击它时调用backing bean中的sort方法,它对我创建的自定义SortableDataModel进行排序。这正确适用于其他xhtml页面,但我猜这个页面没有正确调用该方法。 我不确定方法是否被调用或者我的方法中的逻辑问题。

private String sortSelection;
//.... getters and setters..

//Sort Method
public String sort(){
            try{
                if(asc == true) {
                    if (this.sortSelection.equalsIgnoreCase("id")) {
                        stateModel.sortBy(new Comparator<India>() {
                            @Override
                            public int compare(India o1, India o2) {
                                return Integer.compare(o1.getStateId(), o2.getStateId());
                            }
                        });
                    }
                    if (sortSelection.equalsIgnoreCase("states")) {
                        stateModel.sortBy(new Comparator<India>() {
                            @Override
                            public int compare(India o1, India o2) {
                                return o1.getState().getStateName().compareTo(o2.getState().getStateName());
                            }
                        });
                    }

                    if (sortSelection.equalsIgnoreCase("population")) {
                        stateModel.sortBy(new Comparator<India>() {
                            @Override
                            public int compare(India o1, India o2) {
                                return o1.getState().getStatePopulation().compareTo(o2.getState().getStatePopulation());
                            }
                        });
                    }
                    if (sortSelection.equalsIgnoreCase("area")) {
                        stateModel.sortBy(new Comparator<India>() {
                            @Override
                            public int compare(India o1, India o2) {
                                return o1.getState().getStateArea().compareTo(o2.getState().getStateArea());
                            }
                        });
                    }
                    asc = false;
                }
                else{
                    if (sortSelection.equalsIgnoreCase("id")) {
                        stateModel.sortBy(new Comparator<India>() {
                            @Override
                            public int compare(India o1, India o2) {
                                return Integer.compare(o2.getStateId(), o1.getStateId());
                            }
                        });
                    }
                    if (sortSelection.equalsIgnoreCase("states")) {
                        stateModel.sortBy(new Comparator<India>() {
                            @Override
                            public int compare(India o1, India o2) {
                                return o2.getState().getStateName().compareTo(o1.getState().getStateName());
                            }
                        });
                    }

                    if (sortSelection.equalsIgnoreCase("population")) {
                        stateModel.sortBy(new Comparator<India>() {
                            @Override
                            public int compare(India o1, India o2) {
                                return o2.getState().getStatePopulation().compareTo(o1.getState().getStatePopulation());
                            }
                        });
                    }
                    if (sortSelection.equalsIgnoreCase("area")) {
                        stateModel.sortBy(new Comparator<India>() {
                            @Override
                            public int compare(India o1, India o2) {
                                return o2.getState().getStateArea().compareTo(o1.getState().getStateArea());
                            }
                        });
                    }
                    asc = true;
                }
                return null;
            }
            catch (Exception e) {
                return null;
            }
        }

我的XHTML文件

<h:dataTable value="#{countryDetails.stateModel}" var="india"
                         styleClass="country-table"
                         headerClass="country-table-header"
                         rowClasses="country-table-odd-row,country-table-even-row"
                         border="1">

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="Id"/>
                        <br/>
                        <h:commandLink action="#{countryDetails.sort}" value="sort" style="font-size:10px;color:inherit">
                            <f:setPropertyActionListener value="id" target="#{countryDetails.sortSelection}" />
                        </h:commandLink>
                    </f:facet>
                    <h:outputLabel value="#{india.stateId}" />
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="States"/>
                        <br/>
                        <h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
                            <f:setPropertyActionListener value="states" target="#{countryDetails.sortSelection}" />
                        </h:commandLink>
                    </f:facet>
                    <h:outputLabel value="#{india.state.stateName}" />
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="State Capitals"/>
                        <br/>
                        <h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
                            <f:setPropertyActionListener value="capitals" target="#{countryDetails.sortSelection}" />
                        </h:commandLink>
                    </f:facet>
                    <h:outputLabel value="#{india.state.stateCapital}" />
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="Population"/>
                        <br/>
                        <h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
                            <f:setPropertyActionListener value="population" target="#{countryDetails.sortSelection}" />
                        </h:commandLink>
                    </f:facet>
                    <h:outputLabel value="#{india.state.formattedPop}" />
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="Area (in sq. kms.)"/>
                        <br/>
                        <h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
                            <f:setPropertyActionListener value="area" target="#{countryDetails.sortSelection}" />
                        </h:commandLink>
                    </f:facet>
                    <h:outputLabel value="#{india.state.formattedArea}" />
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="Language"/>
                        <br/>
                        <h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
                            <f:setPropertyActionListener value="language" target="#{countryDetails.sortSelection}" />
                        </h:commandLink>
                    </f:facet>
                    <h:outputLabel value="#{india.state.stateLanguage}" />
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="Government Type"/>
                    </f:facet>
                    <h:outputLabel value="#{india.govtStructure.govtType}" />
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="Total Candidacies"/>
                        <br/>
                        <h:commandLink action="#{countryDetails.sort()}" value="sort" style="font-size:10px;color:inherit">
                            <f:setPropertyActionListener value="candidacies" target="#{countryDetails.sortSelection}" />
                        </h:commandLink>
                    </f:facet>
                    <h:outputLabel value="#{india.govtStructure.totalParties}" />
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputLabel value="Current Ruling Party"/>
                    </f:facet>
                    <h:outputLabel value="#{india.govtStructure.currentRulingParty}" />
                </h:column>

我猜commandLink没有正确调用方法,因为sort()方法中没有执行任何操作。我被卡住了。需要帮助

0 个答案:

没有答案