0

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?

3 Answers 3

2

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]}"> 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Daniel, thx for your Response. With your Tip I got it work. One confusing think left. I tried it with the Output Label from Primefaces and got a Nullpointer all the Time. Then, same Code with the Label from JSFCore and all worked fine.
1

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

Comments

-1

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> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.