0

I got a problem with buttons in a datatable. The table is populated by calling myEventInit and directed to the page MyEvents.xhtml (se code below). Data for the table is provided by lessonlist. I put a breakpoint on the line for getLessonList. First time the page I rendered lessonList got some data. But if I trigger a button in the table lessonList is null, setLesson2delete is not called and no action is done. I really don't get why lessonList on the session scoped bean is empty.

Here is the xhtml page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <h:head></h:head>
    <h:body>
        <ui:composition template="/templates/BasicTemplate.xhtml">
            <ui:define name="content">
                <div style="background-color: #cfcfcf;
                     border: 1px black dotted; width:95%; margin: 10px;">
                    <h:form id="deleteLessons">
                        <p:dataTable value="#{schb.lessonList}" var="lesson" id="lessonlist" >
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Sal" />
                                </f:facet>

                                <h3>
                                    <h:outputText value="#{lesson.lessonInfo}" />
                                </h3>
                            </p:column>

                            <p:column >
                                <f:facet name="header">
                                    <h:outputText value="Dina bokningar" />
                                </f:facet>

                                <p:commandButton style="width:450px; font-size:15px;height:23px;"
                                                 update="@this"
                                                 value="Radera   #{lesson.lessonInfo}">
                                    <f:setPropertyActionListener value="#{lesson.uuid}"
                                                                 target="#{schb.lesson2delete}" />
                                </p:commandButton>
                            </p:column>
                        </p:dataTable>
                    </h:form>
                </div>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

and the backing bean:

package jkpgweb.managedbeans;

@ManagedBean(name = "schb")
@SessionScoped
public class SchemBean2 extends BeanTemplate {

    Date start = new Date(1452765600000L), stop;
    int lengthOfLesson = 70;
    int searchweek;
    String searchklass, requestedDay, requestedGroup;
    String room2book;

    ArrayList<sbasLesson> lessonList;

    public String myEventsInit() {
        ObjectContainer localdb = dbConnector.connDB();
        Query q = localdb.query();
        q.constrain(sbasLesson.class);
        q.descend("Teacher").constrain(this.getCurrUser().getUsername()).contains();
        q.descend("type").constrain('M');
        ObjectSet<sbasLesson> res;
        res = q.execute();

        lessonList = new ArrayList<sbasLesson>();
        lessonList.addAll(res);

        return "/Teacher/myEvents.xhtml";
    }

    public void setLesson2delete(long duuid) {
        ObjectContainer db = dbConnector.connDB();
        Query q = db.query();
        q.constrain(sbasLesson.class);
        q.descend("uuid").constrain(duuid);
        db.close();
    }

    @PostConstruct
    public ArrayList<sbasLesson> getLessonList() {
        return lessonList;
    }

    public void setLessonList(ArrayList<sbasLesson> lessonList) {
        this.lessonList = lessonList;
    }
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
lilleGauss
  • 68
  • 7

1 Answers1

0

The @PostConstruct at there is useless (if not bad). Your commandButton does not have an action.

In fact it will be better to call the action as action (not as property) - just define action for the button

<p:commandButton action="#{schb.deleteLesson(lesson.uuid)} ...

Try to turn it into regular request (non-ajax) so it will print error

<p:commandButton ajax="false" ...

PS: you don't need setter for this list, and u should name your classes with capital letter (SbasLesson).

EDIT: this works for me:

    @ManagedBean(name = "schb")
    @ViewScoped
    public class SchemBean2 {


        ArrayList<sbasLesson> lessonList;

        public static class sbasLesson{
            private String uuid;
            private String lessonInfo;

            public sbasLesson(String uuid, String lessonInfo) {
                this.uuid = uuid;
                this.lessonInfo = lessonInfo;
            }

            public String getUuid() {
                return uuid;
            }

            public void setUuid(String uuid) {
                this.uuid = uuid;
            }

            public String getLessonInfo() {
                return lessonInfo;
            }

            public void setLessonInfo(String lessonInfo) {
                this.lessonInfo = lessonInfo;
            }
        }

        @PostConstruct
        public void myEventsInit() {

            lessonList = new ArrayList<>();
            lessonList.addAll(Arrays.asList(new sbasLesson("uuid 1", "lesson 1"), new sbasLesson("uuid 2", "lesson 2"), new sbasLesson("uuid 3", "lesson 3")));

        }

        public void delete(String uuid) {
            lessonList.remove(0);
        }

        public ArrayList<sbasLesson> getLessonList() {
            return lessonList;
        }

        public void setLessonList(ArrayList<sbasLesson> lessonList) {
            this.lessonList = lessonList;
        }
    }

xhtml:

   <h:form id="deleteLessons">
       <p:dataTable value="#{schb.lessonList}" var="lesson" id="lessonlist">
           <p:column>
               <f:facet name="header">
                   <h:outputText value="Sal" />
               </f:facet>

               <h3>
                   <h:outputText value="#{lesson.lessonInfo}" />
               </h3>
           </p:column>

           <p:column >
               <f:facet name="header">
                   <h:outputText value="Dina bokningar" />
               </f:facet>

               <p:commandButton style="width:450px; font-size:15px;height:23px;"
                                update="@this" action="#{schb.delete(lesson.uuid)}"
                                ajax="false"
                                value="Radera   #{lesson.lessonInfo}">
               </p:commandButton>
           </p:column>
       </p:dataTable>
   </h:form>

changes: uuid is now String (looks like u are using long as argument), defined action in button, works with ajax="true" too, moved @PostConstruct

Flowy
  • 420
  • 1
  • 5
  • 15
  • I changed as you suggested but lessonList is still null when hitting the button. – lilleGauss Jan 04 '16 at 08:49
  • does it report any error when turning the ajax off? – Flowy Jan 04 '16 at 11:51
  • No, nothing. My IDE (eclipse) jumps several times to the breakpoint, lessonList is always null and returns. – lilleGauss Jan 04 '16 at 12:31
  • I tried your changes but still no success. I created a constructor or the bean (paramaterless) with a System.out.println("Bean created"); the constructor i called every time I hit a button in the table. Seems the bean isn't really SessionScoped. read about those issues (e.g. [link](http://stackoverflow.com/questions/8034428/hcommandbutton-not-working-inside-hdatatable) but i am not getting it. Further reading is required. – lilleGauss Jan 04 '16 at 20:09