4

I am using schedule component of Primefaces. I am filling it with values from database and when the user selects sth from the selectonemenu an ajax event is triggered (I tried to put just the related code, if there is sth missing pls remind me):

xhtml:

 <h:outputText value="Scope :" />
 <h:selectOneMenu id="scope" value="#{scheduleView.scope}">                    
   <f:selectItems value="#{lookup.scopeCombo}"/>
   <p:ajax process="scope" update="schedule, scheduleForm, scheduleFormPG" listener="#{scheduleView.changeScopeType()}"/>
 </h:selectOneMenu> 

<p:schedule id="schedule" value="#{scheduleView.model}" editable="true"/>

Backing bean:

@ManagedBean
@ViewScoped
public class ScheduleView implements Serializable { 

@PostConstruct
    public void init() {
        System.out.println("Init ");
        scopeChange();                
    } 

    public void scopeChange(String scope){
        System.out.println("scopeChange ");
        model.clear();
        events = (List<Event>) commonServis.bringEverythingByCriteria(Event.class, "scope" , scope);   
        for(int i= 0; i<events.size();i++){
        model.addEvent(new DefaultScheduleEvent(events.get(i).getAd(), events.get(i).getStartDate(),events.get(i).getEndDate()));
    }

    public void changeScopeType() {
    System.out.println("changeScopeType ");
        scopeChange (scope);
}

The output of the above code is:

Init
scopeChange

When the user changes the value in the selectonemenu:

changeScopeType
Init
scopeChange

It is supposed to go into init method just once. But after the changeScopeType function is triggered it gets into the init method and fills the schedule with unrelated data. I thought it might be related to @Postconstruct annotation but I couldn't find any related explanation. Can anyone understand the reason and offer a solution?

Here is the full page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://prime.primefaces.org/ui"
                template="templates/layout.xhtml"
                xmlns:c="http://java.sun.com/jsp/jstl/core">
    <ui:define name="title">#{labels.schedule}</ui:define>
    <ui:define name="content">
        <h:form id="scheduleForm">
            <h:panelGrid id="scheduleFormPG">
                <p:growl id="msgs" />
                <p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false">
                    <h:graphicImage value="resources/images/ajax-loader.gif" />
                </p:dialog>
                <p:dialog showEffect="explode" hideEffect="explode" resizable="false"
                      header="warning" widgetVar="confirmationErase" appendToBody="true" modal="true">
                    <h:outputText value="Are you sure?"/>
                    <br/>
                    <p:commandButton  value="Yes" actionListener="#{scheduleView.deleteEvent(AE)}"
                                     update="msgs, scheduleFormPG, schedule, wrapperPanel"
                                     onstart="statusDialog.show(),confirmationErase.hide()"
                                     oncomplete="statusDialog.hide(), eventDialog.hide()" process="@parent, scope" />
                    <p:commandButton value="No" onclick="confirmationErase.hide()" type="button" />
                </p:dialog>                
                <h:outputText value="scope :" />
                <h:selectOneMenu id="scope" value="#{scheduleView.scope}">                    
                    <f:selectItems value="#{lookup.scopeTypeCombo}"/>
                    <p:ajax process="scope" update="schedule, scheduleForm, scheduleFormPG" listener="#{scheduleView.changeScopeType()}"/>
                </h:selectOneMenu>                
             </h:panelGrid>            
            <p:schedule onDateSelectUpdate="wrapperPanel" 
                        onEventSelectUpdate="wrapperPanel" onEventSelectComplete="eventDialog.show()" eventSelectListener="#{scheduleView.onEventSelect}" 
                        onDateSelectComplete="eventDialog.show();"  dateSelectListener="#{scheduleView.onDateSelect}" id="schedule" value="#{scheduleView.model}" editable="true"/>
            <p:dialog id="dialog111" widgetVar="eventDialog" header="Event Information" showEffect="clip" hideEffect="clip">
                <p:panel id="wrapperPanel">                    
                    <h:panelGrid id="eventDetails" columns="2">   
                        <h:outputLabel for="eventName" value="Event Name *: " />
                        <p:inputText id="eventName" value="#{scheduleView.event.title}" required="true"/>
                        <h:outputLabel value="Start Date:" />
                        <p:calendar id="sKalender" value="#{scheduleView.event.startDate}">
                            <f:convertDateTime pattern="dd/MM/yyyy" />  
                        </p:calendar>
                        <h:outputLabel value="End Date:" />
                        <p:calendar id="eKalender" value="#{scheduleView.event.endDate}">
                            <f:convertDateTime pattern="dd/MM/yyyy" />  
                        </p:calendar>
                        <h:outputLabel value="All Day:" />
                        <h:selectBooleanCheckbox id="allDay" value="#{scheduleView.event.allDay}" />
                        <h:outputLabel value="scope: " />
                        <h:selectOneMenu id="scopeChoice" value="#{scheduleView.event.scope}">                    
                            <f:selectItems value="#{lookup.scopeTypeCombo}"/>
                         </h:selectOneMenu>                         
                          <p:commandButton onclick="confirmationErase.show()" oncomplete="eventDialog.hide()" update="wrapperPanel, msgs, schedule" type="reset" value="Delete" />
                        <p:commandButton value="Save" actionListener="#{scheduleView.addEvent(AE)}" process="@parent, scope" update="schedule wrapperPanel msgs scheduleForm" oncomplete="eventDialog.hide();"/>
                    </h:panelGrid>
                </p:panel>
            </p:dialog>
           </h:form>
    </ui:define>
</ui:composition>
lamostreta
  • 2,359
  • 7
  • 44
  • 61
  • 1
    Are you referencing the `#{scheduleView}` anywhere in a taghandler like JSTL `` or in `id` or `binding` attributes of JSF components? See also this related question: http://stackoverflow.com/questions/14215098/view-scoped-bean-recreated-on-post-when-url-parameters-not-used/14216971#14216971 – BalusC Jan 11 '13 at 15:28
  • Thanks for your comment. It is not in JSTL or in id or binding attributes. But it is used like: value="#{scheduleView.scope}" or actionListener="#{scheduleView.deleteEvent(AE)} or listener="#{scheduleView.changeKapsamTipi() or rendered="#{scheduleView.takvimTuruYillik}" or eventSelectListener="#{scheduleView.onEventSelect}" inside p:commandButton, p:schedule, p:dataTable and h:selectonemenu components. – lamostreta Jan 11 '13 at 15:53
  • 1
    Well, then I don't see any reasonable causes. Are you using Spring? I recall the JSF partial state saving mechanism to somehow break like that when Spring is installed, causing all view scoped beans to behave like request scoped ones. You'd need to explicitly force `javax.faces.PARTIAL_STATE_SAVING` context parameter to `true`. – BalusC Jan 11 '13 at 15:57
  • yes I am using Spring. I tried turning off the partial state saving as you mentioned but it did not work.. – lamostreta Jan 14 '13 at 10:10
  • Regarding your post (http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ViewScopedFailsInTagHandlers) shouldn't I be using also? – lamostreta Jan 14 '13 at 12:40
  • It's merely for templating. – BalusC Jan 14 '13 at 12:41
  • 1
    try adding to your bean `@PreDestroy public void destory(){ System.out.println("why ow why"); }` and see when its being destroyed... – Daniel Jan 15 '13 at 09:18
  • @lamostreta , also , try to replace `actionListener="#{scheduleView.deleteEvent(AE)}` with `actionListener="#{scheduleView.deleteEvent}` , cause `actionListener` not supposed to receive any arguments... – Daniel Jan 15 '13 at 09:39
  • I added.. but I didn't get it in the output.. I also log out from the system to see if it is triggered in that case, still no good. – lamostreta Jan 15 '13 at 09:47
  • OK, I will also try that.. – lamostreta Jan 15 '13 at 09:48
  • @lamostreta You mean that being is being created twice , without being destroyed in between ? You can also try removing references to `scheduleView` from your xhtmls... – Daniel Jan 15 '13 at 09:53
  • :P I have 1 unrelated question. When you use `update="schedule, scheduleForm, scheduleFormPG"`, it still can work? I thought the syntax for `update` attribute does not have commas. – Mr.J4mes Jan 15 '13 at 23:42
  • @Daniel thanks, but the problem was totally another thing.. – lamostreta Jan 16 '13 at 09:08

1 Answers1

1

The problem was this line of code:

<p:ajax process="scope" update="schedule, scheduleForm, scheduleFormPG" listener="#{scheduleView.changeScopeType()}"/>

update was refreshing the whole page, instead it should've only update schedule. When I change it to:

update="schedule" 

the problem solved. Thanks everyone for their effort.

lamostreta
  • 2,359
  • 7
  • 44
  • 61