【问题标题】:Passing parameters between two jsf pages with parameter conversion带参数转换的两个jsf页面之间传递参数
【发布时间】:2014-01-15 19:51:50
【问题描述】:

我是 jsf 的新手,我想在单击 h:link 时显示问题的详细信息,为此我需要在两个 jsf 页面之间传递问题 ID(我有一个详细信息页面和另一个页面显示问题列表以显示其详细信息)但此 id 是 Long 类型,因此我需要对其进行转换以便能够在数据库中查找它,但我不知道该怎么做。

我用谷歌搜索,但搜索后我写的代码不起作用。这是代码

               <p:dataList value="#{questionBean.questionsForums}" var="quests"
                        itemType="none">
                        <p:panel >
                            <f:facet name="header">
                            <h:outputText value="#{quests.nbupvote}"
                                style="margin-right:30px;color:#cdcdcd" class="voteBox" />
                                <h:outputText class="font-custom" value="#{quests.titre}" />
                            </f:facet>
                            <h:link value="Consulter Détails" outcome="question">
                             <f:param name="quesId" value="#{quests.quesPk}"  >
                            </f:param> 
                            </h:link>
                            </p:panel>
                    </p:dataList>

这是 managedBean 的代码

                  private String quesPk;
           private Question detailQuestion;

public Question getDetailQuestion() {
    return detailQuestion=qDao.selectDetail(Long.valueOf(quesPk));
}

public void setDetailQuestion(Question detailQuestion) {
    this.detailQuestion = detailQuestion;
}

日志中的这个例外

                    2014-01-16T21:11:22.145+0100|Grave: Error Rendering View[/question.xhtml]
                    javax.el.ELException: /question.xhtml @52,24 value="#{questionBean.detailQuestion}": java.lang.NumberFormatException: null
at  com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIData.getValue(UIData.java:732)
                    Caused by: javax.el.ELException: java.lang.NumberFormatException: null
at javax.el.BeanELResolver.getValue(BeanELResolver.java:368)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstValue.getValue(AstValue.java:140)
at com.sun.el.parser.AstValue.getValue(AstValue.java:204)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
... 53 more
                     Caused by: java.lang.NumberFormatException: null
at java.lang.Long.parseLong(Long.java:404)
at java.lang.Long.valueOf(Long.java:540)
at com.portail.managedBeans.QuestionBean.getDetailQuestion(QuestionBean.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606) 
                   2014-01-16T21:11:22.220+0100|Avertissement: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
                   java.lang.NumberFormatException: null
at java.lang.Long.parseLong(Long.java:404)
at java.lang.Long.valueOf(Long.java:540)
at com.portail.managedBeans.QuestionBean.getDetailQuestion(QuestionBean.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:363)  

这是问题细节的代码

                         <h:head>
                         <f:metadata>
                        <f:viewParam name="quesId" value="#{questionBean.quesPk}" converter="javax.faces.Long" />
                          </f:metadata>
                           </h:head>

                         <h:form>

                    <p:dataList value="#{questionBean.detailQuestion}" var="quests"
                        itemType="none">
                    <h:outputText value="1" style="margin-right:30px;color:#cdcdcd"
                        class="voteBox" />
                    <h3><h:outputText class="font-custom"
                        value="#{quests.titre}" /></h3><br/><p:separator></p:separator>
                    <h:outputText class="font-custom"
                        value="#{quests.contenu}" />
                        <div class="navfooter" style="margin-left: 20%">
            <ul>
                <li><a href="profil.xhtml">Modifier</a></li>

                <li style="margin-left: 40%">Auteur</li>
            </ul>

        </div>
        </p:dataList>

我正在使用 eclipse kepler 和 jsf2 和 primefaces。如果你知道怎么做,请告诉我

【问题讨论】:

  • 删除f:convertNumber 行。转换器已用于 f:param。
  • 我删除了它,但 f:param 中的转换器无法正常工作我收到此异常com.sun.faces.mgbean.ManagedBeanPreProcessingException: Erreur inattendue lors du traitement du bean géré «questionBean» Caused by: com.sun.faces.mgbean.ManagedBeanPreProcessingException: Erreur inattendue lors du traitement de la propriété gérée «quesPk» at com.sun.faces.mgbean.ManagedBeanBuilder.bake(ManagedBeanBuilder.java:117) at com.sun.faces.mgbean.BeanManager.preProcessBean(BeanManager.java:353) ... 68 more Caused by: java.lang.IllegalArgumentException: can't parse argument number: param.quesId

标签: jsf-2 type-conversion


【解决方案1】:

@ManagedProperty 不是转换提交值并将其设置为 bean 属性的正确工具。 HTTP 请求参数本质上是字符串。最好使用&lt;f:viewParam&gt;,您可以像对&lt;h:inputText&gt; 那样附加一个转换器。

源页面:

<h:link value="Consulter Détails" outcome="question">
    <f:param name="quesId" value="#{quests.quesPk}" />
</h:link>

目标页面(question.xhtml):

<f:metadata>
    <f:viewParam name="quesId" value="#{questionBean.quesPk}" converter="javax.faces.Long" />
</f:metadata>

private Long quesPk; // +setter

另见:


与具体问题无关,在 getter 方法中执行业务逻辑是个坏主意。不要那样做。使用&lt;f:event type="preRenderView"&gt;&lt;f:viewAction&gt; 监听方法。

另见:

【讨论】:

  • 这很有用,但我怎样才能获得该参数以将其发送到另一个 jsf 页面(详细信息页面),因为按照您所说的进行操作后,我无法访问详细信息页面,它会返回 java.lang.NumberFormatException: null at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)i很好地更新我的问题中的代码
  • 阅读答案底部的“无关”说明。顺便说一句,您应该有一个private Long quesPk; 属性,而不是String 属性。如果在 prerender/viewaction 期间它仍然是 null,那么显然参数根本不存在或参数名称中有错字。
猜你喜欢
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多