JSF方法总是被调用两次

时间:2013-12-17 02:14:43

标签: jsf sorting pagination jsf-2.2

您好我有一个使用视图范围的jsf datatabel,它允许排序和分页。 我的问题是,我的BackingBean排序方法总是被调用两次,如果不排序升序/降序排序同一行(这使它过时),这不会是一个大问题。 任何想法如何解决这个问题?我已经阅读了几个关于这个问题的方块,但是在我的情况下,非帮我... 我也试图改变它以请求范围,但这只是让我的分页消失......

这是BackingBean Methode,只能通过我的facelet链接调用:

/**
 * Changing sorting of list of events by columns.
 * 
 * @param event
 *            ActionListener for the sorting field.
 */
public void sort(String sortingAttribute) {
    System.out.println("you called sort: " + ++x);
    if (sortField.equalsIgnoreCase(sortingAttribute)) {
        boolean sortAscHelp = (sortAsc) ? false : true;
        sortAsc = sortAscHelp;

    } else {
        sortField = sortingAttribute;
    }

    try {
        events = (eventListType.equalsIgnoreCase("search")) ?EventManager.getSearchResults(searchTitle, searchDate, searchLocation, sortField, sortAsc, offset, limit) : EventManager.getEventList(eventListType, offset, limit, sortField, sortAsc);
    } catch (DatabaseException e) {
        FacesContext fcxt = FacesContext.getCurrentInstance();
        fcxt.addMessage(component.getClientId(),
                new FacesMessage(e.toString()));
        logger.error("Error while loading the sorted list of  searched events.");
    }
}

这是我的面孔:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">

<ui:composition template="../../templates/basicTemplate.xhtml">
<ui:define name="metadata">
    <f:metadata>
        <f:viewParam name="title" value="#{listEventsBean.searchTitle}"/>
        <f:viewParam name="location" value="#{listEventsBean.searchLocation}"/>
        <f:viewParam name="date" value="#{listEventsBean.searchDate}"/>
        <f:viewParam name="eventListType" value="#{listEventsBean.eventListType}"/>
        <f:viewParam name="page" value="#{listEventsBean.page}"/>
        <f:viewParam name="sorting" value="#{listEventsBean.sortField}"/>
        <f:viewAction action="#{listEventsBean.init}"/>
    </f:metadata>
</ui:define>
<ui:define name="content">
<h:form>
   <p style="color:#808080; font-weight:bold; font-size:large">#{msgs.listEvents_events}</p>
   <br/>
   <h:outputText style="color:green" value="#{msgs.listEvents_deleted}" rendered="#{listEventsBean.deleted}"/>

   <h:dataTable id="eventTable" value="#{listEventsBean.getEvents()}" var="event" binding="#{listEventsBean.component}"> 
    <h:column>
        <f:facet name="header">
            <h:commandLink value="#{msgs.listEvents_title}" action="#{listEventsBean.sort('title')}">
            <f:attribute name="sortField" value="title"/>
            </h:commandLink>
        </f:facet>
        <h:outputText value="#{event.title}"/>
    </h:column>
    <h:column>
        <f:facet name="header">
            <h:commandLink value="#{msgs.listEvents_dateStart}" action="#{listEventsBean.sort('startofevent')}">
            <f:attribute name="sortField" value="startOfEvent"/>
            </h:commandLink>
        </f:facet>
        <h:outputText value="#{event.startOfEvent}">
            <f:convertDateTime pattern="#{msgs.longDate}"/>
        </h:outputText>
    </h:column>
    <h:column>
        <f:facet name="header">
            <h:commandLink value="#{msgs.listEvents_dateEnd}" action="#{listEventsBean.sort('endofevent')}">
            <f:attribute name="sortField" value="endOfEvent"/>
            </h:commandLink>
        </f:facet>
        <h:outputText value="#{event.endOfEvent}">
            <f:convertDateTime pattern="#{msgs.longDate}"/>
        </h:outputText>
    </h:column>
    <h:column>
        <f:facet name="header">
            <h:commandLink value="#{msgs.listEvents_price}" action="#{listEventsBean.sort('ticketprice')}">
            </h:commandLink>
        </f:facet>
        <h:outputText value="#{event.ticketPrice}"/>
    </h:column>
    <h:column>
        <f:facet name="header">
            <h:outputText value="#{msgs.listEvents_action}"/>
        </f:facet>
        <h:button id="showDetails" value="#{msgs.listEvents_showDetails}" 
                    outcome="showEvent">
                    <f:param name="eventID" value="#{event.eventID}"/>
        </h:button>
        <h:commandButton id="deleteEvent" value="#{msgs.listEvents_delete}"
                    action="#{listEventsBean.deleteEvent(event.eventID)}" 
                    rendered="#{sessionBean.ld.systemAdmin}" 
                    onclick="return confirm('#{msgs.listEvents_deleteMsg}')"/>    
    </h:column>
</h:dataTable>
<h:outputText style="color:blue" value="#{msgs.listEvents_emptyResult}" rendered="#{listEventsBean.notEmpty}"/>
    <br />
<h:outputLink id="backToSearchLink" value="#{request.contextPath}/facelets/allUsers/searchEvents.xhtml" rendered="#{listEventsBean.notEmpty}">
    <h:outputText value="#{msgs.listEvents_searchAgain}"/>
</h:outputLink>

<ui:repeat id="paginationEvent" var="p" value="#{listEventsBean.getPageNumbers()}">
   <h:outputLink value="#{request.contextPath}/facelets/allUsers/listEvents.xhtml">
       <h:outputText value="#{p.toString()}" />
       <f:param name="page" value="#{p.toString()}" />
       <f:param name="sorting" value="#{listEventsBean.sortField}"/>
       <f:param name="eventListType" value="#{listEventsBean.eventListType}" />
   </h:outputLink>
</ui:repeat>

</h:form>
</ui:define>
</ui:composition>
</ui:composition>

1 个答案:

答案 0 :(得分:0)

该死的!我刚刚发现了我的错误! 我仍然有一个绑定附加到我的dataTable(dataTable的开头)。 因为我的视图范围绑定导致错误。一个似乎是在dataTable中调用我的方法两次(坦克到BalusC获取此信息)。通过将它们渲染为h:outputText来删除它并添加消息。 我仍然愿意接受任何建议,如何在不丢失我的分页的情况下更改我的范围,或者如何在不渲染h:outputText和绑定的情况下以不同的方式将BackingBean中的消息添加到我的应用程序中。