0

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

2
  • Remove f:convertNumber line. converter is already used for f:param. Commented Jan 15, 2014 at 19:59
  • i removed it but the converter in the f:param is not working i am getting this exception 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 Commented Jan 15, 2014 at 20:18

1 Answer 1

1

@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 

See also:


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.

See also:

Sign up to request clarification or add additional context in comments.

2 Comments

This was usueful but how can i get that parameter to send it to the other jsf page (the detail page) because after proceeding as you said i cant acess the detail page it reurns a java.lang.NumberFormatException: null at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114) i well update the code in my question
Read the "unrelated" note at the bottom of the answer. By the way, you should have a private Long quesPk; property, not a String one. If it's still null during prerender/viewaction, then apparently the parameter is simply not there or there's a typo in parameter name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.