无法使用Servlet作为控制器和会话bean显示数据

时间:2017-03-06 14:11:14

标签: java jsp servlets netbeans ejb

我目前正在尝试显示数据库中的属性列表。我使用工具支持从数据库自动生成了实体类。我创建了DAO类来访问实体类。我使用servlet访问DAO并尝试将属性设置为从DAO类返回的结果,当我尝试使用JSTL foreach语句从JSP页面循环遍历该属性时,它不起作用。这是我的代码:

财产实体:

@Entity
@Table(name = "PROPERTY")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Property.findAll", query = "SELECT p FROM Property p")})
public class Property implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "PROPERTYID")
    private Integer propertyid;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 500)
    @Column(name = "DESCRIPTION")
    private String description;
    @Size(max = 50)
    @Column(name = "TYPE")
    private String type;
    @Column(name = "NUMBEROFBEDROOM")
    private Integer numberofbedroom;
    @Column(name = "NUMBEROFBATHROOM")
    private Integer numberofbathroom;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "ISFURNISHED")
    private String isfurnished;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "HASGARDEN")
    private String hasgarden;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "SIZE")
    private String size;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "PRICE")
    private String price;
    @Basic(optional = false)
    @NotNull
    @Column(name = "ENTEREDDATE")
    @Temporal(TemporalType.DATE)
    private Date entereddate;
    @Basic(optional = false)
    @NotNull
    @Column(name = "ENTEREDTIME")
    @Temporal(TemporalType.TIME)
    private Date enteredtime;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "property")
    private Paddress paddress;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "propertyid")
    private Collection<Offer> offerCollection;
    @JoinColumn(name = "PROPERTYOWNERID", referencedColumnName = "PROPERTYOWNERID")
    @ManyToOne(optional = false)
    private Propertyowner propertyownerid;
    @JoinColumn(name = "REALESTATEAGENTID", referencedColumnName = "REALESTATEAGENTID")
    @ManyToOne(optional = false)
    private Realestateagent realestateagentid;

    public Property() {
    }

    public Property(Integer propertyid) {
        this.propertyid = propertyid;
    }

    public Property(Integer propertyid, String description, String isfurnished, String hasgarden, String size, String price, Date entereddate, Date enteredtime) {
        this.propertyid = propertyid;
        this.description = description;
        this.isfurnished = isfurnished;
        this.hasgarden = hasgarden;
        this.size = size;
        this.price = price;
        this.entereddate = entereddate;
        this.enteredtime = enteredtime;
    }

    public Integer getPropertyid() {
        return propertyid;
    }

    public void setPropertyid(Integer propertyid) {
        this.propertyid = propertyid;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Integer getNumberofbedroom() {
        return numberofbedroom;
    }

    public void setNumberofbedroom(Integer numberofbedroom) {
        this.numberofbedroom = numberofbedroom;
    }

    public Integer getNumberofbathroom() {
        return numberofbathroom;
    }

    public void setNumberofbathroom(Integer numberofbathroom) {
        this.numberofbathroom = numberofbathroom;
    }

    public String getIsfurnished() {
        return isfurnished;
    }

    public void setIsfurnished(String isfurnished) {
        this.isfurnished = isfurnished;
    }

    public String getHasgarden() {
        return hasgarden;
    }

    public void setHasgarden(String hasgarden) {
        this.hasgarden = hasgarden;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public Date getEntereddate() {
        return entereddate;
    }

    public void setEntereddate(Date entereddate) {
        this.entereddate = entereddate;
    }

    public Date getEnteredtime() {
        return enteredtime;
    }

    public void setEnteredtime(Date enteredtime) {
        this.enteredtime = enteredtime;
    }

    public Paddress getPaddress() {
        return paddress;
    }

    public void setPaddress(Paddress paddress) {
        this.paddress = paddress;
    }

    @XmlTransient
    public Collection<Offer> getOfferCollection() {
        return offerCollection;
    }

    public void setOfferCollection(Collection<Offer> offerCollection) {
        this.offerCollection = offerCollection;
    }

    public Propertyowner getPropertyownerid() {
        return propertyownerid;
    }

    public void setPropertyownerid(Propertyowner propertyownerid) {
        this.propertyownerid = propertyownerid;
    }

    public Realestateagent getRealestateagentid() {
        return realestateagentid;
    }

    public void setRealestateagentid(Realestateagent realestateagentid) {
        this.realestateagentid = realestateagentid;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (propertyid != null ? propertyid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Property)) {
            return false;
        }
        Property other = (Property) object;
        if ((this.propertyid == null && other.propertyid != null) || (this.propertyid != null && !this.propertyid.equals(other.propertyid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sushan.model.Property[ propertyid=" + propertyid + " ]";
    }

}

DAO会话Bean(LOCAL):

import com.sushan.model.Property;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class PropertyDAO implements PropertyDAOLocal {
    @PersistenceContext(unitName="RealEstateTrialSystemPU")
    private EntityManager em;

    @Override
    public void AddProperty(Property property) {
        em.persist(property);
    }

    @Override
    public void EditProperty(Property property) {
        em.merge(property);
    }

    @Override
    public void DeleteProperty(int propertyId) {
        em.remove(GetProperty(propertyId));
    }

    @Override
    public List<Property> GetAllProperty() {
        return em.createNamedQuery("Property.findAll").getResultList();
    }

    @Override
    public Property GetProperty(int propertyId) {
        return em.find(Property.class, propertyId);
    }

}

Servlet类:

import com.sushan.dao.PropertyDAOLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "PropertyServlet", urlPatterns = {"/PropertyServlet"})
public class PropertyServlet extends HttpServlet {

    @EJB
    private PropertyDAOLocal propertyDAO;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("allProperty", propertyDAO.GetAllProperty());
        request.getRequestDispatcher("/AllProperty.jsp").forward(request, response);
        }

JSP页面:

<table border="1">
            <th>ID</th>
            <th>Description</th>
            <th>Type</th>
            <th>Number Of Bedroom</th>
            <th>Number Of Bathroom</th>
            <th>Furnished State</th>
            <th>Garden</th>
            <th>Size</th>
            <th>Price</th>
            <c:forEach items="${requestScope.allProperty}" var="p">
                <tr>
                    <td>${p.propertyID}</td>
                    <td>${p.description}</td>
                    <td>c:out value=${p.type}</td>
                    <td>c:out value=${p.numberOfBedroom}</td>
                    <td>c:out value=${p.numberOfBathroom}</td>
                    <td>c:out value=${p.isFurnished}</td>
                    <td>c:out value=${p.hasGarden}</td>
                    <td>c:out value=${p.size}</td>
                    <td>c:out value=${p.price}</td>
                </tr>
            </c:forEach>
        </table> 

数据未显示且没有错误消息,并且jsp页面只有没有数据的表头。我在数据库的属性表中有数据。

我有什么遗失的东西吗?

0 个答案:

没有答案