Primefaces OutputLabel由param方法填充

时间:2012-09-04 02:44:40

标签: java jsf parameters primefaces

我有一个关于通过方法调用填充Primafaces输出标签的问题。该方法还需要一个参数。

标签如下所示:

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

在我的Backing Bean中,我有一个名为“votingCounter”的整数字段,后跟Getter:

public int getVotingCounter() {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        int answerID = Integer.parseInt(params.get("id"));

        return answeredDAO.getCountForAnswer(answerID);
    }

如果我尝试加载站点,我会从AppServer(Tomcat 6)获取LogOutput:

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

任何人都可以解释为什么这不起作用,并且可以给我一个解决方法,我可以调用方法来用文本填充标签吗?

3 个答案:

答案 0 :(得分:2)

由于您要为所有答案提供所有计数答案,为什么不创建地图并将其填充到@PostConstruct中并以简单的方式访问它,例如

<p:outputLabel value="#{AllQuestionBean.countForAnserMap[answer.answerId]}">

答案 1 :(得分:1)

首先,您应该阅读堆栈跟踪的根本原因,以了解具体问题的根本原因。堆栈跟踪的根本原因是堆栈跟踪的最底部。

在您的特定情况下,它可能只是一个普通的NullPointerException指向您尝试Integer.parseInt(params.get("id"))的行。异常的根本原因更多地讲述了具体问题。这说明params.get("id")返回了null,这反过来基本上意味着<f:param>不会以这种方式运作。

实际上,这根本不是<f:param>的目的。 getRequestParameterMap()返回真实的HTTP请求参数,而不是您嵌入在任意组件中的<f:param>值,该组件不生成指向URL的链接/按钮,这些参数仅包含在 请求(而不是当前请求!)。

您应该按照Daniel的建议实施解决方案,或者将votingCounter移至Answer类,或者实施替代方法,如下所示:

public int getVotingCounter() {
    FacesContext context = FacesContext.getCurrentInstance();
    Integer answerID = context.getApplication().evaluateExpressionGet(context, "#{answer.answerId}", Integer.class);
    return answeredDAO.getCountForAnswer(answerID);
}

答案 2 :(得分:-1)

尝试如下,假设你有votingCounter

的getter和setter
<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>
相关问题