我是jsf上的新手我想显示问题的详细信息当我点击啊:链接并且这样做我需要在两个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。如果你知道怎么做,请告诉我
答案 0 :(得分:1)
@ManagedProperty
不是转换提交值并将其设置为bean属性的正确工具。 HTTP请求参数本质上是字符串。更好地使用<f:viewParam>
,您可以像对<h:inputText>
那样附加转换器。
来源页面:
<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方法中执行业务逻辑是一个坏主意。不要这样做。使用<f:event type="preRenderView">
或<f:viewAction>
侦听器方法。