调用表中的按钮后Bean属性为空

时间:2016-01-03 22:15:46

标签: jsf primefaces

我在数据表中遇到了按钮问题。该表通过调用myEventInit填充并定向到页面MyEvents.xhtml(下面的代码)。该表的数据由课程列表提供。我在getLessonList的行上放了一个断点。我第一次渲染的课程得到了一些数据。 但是,如果我触发表中的按钮,则lessonList为null,则不会调用setLesson2delete,也不会执行任何操作。我真的不明白为什么sessionList在会话范围内的bean是空的。

这是xhtml页面:

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;
    }
}

和支持bean:

Store

1 个答案:

答案 0 :(得分:0)

@PostConstruct在那里没用(如果还不错)。 你的commandButton没有动作。

事实上,最好将动作称为动作(而不是属性) - 只需为按钮定义动作

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

尝试将其转换为常规请求(非ajax),因此会打印错误

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

PS:你不需要这个列表的setter,你应该用大写字母命名你的类(SbasLesson)。

编辑:这对我有用:

    @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>

更改:uuid现在是String(看起来你使用long作为参数),按钮中定义的操作,也适用于ajax =“true”,移动@PostConstruct