在JSF中使用commandLink传递参数时,页面不会改变

在JSF中使用commandLink传递参数时,页面不会改变,jsf,javabeans,facelets,Jsf,Javabeans,Facelets,我正在使用JSF、javabeans和GlassFish,在使用参数时,CommandLink和查询出现了问题(至少我认为这是造成问题的原因) 我目前有两个页面,一个称为listRents.xhtml,它显示了存储在RentProperty表中的所有RentProperty实体,另一个是要从listRents.xhtml页面访问的rentDetails.xhtml。listRents.xhtml页面工作正常,因为我只需要执行一个创建的命名查询,就可以从表中获取所有RentProperty对象 在

我正在使用JSF、javabeans和GlassFish,在使用参数时,CommandLink和查询出现了问题(至少我认为这是造成问题的原因)

我目前有两个页面,一个称为listRents.xhtml,它显示了存储在RentProperty表中的所有RentProperty实体,另一个是要从listRents.xhtml页面访问的rentDetails.xhtml。listRents.xhtml页面工作正常,因为我只需要执行一个创建的命名查询,就可以从表中获取所有RentProperty对象

在这个页面上,我需要一个commandLink,它是每个RentProperty的ID,然后使用下面的findRentsById(Long ID)方法转到rentdails.xhtml页面来显示对象的所有信息

当前,当我单击commandLink时,它只刷新listRents.xhtml页面,没有结果,甚至没有更改到rentdails.xhtml页面

下面是我的类和xthml页面,我使用的是GlassFish Server 3.1.2,在使用链接和方法时不会出现任何错误。注意,我已经删除了一些基本的getter和setter以及toString方法等

我的java类:

@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;

}

关于rp.id为什么不起作用有什么想法吗?

看起来您正在使用请求作用域。在JSF中,有不同的作用域集可用。每个范围用于不同的目的

这里提到的是@RequestScoped托管Bean。例:

@RequestScoped Bean的寿命与HTTP请求-响应的寿命一样长。它在HTTP请求时创建,在与HTTP请求关联的HTTP响应完成时销毁

@ViewScoped
只要用户在浏览器窗口/选项卡中与相同的JSF视图交互,Bean就会存在。它是根据HTTP请求创建的,一旦用户发回另一个视图,它就会被销毁。

哦,别担心,你让我走上了正确的轨道,我最终使用了SessionScoped。谢谢你的帮助!:)-1为了避免明目张胆地复制粘贴我的文字,请将其放在带有参考链接的引用块中,或使用您自己的文字。
 <!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>
<!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>
<!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>
action="#{rentPropertyController.showRentDetails(22)}"