p:dataTable selected value setter doesn't work - jsf

I'm new in JSF and PrimeFaces and I can't understand why my setter for selected value doesn't set:
I've got main XHTML in which after click on <p:commandButton> I want to change selected row. But after changing selection of the row on data setter setSelectedBook() get null value on entrance.
I already set selection and rowKey for <p:dataTable>.
It looks like :
main XTML:
<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://xmlns.jcp.org/jsf/html"
>
<body>
<ui:composition template="./../../WEB-INF/pagesTemplate.xhtml">
<form>
<ui:define name="content">
<p:dataTable id="eventsDT" var="book" value="#{bookHolder.books}"
selectionMode= "single" selection="#{bookController.selectedBook}"
rowKey="#{book.id}">
<p:ajax event="rowSelect"/>
<p:ajax event="rowDblselect" listener="#{bookController.onDoubleRowSelect}" />
<p:column headerText="ID">
<h:outputText value="#{book.id}" />
</p:column>
<p:column headerText="Title">
<h:outputText value="#{book.longName}" />
</p:column>
</p:dataTable>
</ui:define>
<ui:define name="right">
<h:panelGrid columns="1" cellpadding="5">
<p:commandButton id="btnEdit" value="Edit"
action="#{navigationController.moveToBookEditPageSimple}"
style="width: 100px;height: 28px; margin-left: 10%"/>
</h:panelGrid>
</ui:define>
</form>
</ui:composition>
</body>
</html>
BookController that can't get value:
#Named("bookController")
#SessionScoped
#Local(BookView.class)
public class BookController implements Serializable {
public BookController() {
navigator = new NavigationController();
}
NavigationController navigator;
#Inject
BookHolder bookHolder;
private Book selectedBook;
public Book getSelectedBook() {
return selectedBook;
}
public void setSelectedBook(Book selectedBookValue) {
this.selectedBook = selectedBookValue;
}
public void onDoubleRowSelect(SelectEvent event) throws IOException {
String str = navigator.showPage("BookEdit");
FacesContext.getCurrentInstance().getExternalContext().redirect(str + ".xhtml");
}
}
BookHolderBean that contains data:
#Named("bookHolder")
#ApplicationScoped
public class BookHolder {
public BookHolder() {
}
#PostConstruct
void setValues() {
this.books = new ArrayList<>();
for (int i = 0; i < 10; i++) {
this.books.add(new Book(new ParkType(i, i,
"Book #: " + i + " (long name)",
"Book #: " + i + " (short name)",
"Book #: " + i + " (note)")));
}
}
private List<Book> books;
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> parkTypes) {
this.books = parkTypes;
}
}
Class Book:
public class Book extends MainEntitie<Book> implements Serializable, SelectableDataModel<Book> {
#Inject
BookHolder bookHolder;
private final String longName;
private final String shortName;
public Book(BookBase sd) {
super(sd.getId(), false, false, sd.getNote());
this.longName = sd.getLongName();
this.shortName = sd.getShortName();
}
public String getLongName() {
return longName;
}
public String getShortName() {
return shortName;
}
#Override
public int getId() {
return super.getId();
}
#Override
public Object getRowKey(Book t) {
return t.getId();
}
#Override
public Book getRowData(String string) {
for (Book app : bookHolder.getBooks()) {
if (app.getId() == Integer.parseInt(string)) {
return app;
}
}
return null;
}
}

Since you use ui:define, I assume there is a ui:include in your template file (pagesTemplate.xhtml).
If so, that is great, but your included page has some unnecessary stuff, e.g. html and body tag (by the way: use h:body). No need for that.
See second part of this posting to learn how to include an xhtml file into another one.
You will notice that the content of ui:define is kind of "cut&pasted" into the your template file.
So, what about your form tag? It's being ignored (at least, I guess it won't be a parent of your datatable).
Make the h:form (again, use JSF's h:form instead of HTML form) a child of ui:define,
like:
<ui:composition template="/WEB-INF/template.xhtml"
xmlns=.....>
<ui:define name="content">
<h:form>
<p:dataTable ...>
...
</p:dataTable>
</h:form>
</ui:define>
</ui:composition>

Related

How to send values entered in two separate <h:inputText/> elements to a single method in java? [duplicate]

This question already has an answer here:
How to send form input values and invoke a method in JSF bean
(1 answer)
Closed last month.
I have a problem as I mentioned in the topic title. I want to pass the values from two different <h:inputText/> elements as parameters to a method that takes two parameters in Java and send the result to a single <h:outputText/> element. I don't know how to do this.
For this problem, I created the setter and getter methods separately on the CDI Bean to represent both <h:inputText/> elements. Sample codes are as follows.
XHTML Code:
<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Ana Sayfa</title>
</h:head>
<h:body>
<h:form id="indexForm">
<h:outputLabel value="Tarih1"/><br/>
<h:inputText id="tarih1Text" p:placeholder="gg/aa/yyyy" value="#{dateCDIBean.date1}"/>
<br/>
<h:outputLabel value="Tarih2"/><br/>
<h:inputText id="tarih2Text" p:placeholder="gg/aa/yyyy" value="#{dateCDIBean.date2}"/>
<br/>
<h:commandButton id="btn" value="Farkı Hesapla"/>
<br/>
<h:outputText id="cikti" value="#{dataCDIBean.value}"/>
</h:form>
</h:body>
</html>
CDI Bean Code:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSF/JSFManagedBean.java to edit this template
*/
package tr.com.bilisim.cdibeans;
import jakarta.enterprise.context.SessionScoped;
import jakarta.inject.Named;
import java.io.Serializable;
import tr.com.selimfatih.tarihfark.TarihFark;
import tr.com.selimfatih.tarihkontrol.TarihKontrol;
/**
*
* #author Mehmet Fatih ÇİN
*/
#Named
#SessionScoped
public class dateCDIBean implements Serializable {
private String date1;
private String date2;
private String value;
/**
* Creates a new instance of dateCDIBean
*/
public dateCDIBean() {
}
public String getDate1() {
return date1;
}
public void setDate1(String date1) {
TarihKontrol tk1 = new TarihKontrol();
tk1.Kontrol(date1);
if (tk1.hataliMi == false) {
this.date1 = date1;
}
}
public String getDate2() {
return date2;
}
public void setDate2(String date2) {
TarihKontrol tk2 = new TarihKontrol();
tk2.Kontrol(date2);
if (tk2.hataliMi == false) {
this.date2 = date2;
}
}
public String hesapla() {
if (date1 != null && date2 != null) {
TarihFark fark = new TarihFark(date1, date2);
date1 = "";
date2 = "";
return fark.GunFark() + " gün, " + fark.AyFark() + " ay, " + fark.YilFark() + " yıl";
} else {
return "Yukarıdaki alanlara \"gg/aa/yyyy\" ya da \"gg.aa.yyyy\" formatında tarih değeri giriniz.";
}
}
public String getValue() {
return hesapla();
}
public void setValue(String value) {
this.value = value;
}
}
How can I apply the solution in the topic title as an alternative to what I did above? Or can you tell me if this is possible?
<h:form id="indexForm">
<h:outputLabel value="Tarih1"/><br/>
<h:inputText id="tarih1Text" value="#{dateCDIBean.date1}"/>
<br/>
<h:outputLabel value="Tarih2"/><br/>
<h:inputText id="tarih2Text" value="#{dateCDIBean.date2}"/>
<br/>
<h:commandButton id="btn" value="Farkı Hesapla" action="#{dateCDIBean.concdata()}"/>
<br/>
<h:outputLabel id="cikti" value="#{dateCDIBean.value}"/>
</h:form>
public void concdata() {
value = date1 + date2;
PrimeFaces.current().ajax().update("indexForm:cikti");
}

How to use complex EL like A.B(x).C to set a value?

Mojara 2.1.21
I'm using primefaces editor component p:editor. The value attribute in editor is a complex EL-Statement.
<h:form>
<p:datatable value="#{bean.getItems}" var="item">
<p:column>
<p:editor value="bean.A(item).value" />
</p:column>
</p:datatable>
</h:form>
class Bean {
public Entity A (Item i) { return ...}
}
class Entity {
public String getValue();
public void setValue(String);
}
The getter Entity.getValue() is called, but the setter Entity.serValue(String) is not called, if form is submitted.
I suppose it has nothing to do with editor but a common feature of EL. How can I instruct the editor to call a setter if some changes will be made in editor by a user ?
UPDATE
The variant <p:editor value="#{multiEditorBacking.eval(editor).text}" id="textArea" /> has trouble if setter will be called. But <p:editor value="#{editor.text}" id="textArea" /> is ok. The following examples can be used for testing.
<!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: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://primefaces.org/ui"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:o="http://omnifaces.org/ui" xmlns:pe="http://primefaces.org/ui/extensions">
<ui:composition>
<h:head></h:head>
<h:body>
<h:form id="formId">
<p:dataTable value="#{multiEditorBacking.editors}" var="editor" rowIndexVar="index" >
<p:column>
<p:commandButton value="Refresh" actionListener="#{multiEditorBacking.onRefresh(index)}" update="textArea"
process="#this" />
<p:editor value="#{multiEditorBacking.eval(editor).text}" id="textArea" />
<!-- <p:editor value="#{editor.text}" id="textArea" /> -->
</p:column>
</p:dataTable>
<p:commandButton value="Save" />
</h:form>
</h:body>
</ui:composition>
</html>
MultiEditorBean.java
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
#SessionScoped
#Named
public class MultiEditorBacking implements Serializable{
private List<MultiPojo> editors;
private HashMap <Integer, MultiPojo> hash = new HashMap<Integer, MultiPojo>();
#PostConstruct
public void init(){
editors = new ArrayList<MultiPojo>();
MultiPojo m = new MultiPojo();
m.setText("hey1");
editors.add(m);
hash.put(1, m);
m=new MultiPojo();
m.setText("adf2");
editors.add(m);
hash.put(2, m);
m=new MultiPojo();
m.setText("cjd3");
editors.add(m);
hash.put(3, m);
}
public MultiPojo eval (MultiPojo m){
return m;
}
public void onRefresh (int index){
}
public List<MultiPojo> getEditors() {
return editors;
}
public void setEditors(List<MultiPojo> editors) {
this.editors = editors;
}
public HashMap <Integer, MultiPojo> getHash() {
return hash;
}
public void setHash(HashMap <Integer, MultiPojo> hash) {
this.hash = hash;
}
}
MultiPojo.java
public class MultiPojo {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
This works for me, in Mojarra 2.2.5, using EL 2.2. Are you sure you've got that EL version enabled which allows method parameter passing? You need a Servlet 3.x container available (such as Tomcat 7) or you'll need to add the library yourself. However, it seems you've got it as #{multiEditorBacking.eval(editor).text} value for your editors is being properly evaluated.
By the way, your <ui:composition> surrounding <h:head /> and <h:body /> is unecessary. Another thing I don't like from your code is the use of #SessionScoped for pure view matters. Go with #ViewScoped unless you're explicitly dealing with session related stuff.
#SessionScoped
#Named
public class MultiEditorBacking implements Serializable {
private List<MultiPojo> editors;
private HashMap<Integer, MultiPojo> hash = new HashMap<Integer, MultiPojo>();
public MultiEditorBacking() {
editors = new ArrayList<MultiPojo>();
MultiPojo m = new MultiPojo();
m.setText("hey1");
editors.add(m);
hash.put(1, m);
m = new MultiPojo();
m.setText("adf2");
editors.add(m);
hash.put(2, m);
m = new MultiPojo();
m.setText("cjd3");
editors.add(m);
hash.put(3, m);
}
public MultiPojo eval(MultiPojo m) {
return m;
}
public void onRefresh(int index) {
System.out.println("Editor " + index + " refreshed");
}
public List<MultiPojo> getEditors() {
return editors;
}
public void setEditors(List<MultiPojo> editors) {
this.editors = editors;
}
public HashMap<Integer, MultiPojo> getHash() {
return hash;
}
public void save() {
System.out.println("Editors: " + editors);
}
public void setHash(HashMap<Integer, MultiPojo> hash) {
this.hash = hash;
}
public class MultiPojo {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
#Override
public String toString() {
return "MultiPojo [text=" + text + "]";
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
<h:form id="formId">
<p:dataTable value="#{multiEditorBacking.editors}" var="editor"
rowIndexVar="index">
<p:column>
<p:commandButton value="Refresh"
actionListener="#{multiEditorBacking.onRefresh(index)}"
update="textArea" process="#this" />
<p:editor value="#{multiEditorBacking.eval(editor).text}"
id="textArea" />
</p:column>
</p:dataTable>
<p:commandButton value="Save" action="#{multiEditorBacking.save}" />
</h:form>
</h:body>
</html>
See also:
Using EL 2.2 with Tomcat 6.0.24
The solution was to use brackets in EL to get setter called.
<p:editor value="#{(multiEditorBacking.eval(editor)).text}"
id="textArea" />

How do you update the page size of a PrimeFaces datatable dynamically?

I am trying to update the page size of a PrimeFaces datatable dynamically, after the datatable is displayed on the page but I can't seem to be able to do that. I am using a lazy loaded datatable if that matters... putting an EL expression in the rows attribute and doing a table refresh does not work and causes the paginator to return no data.
Any ideas if this is a bug and how to fix it?
Thanks
You need not refresh or update the datatable. Use EL expression in rowsPerPageTemplate attribute of <p:dataTable> and after loading datatable just change the rows number.
<p:dataTable value="#{managedBean.users}" var="user" lazy="true" paginator="true" rows="10" rowsPerPageTemplate="10,25 #{fn:length(managedBean.users)}">
And include this xmlns:fn="http://java.sun.com/jsp/jstl/functions" in page.
What exactly are you trying to do? This works for me (Mojarra 2.1.26, PrimeFaces 3.5):
#ManagedBean
#ViewScoped
public class Bean {
public class Item {
private String name;
public Item(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private List<Item> items;
public Bean() {
Item item1 = new Item("item1");
Item item2 = new Item("item2");
items = Arrays.asList(item1, item2);
}
public List<Item> getItems() {
return items;
}
private int tableSize = 1;
public int getTableSize() {
return tableSize;
}
public void actionSwitchSize() {
tableSize = tableSize == 1 ? 2 : 1;
}
}
<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:body>
<h:form>
<p:dataTable id="table" var="item" value="#{bean.items}"
rows="#{bean.tableSize}" paginator="true">
<p:column>
#{item.name}
</p:column>
</p:dataTable>
<p:commandButton value="switch size" action="#{bean.actionSwitchSize}"
update="table" />
</h:form>
</h:body>
</html>

How to map an object in jsf from backing bean?

I am getting error while running this snippet as /facelet/crew/objectMapGossip.xhtml #14,94 value="#{objcetMapBean.searchCrewParam.staffNum}": Property 'staffNum' not readable on type java.lang.String
please help me out from this small error .. I am new to jsf so stucking at things basic ...
Thanks in advance :-)
This is my backing bean...
import javax.faces.bean.ManagedBean;
#ManagedBean(name = "objcetMapBean")
public class ObjectMapGossip {
private SearchCrew1 searchCrewParam = new SearchCrew1("212","kart","asd");
public SearchCrew1 getSearchCrewParam() {
return searchCrewParam;
}
public void setSearchCrewParam(SearchCrew1 searchCrewParam) {
this.searchCrewParam = searchCrewParam;
}
public String search() {
return "success";
}
}
class SearchCrew1 {
public SearchCrew1() {
super();
}
/**
* #param staffNum
* #param surName
* #param rank
*/
public SearchCrew1(String staffNum, String surName, String rank) {
super();
this.staffNum = staffNum;
this.surName = surName;
this.rank = rank;
}
private String staffNum;
private String surName;
private String rank;
public String getStaffNum() {
return staffNum;
}
public void setStaffNum(String staffNum) {
this.staffNum = staffNum;
}
public String getSurName() {
return surName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
}
This is my jsf 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">
<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:ui="http://java.sun.com/jsf/facelets">
<h:body>
<ui:composition template="/facelet/layout/mainlayout.xhtml">
<ui:define name="content">
<h:form>
<div align="left">
<h:outputText value="Staff Number: " />
<h:inputText id="staffnum" size="6" value="# {objcetMapBean.searchCrewParam.staffNum}" />
<h:outputText value="Surname: " />
<h:inputText id="surname" size="10" maxlength="25" value="# {objcetMapBean.searchCrewParam.surName}" />
<h:outputText value="Rank: " />
<h:inputText id="rank" size="3" value="#{objcetMapBean.searchCrewParam.rank}" />
<h:commandButton value="Search" action="#{objcetMapBean.search}" />
</div>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
We can use h:dataTable from jsf tag and map easily.
http://www.mkyong.com/jsf2/jsf-2-datatable-example/

Primefaces Datatable Selection Object

I have a question to the Primefaces Datatable, especially to the Selection Object.
In my following Code I get always Null for the Variable "Selected Question" which is bound to the Datatable with Selection.
The jsf as followed:
<?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">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:composition template="mainTemplate.xhtml">
<ui:define name="contentTitle">Your Questions</ui:define>
<ui:define name="content">
<h:form id="formAllQuestion">
<p:growl id="allQuestionGrowl" showDetail="true"/>
<p:dataTable id="allQuestionsTable" var="question" value="#{allQuestionBean.allQuestionDataHelper}" paginator="true" rows="10"
selection="#{allQuestionBean.selectedQuestion}" selectionMode="single">
<p:ajax event="rowSelect" listener="#{allQuestionBean.onRowSelect}" update=":formAllQuestion:AnswerToQuestionDialogTable :formAllQuestion:allQuestionGrowl"
oncomplete="questDialog.show()"/>
<p:ajax event="rowUnselect" listener="#{allQuestionBean.onRowUnselect}" update=":formAllQuestion:allQuestionGrowl"/>
<f:facet name="header">Select a Row to display your Question Details</f:facet>
<p:column headerText="QuestionID">
#{question.questionId}
</p:column>
<p:column headerText="Question Name">
#{question.questionName}
</p:column>
<p:column headerText="Question Description">
#{question.questionText}
</p:column>
<p:column headerText="Question Short Description">
#{question.questionShortText}
</p:column>
<p:column headerText="Author">
#{question.professor.profSurename} #{question.professor.profName}
</p:column>
</p:dataTable>
<p:dialog header="Question Details" widgetVar="questionDialog" resizable="true" id="questDialog"
showEffect="fade" hideEffect="fade" modal="true">
<p:dataTable id="AnswerToQuestionDialogTable" var="answer" value="#{allQuestionBean.answers}">
<f:facet name="header">
Hier kommt der QR_Code rein!
#{allQuestionBean.selectedQuestion.questionId} - #{allQuestionBean.selectedQuestion.questionName}
</f:facet>
<p:column headerText="Answer">
<h:outputText value="#{answer.answerText}"/>
</p:column>
<p:column headerText="Counts For this Answer">
<h:outputText value="Bis jetz noch nix!"/>
</p:column>
</p:dataTable>
</p:dialog>
</h:form>
</ui:define>
</ui:composition>
</html>
And the associated Bean Class (AllQuestionBean.class):
#ManagedBean(name = "allQuestionBean")
#ViewScoped
public class AllQuestionBean implements Serializable {
private static final long serialVersionUID = 7038894302985973905L;
#ManagedProperty(value = "#{questionDAO}")
private QuestionDAO questionDAO;
#ManagedProperty(value = "#{profSession.professor}")
private Professor professor;
#ManagedProperty(value = "#{answerDAO}")
private AnswerDAO answerDAO;
#ManagedProperty(value = "#{answeredDAO}")
private AnsweredDAO answeredDAO;
private List<Question> questions;
private Question selectedQuestion;
private List<Answer> answers;
private AllQuestionDataHelper allQuestionDataHelper;
public AllQuestionBean(){
System.out.println("Starting Bean: "+this.getClass().getName());
}
#PostConstruct
public void initVariables(){
questions = questionDAO.readByProfessor(professor);
}
public void onRowSelect(SelectEvent event) {
FacesMessage msg = new FacesMessage("Question Selected", selectedQuestion.getQuestionId()+" -- "+selectedQuestion.getQuestionName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowUnselect(UnselectEvent event) {
FacesMessage msg = new FacesMessage("Question Selected", selectedQuestion.getQuestionId()+" -- "+selectedQuestion.getQuestionName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
//---GETTER and SETTER
public AllQuestionDataHelper getAllQuestionDataHelper() {
allQuestionDataHelper = new AllQuestionDataHelper(questions);
return allQuestionDataHelper;
}
public void setAllQuestionDataHelper(AllQuestionDataHelper allQuestionDataHelper) {
this.allQuestionDataHelper = allQuestionDataHelper;
}
public QuestionDAO getQuestionDAO() {
return questionDAO;
}
public void setQuestionDAO(QuestionDAO questionDAO) {
this.questionDAO = questionDAO;
}
public Professor getProfessor() {
return professor;
}
public void setProfessor(Professor professor) {
this.professor = professor;
}
public AnswerDAO getAnswerDAO() {
return answerDAO;
}
public void setAnswerDAO(AnswerDAO answerDAO) {
this.answerDAO = answerDAO;
}
public AnsweredDAO getAnsweredDAO() {
return answeredDAO;
}
public void setAnsweredDAO(AnsweredDAO answeredDAO) {
this.answeredDAO = answeredDAO;
}
public List<Question> getQuestions() {
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
public Question getSelectedQuestion() {
System.out.println("getSelectedQuestion");
return selectedQuestion;
}
public void setSelectedQuestion(Question selectedQuestion) {
System.out.println("Set selected Question: "+selectedQuestion);
this.selectedQuestion = selectedQuestion;
}
public List<Answer> getAnswers() {
answers = answerDAO.getAllAnswersForQuestion(selectedQuestion);
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
}
The Data Modell:
public class AllQuestionDataHelper extends ListDataModel<Question> implements SelectableDataModel<Question> {
public AllQuestionDataHelper() {
}
public AllQuestionDataHelper(List<Question> list) {
super(list);
}
#Override
public Object getRowKey(Question question) {
if(!(question == null)){
System.out.println("Your Questions --> Getting RowKey");
System.out.println("RowKey: "+question);
System.out.println("RowKey: "+question.getQuestionId());
}else{
System.out.println("Warning Row Key is null");
}
return question.getQuestionId();
}
#Override
public Question getRowData(String rowKey) {
System.out.println("Your Questions --> Getting RowData");
System.out.println("RowData: "+rowKey);
List<Question> questionList = (List<Question>) getWrappedData();
for(Question q : questionList){
if(rowKey.equals(q.getQuestionId())){
System.out.println("Returning "+q.getQuestionId());
return q;
}
}
return null;
}
}
I debugged a few runs and mentioned that the Variable "selectedQuestion" in AllQuestionBean.class is never set. Will say, the event Variable in "onRowSelect" holds a NULL-Object.
As you can see there are two Datatables in the *.xhtml. The first one will load normally, no problems. The onClick-Method of the Bean should launch a Dialog with the Second Datatable, but will quit with a Nullpointer.
For the Datatable I followed the Tutorial at Primefaces (http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionInstant.jsf)
Use rowKey attribute on p:dataTable.
rowKey is a unique Identifier that helps Primefaces engine to return the selected Object based on selection.
Usually the value you supply to the rowKey attribute is the unique property of the POJO that you are populating in to p:dataTable.
If you don't have any such unique fields in your POJO. Then its always useful to make one, for example: int rowId;, which you might increment and put in to POJO while you adding them to the List.

Resources