我有问题:
@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>