Reputation: 185
I am a newbie on jsf i want to display the details of a question when i click on a h:link and to do so i need to pass the question id between two jsf pages (i have a detail page and another page that display a list of questions to display their details) but this id is of type Long so i need to convert it in order to be able to look for it in the database but i don't know how to do so.
I google it but the code i wrote after the search don't work. Here's the code
<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>
and this the code of the 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;
}
And this in the log with the exception
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)
And this is the code of the details of the question
<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>
I am using eclipse kepler and jsf2 and primefaces. If you know how it can be done please tell me
Upvotes: 0
Views: 3202
Reputation: 1108722
@ManagedProperty
is not the right tool for the job of converting a submitted value and setting it as a bean property. HTTP request parameters are inherently strings. Better use <f:viewParam>
, you can attach a converter to it like as you would do to <h:inputText>
.
Source page:
<h:link value="Consulter Détails" outcome="question">
<f:param name="quesId" value="#{quests.quesPk}" />
</h:link>
Target page (question.xhtml
):
<f:metadata>
<f:viewParam name="quesId" value="#{questionBean.quesPk}" converter="javax.faces.Long" />
</f:metadata>
With
private Long quesPk; // +setter
Unrelated to the concrete problem, doing business logic in getter methods is a bad idea. Don't do that. Use <f:event type="preRenderView">
or <f:viewAction>
listener method.
Upvotes: 1