0

I have a mySql table of PROJECTS, which I am displaying as a list in the index.xhtml. The projectid column contains hyperlinks. When they're clicked I would like the specific projectid row selected to be passed as the query argument into another jsf file (ListProjects.xhtml) which displays all the project values referring to the projectid selected in the index.xhtml. The index.xhtml page seems to pass the values correctly (when hovering over the selection the url displays the right id value). When actually clicking the selction I get a blank page in the resulting ListPprojects.xhtml:

I'm not sure if it's my choice of tags on index.xhtml where should be replaced by another tag like CommandLink which maps to a bean action.

Alternatively, is tag in the result page (ListProject.xhtml) the right tag to use to retrieve the projectid value as the query argument?

Also if the projectid is an int, should this first be converted to String by using parseInt or other method?

Is the problem in one of the session beans where projectid is resulting in a null value which causes the result page to render blank records? Any advice is greatly appreciated. Thanks in advance!

The named query in the entity bean (projects.java) is:

@NamedQuery(name = "Projects.findByProjectid", query = "SELECT p FROM Projects p WHERE    p.projectid = :projectid")  

In the index.xhtml I use the following tag to display the hyperlink:

<h:link value="#{item.projectid}" outcome="ListProject">  
<f:param name="projectid" value="#{item.projectid}"/>  
</h:link>

The ListProjects.xhtml is the page where I would like to display the project details based on the projectid selected in the index.xhtml (and here's where I start getting confused):

<h:column>  
<f:facet name="header">  
<h:outputText value="Countryid"/>  
</f:facet>  
<h:commandLink action="#{selProjectMgtBean.selProjectList}" value="#{item.projectid}"/>  
</h:column>  

My session bean:

@Stateless
public class ProjectSelectedBean implements ProjectSelectedBeanLocal {

@PersistenceContext(unitName = "QueryTrialNoMavenPU")
private EntityManager em;


@Override
public List<Projects> getSelectedProjects(String projectid) {
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    projectid=request.getParameter(projectid);
    return em.createNamedQuery("Projects.findByProjectid", Projects.class).setParameter    ("projectid", projectid).getResultList();
}
}

Finally the calls the methods for rendering the selection:

package com.manaar.beans;

imports....

@Named(value = "selProjectMgtBean")
@RequestScoped
public class SelProjectMgtBean {

@ManagedProperty(value="#{item.projectid}")
private String projectid;

private List<Projects> selProjectList;
@EJB
private ProjectSelectedBeanLocal projectSelectedBeanLocal;

@PostConstruct
public void init() {
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance    ().getExternalContext().getRequest();   
    projectid=request.getParameter("projectid");
    selProjectList = projectSelectedBeanLocal.getSelectedProjects();
}

public List<Projects> getSelProjectList() {
    return selProjectList;
}

public void setSelProjectList(List<Projects> selProjectList) {
    this.selProjectList = selProjectList;
}
}
jay tai
  • 419
  • 1
  • 17
  • 35

2 Answers2

0

you didn't set :projectId in query:
you can change code as follow:

public List<Projects> getSelectedProjects(String projectId) {
    return em.createNamedQuery("Projects.findByProjectid", Projects.class).setParameter("projectId" , projectId).getResultList();
}
Hamid Samani
  • 445
  • 5
  • 15
  • Hamid, Thanks a lot. That's a really helpful suggestion which I fixed in my code. The query exception is resolved but the result comes out blank (in ListProjet.xhtml). I have 3 questions about what could be wrong. 1) projectid is an int so should I be using parsetInt or some other method to convert from int to String? 2) Is the the right thing to use in index.xhtml or should I consider using another tag (ie: to refer to a bean action instead? 3) Is it correct to use the commandLink tag in LilstProjects.html to RETRIEVE the project id? – jay tai Dec 30 '13 at 02:54
  • you're welcome. if the asnwer was helpful accept the asnwer ,for another questions I suggest searching in stackoverflow, you can find answer for them, if you don't find, just submit a new question. – Hamid Samani Dec 30 '13 at 06:39
  • I accepted the answer. Apologies for missing it earlier. My issue remains unresolved and I am still unable to pass query arguments correctly. If I submit a new question it will be almost identical to the modified question above. So should I still submit a new question or continue on this thread? – jay tai Dec 30 '13 at 10:18
  • briefly check this link for [`` vs ``](http://stackoverflow.com/a/4317723/1004867) , for convertor just declare projectId as `int` inside cdi bean, and finally check your accepted asnwer. – Hamid Samani Dec 30 '13 at 13:30
  • I think it's clear that the commandLink is probably the most appropriate tag but this only answers PART of my issue. I'm going to accept your answer as having helped significantly but the issue remains unresolved. I think the problem lies in my use of the session beans and possibly I have to expose DataModel or ListDataModel classes for the request page to pass to the result page and pass a String to display the details. The way I have structured my bean classes looks wrong! – jay tai Dec 30 '13 at 21:27
0

Regarding one of your questions: Use h:link for pure navigational purposes (with the outcome parameter) and h:commandLink to invoke an action directly (with the action parameter). The called method in this case should return a String telling JSF where to navigate next.

Hubert Schumacher
  • 1,683
  • 1
  • 16
  • 25
  • Thank you very much. You helped me clarify that commmandLink is the appropriate method to use in my tags. I now need to figure out what's going wrong in my beans and the answer could be related to using DataModel or LIstDataModel methods – jay tai Dec 30 '13 at 21:24