【发布时间】:2014-10-31 02:00:42
【问题描述】:
我正在使用 JSF、javabeans 和 GlassFish,并且在使用参数时遇到了 commandLinks 和查询的问题(至少我认为这是造成问题的原因)。
我目前有两个页面,一个名为 listRents.xhtml,它显示了存储在 RentProperty 表中的所有 RentProperty 实体,另一个是要从 listRents.xhtml 页面访问的rentDetails.xhtml。 listRents.xhtml 页面运行良好,因为我只需要执行我创建的命名查询即可从表中获取所有 RentProperty 对象。
在这个页面上,虽然我需要一个 commandLink,它是每个 RentProperty 的 ID,然后它将使用下面的 findRentsById(Long id) 方法转到rentDetails.xhtml 页面以显示对象的所有信息。
目前,当我单击 commandLink 时,它只会刷新 listRents.xhtml 页面而没有结果,甚至不会更改到rentDetails.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;
}
Xhtml 页面: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>
这里演示的是一个屏幕截图,显示了单击 commandLink 之前和之后。前半部分显示使用“#{rentPropertyController.goToListRents}”操作导航到 listRents.xhtml 页面,第二部分显示当我单击 commandLink 时发生的情况。
请注意,该网址似乎仍位于 newRent.xhtml 页面(在 listRents.xhtml 页面上),这是我用来创建新 RentProperty 对象以添加到表中的页面。如果需要,我可以将其包括在内,但我并不认为它是必要的。 Screen shot
这真的让我很困惑,我不知道如何解决它,因为我似乎找不到问题所在。任何帮助表示赞赏。抱歉,格式不正确,我是新手。
编辑:好的,显然,当我在 commandLink 中直接使用 ID 时,它可以工作,但仅在我的 index.xhtml 页面上有效,即使我在 listRents 页面上添加以下内容作为 commandLink,它也不起作用,例如:
action="#{rentPropertyController.showRentDetails(22)}"
关于为什么 rp.id 不起作用的任何想法?
【问题讨论】: