0

我有验证:

   @ManagedProperty(value = "#{param.id}")
    private int id;
    List<Product> listviewd;
    List<Product> getSessionviewed;

以及设置List<Product>为会话的方法

public void setViewedSession() {
    try {
        getSessionviewed = new ArrayList<Product>();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
        if (!session.isNew()) {
        getSessionviewed = getViewedSession();
            cnn = classConnection.getInstance().getConnect();
            String sql = "SELECT * FROM Products WHERE ProductID= ? AND StatusExist = 1";
            PreparedStatement ppr = cnn.prepareCall(sql);
            ppr.setInt(1, getIdparam());
            rs = ppr.executeQuery();
            if (rs.next()) {
                getSessionviewed.add(new Product(rs.getInt("ProductID"), rs.getInt("CategoryID"), rs.getInt("Quantity"), rs.getString("ProductName"), rs.getFloat("UnitCost"), rs.getFloat("UnitCost_sale"), rs.getString("PrDescription"), rs.getString("PrSumary"), rs.getString("Image"), rs.getInt("StatusExist")));
            } else {
                getRedrect();
            }
            session.setAttribute("listvied", getSessionviewed);
        }

    } catch (SQLException ex) {
        Logger.getLogger(SessionProduct.class.getName()).log(Level.SEVERE, null, ex);
    }
}

我使用一种方法从我的会话中获取属性并始终接收最新的对象:

public List<Product> getViewedSession() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
        listviewd = (List<Product>) session.getAttribute("listvied");
        return listviewd;
    }

我如何在我的会话listviewd中追加List<Product>

4

0 回答 0