Reputation: 360
I have a question about filling a Primafaces Output Label via Method Call. The method also required a parameter.
The Label looks like followed:
<p:dataTable id="answerToQuestionDialogTable" var="answer" value="#{allQuestionBean.answers}">
<p:column headerText="Counts For this Answer">
<p:outputLabel value="#{allQuestionBean.votingCounter}">
<f:param name="id" value="#{answer.answerId}"/>
</p:outputLabel>
</p:column>
</p:dataTable>
In my Backing Bean i have an Integer Field named "votingCounter" with the followed Getter:
public int getVotingCounter() {
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
int answerID = Integer.parseInt(params.get("id"));
return answeredDAO.getCountForAnswer(answerID);
}
If I try to load the Site i get following LogOutput from my AppServer(Tomcat 6):
04.09.2012 04:30:47 com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback visit
SCHWERWIEGEND: javax.el.ELException: /pages/allQuestion.xhtml @69,81 value="#{allQuestionBean.votingCounter}": Error reading 'votingCounter' on type bean.view.AllQuestionBean
Can anyone explain me why this won´t work and can give me a solution how I can call the method to fill the Label with Text?
Upvotes: 0
Views: 2353
Reputation: 37061
Since you are going to bring all count answers for all answers anyway , why don't you create a map and populate it in your @PostConstruct
and access it the simple way , something like
<p:outputLabel value="#{AllQuestionBean.countForAnserMap[answer.answerId]}">
Upvotes: 2
Reputation: 1108972
First of all, you should read the root cause of the stack trace to learn about the root cause of your concrete problem. The root cause of the stack trace is the bottommost part of the stack trace.
In your particular case, it's likely just an ordinary NullPointerException
pointing to the line where you're attempting to do Integer.parseInt(params.get("id"))
. This root cause of the exception tells so much more about the concrete problem. It's telling that params.get("id")
returned null
which in turn basically means that the <f:param>
doesn't work this way.
Indeed, this isn't the purpose of the <f:param>
at all. The getRequestParameterMap()
returns the real HTTP request parameters, not the <f:param>
values which you embedded in an arbitrary component which doesn't generate a link/button which points to an URL with those parameters which are only available in that request (and not the current request!).
You should instead implement the solution as suggested by Daniel, or to move the votingCounter
to the Answer
class, or to implement an alternate approach such as follows:
public int getVotingCounter() {
FacesContext context = FacesContext.getCurrentInstance();
Integer answerID = context.getApplication().evaluateExpressionGet(context, "#{answer.answerId}", Integer.class);
return answeredDAO.getCountForAnswer(answerID);
}
Upvotes: 1
Reputation: 14731
Try as follows, presume that you have getter and setter for votingCounter
<p:dataTable id="answerToQuestionDialogTable" var="answer" value="#{AllQuestionBean.answers}">
<p:column headerText="Counts For this Answer">
<p:outputLabel value="#{AllQuestionBean.votingCounter}">
<f:param name="id" value="#{answer.answerId}"/>
</p:outputLabel>
</p:column>
</p:dataTable>
Upvotes: -1