将服务层添加到Web APP以在jsp上显示DB内容

时间:2014-02-14 11:28:14

标签: java mysql jsp spring-mvc servlets

A Product class representing a real world entity of a product, it should be just a Javabean.

public class Product {
        private Long id; 
        private String name;
        private String description;
        private BigDecimal price;

        // Add/generate getters/setters/c'tors/equals/hashcode boilerplate.
    }

ProductDAO:

public class ProductDAO {

    public List<Product> list() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Product> products = new ArrayList<Product>();

        try {
            connection = database.getConnection();
            statement = connection.prepareStatement("SELECT id, name, description, price FROM product");
            resultSet = statement.executeQuery();

            while (resultSet.next()) {
                Product product = new Product();
                product.setId(resultSet.getLong("id"));
                product.setName(resultSet.getString("name"));
                product.setDescription(resultSet.getString("description"));
                product.setPrice(resultSet.getBigDecimal("price"));
                products.add(product);
            }
        } finally {
            if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
            if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
            if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
        }

        return products;
    }

}

获取列表的Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        List<Product> products = productDAO.list();
        request.setAttribute("products", products); // Will be available as ${products} in JSP
        request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
    } catch (SQLException e) {
        throw new ServletException("Cannot obtain products from DB", e);
    }
}

最后是jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.name}</td>
            <td>${product.description}</td>
            <td>${product.price}</td>
        </tr>
    </c:forEach>
</table>

伙计们,我从Stackoverflow本身得到了这个代码....它类似于我正在处理的项目......我只是想知道我是否想要为这个应用程序添加服务层我必须做些什么改变...我尝试将列表放在另一个方法中,但它给出了一些错误...所以有人请指导我....应该有一个方法从servlet到服务,然后从服务到最后到DAO。请指导我.....

1 个答案:

答案 0 :(得分:1)

您必须创建一个类似于此的新服务类:

public class ProductService {
    ProductDAO productDAO = new ProductDAO();

    public List<Product> list() throws SQLException{
       return productDAO.list();
    }
}

将服务实例代替DAO实例添加到您的servlet中,如下所示:

ProductService productService = new ProductService();

然后修改你的doGet以使用服务而不是DAO:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            List<Product> products = productService.list();
            request.setAttribute("products", products); // Will be available as ${products} in JSP
            request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);

        } catch (SQLException e) {
            throw new ServletException("Cannot obtain products from DB", e);
        }
}

这是一个让您入门的示例。您应该将SQLException处理移动到服务类而不是servlet。