primefaces - When there are two different selection tables in my JSF page I can't get the selected row of the second table -
i'm working on jsf database management system project using primefaces , i'm trying create page has 2 data selection tables. there 2 different data tables in page, 1 of them user able select multiple data while other 1 user can select one. after selection backing bean takes needed values both of selected rows , creates new data third different table. third different table available in page data table no selection. unfortunately in design backing bean cannot take value of selected row of second data table while takes multiple selected rows first table. here code:
this xhtml code page:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"> <h:head> <title>ogrenci-ders atama sayfasi</title> </h:head> <h:body> <h:form id="form"> <h:outputtext id="msgcomponent" binding="#{msgbean.component}" /> <p:messages showdetail="true" showsummary="true" autoupdate="true" /> <p:datatable id="lessontable" var="lesson" value="#{lessonview.lessons}" selectionmode="multiple" selection="#{studentlessonbean.selectedlessons}" rowkey="#{lesson.lessonid}" scrollable="true" scrollheight="150"> <f:facet name="header"> dersler </f:facet> <p:column headertext="id"> <h:outputtext value="#{lesson.lessonid}" /> </p:column> <p:column headertext="name"> <h:outputtext value="#{lesson.lessonname}" /> </p:column> </p:datatable> </h:form> <h:form id="studenttableform"> <p:datatable id="studenttable" var="student" value="#{studentview.students}" selectionmode="single" selection="#{studentlessonbean.selectedstudent}" rowkey="#{student.studentid}" scrollable="true" scrollheight="150"> <f:facet name="header"> Öğrenciler </f:facet> <p:column headertext="id"> <h:outputtext value="#{student.studentid}" /> </p:column> <p:column headertext="number"> <h:outputtext value="#{student.studentnumber}" /> </p:column> <p:column headertext="name"> <h:outputtext value="#{student.studentname}" /> </p:column> <p:column headertext="class"> <h:outputtext value="#{student.studentclass}" /> </p:column> </p:datatable> </h:form> <p:commandbutton icon="ui-icon-arrowthick-2-e-w" action="#{studentlessonbean.insertstudentlesson()}"> </p:commandbutton> <h:form id="kullanilmayan"> <p:datatable var="studentlesson" value="#{studentlessonbean.studentlessons}" widgetvar="studentlessonstable" emptymessage="no students found given criteria" filteredvalue="#{studentlessonbean.filteredstudentlessons}"> <f:facet name="header"> <p:outputpanel> <h:outputtext value="search fields:" /> <p:inputtext id="globalfilter" onkeyup="pf('studentlessonstable').filter()" style="width:150px" placeholder="enter keyword" /> </p:outputpanel> </f:facet> <p:column filterby="#{studentlesson.studentlessonid}" headertext="student-lesson id" footertext="contains" filtermatchmode="contains"> <h:outputtext value="#{studentlesson.studentlessonid}" /> </p:column> <p:column filterby="#{studentlesson.studentid}" headertext="student id" footertext="contains" filtermatchmode="contains"> <h:outputtext value="#{studentlesson.studentid}" /> </p:column> <p:column filterby="#{studentlesson.lessonid}" headertext="lesson id" footertext="contains" filtermatchmode="contains"> <h:outputtext value="#{studentlesson.lessonid}" /> </p:column> <p:column filterby="#{studentlesson.score}" headertext="score" footertext="contains" filtermatchmode="contains"> <h:outputtext value="#{studentlesson.score}" /> </p:column> </p:datatable> </h:form> </h:body> </html>
here bean getting selected rows , use them insert new data third different table (the problem right here, program never runs setselectedstudent() function leaving null , throwing nullpointerexception when tries selected row's id):
package bean; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.list; import javax.faces.bean.managedbean; import javax.faces.bean.requestscoped; import dao.studentlessoninsertion; import egeproject.connectionclass; import model.lesson; import model.student; import model.studentlesson; @managedbean(name = "studentlessonbean") @requestscoped public class studentlessonbean { studentlessoninsertion studentlessoninsertion; list<lesson> selectedlessons; list<studentlesson> studentlessons; list<studentlesson> filteredstudentlessons; student selectedstudent; public list<lesson> getselectedlessons() { return selectedlessons; } public void setselectedlessons(list<lesson> selectedlessons) { this.selectedlessons = selectedlessons; } public student getselectedstudent() { return selectedstudent; } public void setselectedstudent(student selectedstudent) { this.selectedstudent = selectedstudent; } public void insertstudentlesson() throws sqlexception { studentlessoninsertion = new studentlessoninsertion(); studentlesson studentlesson = new studentlesson(); (int = 0; < selectedlessons.size(); i++) { studentlesson.setlessonid(selectedlessons.get(i).getlessonid()); studentlesson.setstudentid(selectedstudent.getstudentid()); studentlessoninsertion.insertstudentlesson(studentlesson); studentlesson.clear(); } } public list<studentlesson> getfilteredstudentlessons() { return filteredstudentlessons; } public void setfilteredstudentlessons(list<studentlesson> filteredstudentlessons) { this.filteredstudentlessons = filteredstudentlessons; } public list<studentlesson> getstudentlessons() throws sqlexception { studentlessons = new arraylist<studentlesson>(); connectionclass connectionclass = new connectionclass(); connection connection = connectionclass.getdbconnection(); string selectionsql = "select \"student_id\", \"lesson_id\", \"score\", \"student_lesson_id\"\r\n" + " public.\"student_lesson_table\";\r\n" + ""; try { preparedstatement preparedstatement = connection.preparestatement(selectionsql); resultset results = preparedstatement.executequery(); while (results.next()) { studentlesson newstudentlesson = new studentlesson(); newstudentlesson.setstudentid(results.getint("student_id")); newstudentlesson.setlessonid(results.getint("lesson_id")); newstudentlesson.setscore(results.getint("score")); newstudentlesson.setstudentlessonid(results.getint("student_lesson_id")); studentlessons.add(newstudentlesson); } } catch (sqlexception e) { e.printstacktrace(); system.out.println(e); } { connection.close(); } return studentlessons; } }
here models of used classes:
student model:
package model; import javax.faces.bean.managedbean; import javax.faces.bean.requestscoped; import egeproject.studentview; @managedbean(name = "student") @requestscoped public class student { private integer studentid; private integer studentnumber; private string studentname; private string studentclass; public integer getstudentnumber() { return studentnumber; } public void setstudentnumber(integer studentnumber) { this.studentnumber = studentnumber; } public string getstudentname() { return studentname; } public void setstudentname(string studentname) { this.studentname = studentname; } public string getstudentclass() { return studentclass; } public void setstudentclass(string studentclass) { this.studentclass = studentclass; } public student createstudent(int studentnumber, string studentname, string studentclass) { this.setstudentnumber(studentnumber); this.setstudentname(studentname); this.setstudentclass(studentclass); return this; } public student returnstudent() { return this; } public integer getstudentid() { return studentid; } public void setstudentid(integer studentid) { this.studentid = studentid; } public void clear() { this.studentid = null; this.studentclass = null; this.studentname = null; this.studentnumber = null; } }
lesson model:
package model; import javax.faces.bean.managedbean; import javax.faces.bean.requestscoped; @managedbean(name = "lesson") @requestscoped public class lesson { private integer lessonid; private string lessonname; public string getlessonname() { return lessonname; } public void setlessonname(string lessonname) { this.lessonname = lessonname; } public lesson createlesson(string lessonname) { this.setlessonname(lessonname); return this; } public integer getlessonid() { return lessonid; } public void setlessonid(int lessonid) { this.lessonid = lessonid; } public lesson returnlesson() { return this; } public void clear() { lessonid = null; lessonname = null; } }
student lesson model:
package model; import javax.faces.bean.managedbean; import javax.faces.bean.requestscoped; @managedbean(name = "studentlesson") @requestscoped public class studentlesson { private integer studentlessonid; private integer studentid; private integer lessonid; private integer score; public integer getstudentlessonid() { return studentlessonid; } public void setstudentlessonid(integer studentlessonid) { this.studentlessonid = studentlessonid; } public integer getstudentid() { return studentid; } public void setstudentid(integer studentid) { this.studentid = studentid; } public integer getlessonid() { return lessonid; } public void setlessonid(integer lessonid) { this.lessonid = lessonid; } public integer getscore() { return score; } public void setscore(integer score) { this.score = score; } public studentlesson createreturn(student student, lesson lesson) { this.lessonid = lesson.getlessonid(); this.studentid = student.getstudentid(); return this; } public void clear() { this.lessonid=null; this.score=null; this.studentid=null; this.studentlessonid=null; } }
here full stack trace:
warning: #{studentlessonbean.insertstudentlesson()}: java.lang.nullpointerexception javax.faces.facesexception: #{studentlessonbean.insertstudentlesson()}: java.lang.nullpointerexception @ com.sun.faces.application.actionlistenerimpl.processaction(actionlistenerimpl.java:118) @ javax.faces.component.uicommand.broadcast(uicommand.java:315) @ javax.faces.component.uiviewroot.broadcastevents(uiviewroot.java:794) @ javax.faces.component.uiviewroot.processapplication(uiviewroot.java:1259) @ com.sun.faces.lifecycle.invokeapplicationphase.execute(invokeapplicationphase.java:81) @ com.sun.faces.lifecycle.phase.dophase(phase.java:101) @ com.sun.faces.lifecycle.lifecycleimpl.execute(lifecycleimpl.java:118) @ javax.faces.webapp.facesservlet.service(facesservlet.java:593) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:231) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:198) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:96) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:478) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:140) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:80) @ org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:624) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:87) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:342) @ org.apache.coyote.http11.http11processor.service(http11processor.java:799) @ org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:66) @ org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:868) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1455) @ org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:49) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(unknown source) caused by: javax.faces.el.evaluationexception: java.lang.nullpointerexception @ javax.faces.component.methodbindingmethodexpressionadapter.invoke(methodbindingmethodexpressionadapter.java:102) @ com.sun.faces.application.actionlistenerimpl.processaction(actionlistenerimpl.java:102) ... 29 more caused by: java.lang.nullpointerexception @ bean.studentlessonbean.insertstudentlesson(studentlessonbean.java:51) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ org.apache.el.parser.astvalue.invoke(astvalue.java:247) @ org.apache.el.methodexpressionimpl.invoke(methodexpressionimpl.java:267) @ com.sun.faces.facelets.el.tagmethodexpression.invoke(tagmethodexpression.java:105) @ javax.faces.component.methodbindingmethodexpressionadapter.invoke(methodbindingmethodexpressionadapter.java:88) ... 30 more
thank you, sincerely :) happy coding
the problem solved collecting tables , button in same form. tried before page wasn't loading apparently because of "spring java.lang.illegalstateexception: cannot create session after response has been committed" issue. many tonkitchi (https://stackoverflow.com/users/8079165/tonkichi) clearing out problem.
Comments
Post a Comment