Domanda

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;
}
}
È stato utile?

Soluzione

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();
}

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top