1

实际上,我在我的应用程序中遵循 mvc-3 层架构。这是我的搜索选项的实现。在我的 servlet(控制器)中,我正在这样做:

        String select = request.getParameter("select");  // getting value
        String search = request.getParameter("search");  // getting value
        request.setAttribute("select", select);
        request.setAttribute("search", search);
        System.out.println("Select : "+select+" Search : "+search);

        int page = 1;
        int recordsPerPage = 20;
        if(request.getParameter("page") != null)
            page = Integer.parseInt(request.getParameter("page"));
        SearchDAO searchDAO=new SearchDAO();
        List<User> list1=searchDAO.searchAllUsers((page-1)*recordsPerPage,recordsPerPage,select,search);  //getting null value,doing SOP of list1
        int noOfRecords = searchDAO.getNoOfRecords();  //getting null value,doing SOP
        int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);  //getting null value,doing SOP
        session.setAttribute("noOfRecords", noOfRecords);
        request.setAttribute("searchList", list1);
        request.setAttribute("noOfPages", noOfPages);
        request.setAttribute("currentPage", page);

在 SearchDAO 中是:

public class SearchDAO {
private int noOfRecords;
Connection connection;
Statement stmt;

    public List<User> searchAllUsers(int offset ,int noOfRecords,String select,String search){
    private static Connection getConnection() throws SQLException,ClassNotFoundException{
    Connection con = ConnectionFactory.getInstance().getConnection();
    return con;  //ConnectionFactory is class for making the connection to DB.
}
     String query="select SQL_CALC_FOUND_ROWS * from info where '"+select+
"' like '%"+search+"%' order by serialNo asc limit 
" + offset + " , " + noOfRecords;

    List<User> list1 = new ArrayList<User>();
    User user1=null;

    try {
        connection = getConnection();
        stmt = connection.createStatement();
        ResultSet rs=stmt.executeQuery(query);
        while(rs.next()){
        user1=new User();
        user1.setSerial(rs.getInt(1));
            user1.setName(rs.getString(2));
            user1.setEmail(rs.getString(3));
            list1.add(user1);
            }

        rs.close();
        rs = stmt.executeQuery("SELECT FOUND_ROWS()");
        if(rs.next())
            this.noOfRecords = rs.getInt(1); 

    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }finally
    {
        try {
            if(stmt != null)
                stmt.close();
            if(connection != null)
                connection.close();
            } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    return list1;

}

public int getNoOfRecords() {
    return noOfRecords;
}
}
  • 已编辑如果我没有遵循正确的 mvc 架构,请随时发表评论并纠正我。
4

3 回答 3

1

AFAIK,创建一个 bean 类,在该类的帮助下,您可以在控制器和 DAO 之间进行通信,您无法使用 request.getAttribute(在您的 DAO 中)来做到这一点。

于 2012-11-28T08:44:40.367 回答
1
  • 检查您是否能够在 request.getParameter() 中检索参数;调用你的 servlet

  • 检查您是否能够在请求范围内存储参数,例如
    request.setAttribute() 调用

于 2012-11-28T08:32:57.690 回答
1

你的担心是对的:

'%"+request.getParameter("search)+"%'
  1. 首先,您从未在 SearchDAO 中初始化请求,并且您在其实例上调用 getParameter,因此 NullPointerException
  2. 其次,您的 seachDAO 将如何获得请求??,它不是一个 servlet,它只是简单的 POJO
于 2012-11-28T08:36:08.347 回答