将对象追加到会话jsf中的列表对象

时间:2013-06-04 06:35:50

标签: jsf jsf-2 session-variables

我有问题:

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

0 个答案:

没有答案