2012-10-31 52 views

回答

1

被配置为一个会话bean的范围是什么?

您是否尝试将其范围更改为请求?

不要忘记尽显你的结果集,这已经是一个CachedRowSet

这里是核心的JavaServer一个例子后,关闭您的连接面书:

import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javax.annotation.Resource; 
import javax.faces.bean.ManagedBean; 
import javax.enterprise.context.RequestScoped; 
import javax.sql.DataSource; 
import javax.sql.rowset.CachedRowSet; 

@ManagedBean 
@RequestScoped 
public class CustomerBean { 
    @Resource(name = "jdbc/mydb") 
    private DataSource ds; 

    public ResultSet getAll() throws SQLException { 
     Connection conn = ds.getConnection(); 
     try { 
      Statement stmt = conn.createStatement(); 
      ResultSet result = stmt.executeQuery("SELECT * FROM Customers"); 
      CachedRowSet crs = new com.sun.rowset.CachedRowSetImpl(); 
      crs.populate(result); 
      return crs; 
     } finally { 
      conn.close(); 
     } 
    } 
} 
+0

它的工作..我将管理bean的范围从会话更改为请求。如果你不介意,你能告诉我请求和会话范围豆之间的基本区别..!谢谢..! :) –

+0

会话范围在会话处于活动状态时保存模型的数据。您可以指定在web.xml中保留多少时间(搜索标记)。这是存储一些数据的好方法,例如在虚拟商店中购物车。会话存储在服务器中,它通常在客户端使用cookie来将客户端绑定到会话。 同时,请求范围寿命短。它在请求中开始并在响应被发回后结束。没有数据将被保存。每个新的请求都会再次构建你的bean。 –

0
'@RequestScoped' should be added to refresh your bean File to get the new data.