0

I am using JSF, javabeans and GlassFish and am having issues with the commandLinks and queries when using parameters (at least that is what I assume is creating the issue).

I currently have two pages, one called listRents.xhtml which shows all of the RentProperty entities stored in the RentProperty table and the other rentDetails.xhtml to be accessed from the listRents.xhtml page. The listRents.xhtml page works fine as I just have to do a named query I created to get all of the RentProperty objects from the table.

On this page though I need to have a commandLink which is the ID of each RentProperty which would then use the findRentsById(Long id) method below to go to the rentDetails.xhtml page to show all of the information of the object.

Currently when I click on the commandLink it just refreshes the listRents.xhtml page with no results and doesn't even change to the rentDetails.xhtml page.

Below is my classes and xthml pages, I am using GlassFish Server 3.1.2 and don't get any errors when the links and methods are used. Note I have removed some basic getters and setters and toString methods and such.

My java Classes:

@Stateless
public class RentPropertyEJB 
{   
    @PersistenceContext(unitName = "MyPU")
    private EntityManager em;

    public List<RentProperty> findRents()
    {
        TypedQuery<RentProperty> query = em.createNamedQuery("findAllRent", RentProperty.class);
        return query.getResultList();
    }

    public RentProperty findRentsById(Long id)
    {
        Query q = em.createQuery("SELECT r FROM RentProperty r where r.id = :id");
        q.setParameter("id", id);
        return (RentProperty) q.getSingleResult();
    }

    public RentProperty createRent(RentProperty rent)
    {
        em.persist(rent);
        return rent;
    }
}

@ManagedBean
@RequestScoped
public class RentPropertyController 
{
    @EJB
    private RentPropertyEJB rentEJB;
    private RentProperty rent = new RentProperty();
    private List<RentProperty> rentList = new ArrayList<RentProperty>();

    public String doCreateRent()
    {
        rent.setType("House");
        rent = rentEJB.createRent(rent);
        rentList = rentEJB.findRents();
        return "listRents.faces";
    }

    public String showRentDetails(Long id)
    {
        rent = rentEJB.findRentsById(id);
        return "rentDetails.faces";
    }

    public String goToListRents()
    {
        rentList = rentEJB.findRents();
        return "listRents.faces";
    }   
}

@Entity 
@NamedQueries({
    @NamedQuery(name = "findAllRent", query = "SELECT r FROM RentProperty r"),
    @NamedQuery(name = "findRentByID", query = "SELECT r FROM RentProperty r where r.id = :id")
})
@Table(name = "RentProperty")
public class RentProperty extends Property
{
    private String furnished;
    private Float rentPrice;

}

The Xhtml pages: listRents.xhtml

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>List of the Rent Properties</title>
    </h:head>
    <h:body>
        <h1>List of the Rent Properties</h1>
        <hr/>
        <h:dataTable value="#{rentPropertyController.rentList}" var="rp" border="1">
            <h:column>
                <f:facet name="header">
                    <h:outputText value="ID"/>
                </f:facet>
                <h:form>
                <h:commandLink value="#{rp.id}" action="#{rentPropertyController.showRentDetails(rp.id)}" />
                </h:form>
            </h:column>

            <h:column>
                <f:facet name="header">
                    <h:outputText value="Description"/>
                </f:facet>
                <h:outputText value="#{rp.description}"/>
            </h:column>

        </h:dataTable>
        <h:form>
            <h:link outcome="newRent.faces" value="Create a new Rent Property"/> |
            <h:link outcome="index.faces" value=" Home"/>
        </h:form>
        <hr/>
    </h:body>
    </html>

rentDetails.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Rent Property Details</title>
</h:head>
<h:body>
    <h1>Rent Property Details</h1>
    <hr/>
    <h:dataTable value="#{rentPropertyController.rent}" var="rp" border="1">
        <h:column>
            <f:facet name="header">
                <h:outputText value="ID"/>
            </f:facet>
            <h:outputText value="#{rp.id}"/>
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="Rent Price"/>
            </f:facet>
            <h:outputText value="#{rp.rentPrice}"/>
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="Number Of bedrooms"/>
            </f:facet>
            <h:outputText value="#{rp.nmOfBedrooms}"/>
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="Number of bathrooms"/>
            </f:facet>
            <h:outputText value="#{rp.nmOfBathrooms}"/>
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="Address"/>
            </f:facet>
            <h:outputText value="#{rp.address}"/>
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="Description"/>
            </f:facet>
            <h:outputText value="#{rp.description}"/>
        </h:column>

        <h:column>
            <f:facet name="header">
                <h:outputText value="Furnished"/>
            </f:facet>
            <h:outputText value="#{rp.furnished}"/>
        </h:column>


    </h:dataTable>
    <h:form>
        <h:link outcome="newRent.faces" value="Create a new Rent Property"/> |
        <h:link outcome="index.faces" value=" Home"/>
    </h:form>
    <hr/>
</h:body>
</html>

index.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<head>
    <title>eBusiness</title>
</head>
<body>
    <h1>eBusiness: Product, Customer, and Order Management</h1>
    <h2>Products</h2>
    <h:form>
        <a href="newRent.faces">Create a new Rent Property</a> |
        <h:commandLink value="List Rent Properties" action="#{rentPropertyController.goToListRents}" /> |
                <h:commandLink value="This one works for some reason" action="#{rentPropertyController.showRentDetails(22)}" /> | 
        <a href="searchRents.faces">Searh for a Rent Property</a><br/>

        <a href="newSale.faces">Create a new Sale Property</a> |
        <h:commandLink value="List Sale Properties" action="#{salePropertyController.goToListSales}" /> |
        <a href="searchSales.faces">Searh for a Sale Property</a><br/>
    </h:form> 
</body>
</html>

To demonstrate here is a screenshot showing the before and after the clicking of the commandLink. The first half shows the listRents.xhtml page when it is navigated to using the '#{rentPropertyController.goToListRents}' action and the second showing what happens when I click on the commandLink.

Note that the web address still seems to be on the newRent.xhtml page (on the listRents.xhtml page) which is the page I use to create a new RentProperty object to add to the table. I can include this if needed, but I don't really see it as necessary. Screen shot

This is really confusing me and I have no idea how to go about fixing it as I can't seem to find the issue. Any help is appreciated. Sorry for any bad formatting, I am new to using this.

Edit: Ok, so apparently when i directly use a ID in the commandLink it works but only on my index.xhtml page, even when I add the following as the commandLink on the listRents page it doesn't work, for example:

action="#{rentPropertyController.showRentDetails(22)}"

Any ideas on why the rp.id isn't working?

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513

1 Answers1

-1

It seems like your using Request Scope.In JSF , there are different set of scope available. Each scope used for different purpose.

Your are mentioned @RequestScoped Managed Bean. Ex :

@RequestScoped Bean lives as long as the HTTP request-response lives. It get created upon a HTTP request and get destroyed when the HTTP response associated with the HTTP request is finished.

@ViewScoped Bean lives as long as user is interacting with the same JSF view in the browser window/tab. It get created upon a HTTP request and get destroyed once user postback to a different view.

ashokhein
  • 1,042
  • 11
  • 37
  • Oh don't worry, you put me on the right track, I ended up using SessionScoped. Thanks for the help! :) – user265502 Sep 06 '14 at 06:22
  • -1 for [plagiarism](http://meta.stackexchange.com/questions/160077/users-are-calling-me-a-plagiarist-what-do-i-do) of http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope Instead of blatantly copypasting my words, put it in quote blocks with reference links, or use your own words. – BalusC Sep 06 '14 at 06:55