Spring Mvc多记录提取示例

时间:2016-06-01 14:21:46

标签: java spring jsp servlets jdbc

我是Spring的新手。我正在尝试在DAO上获取多条记录,然后将其发送回服务,然后再发送到控制器层,在Controller之后需要显示在JSP页面上。我不想使用JDBC模板如何编码。 ??

public ArrayList<userBean> Manage() 
{
    ArrayList<userBean> list = new ArrayList<userBean>();
    System.out.println("manage from dao");
    try
    {
        con=DbConnection.getConnection();
        ps=con.prepareStatement("select * from employee_info");
        rs=ps.executeQuery();
        while(rs.next())
        {
            ub= new userBean();
            ub.setNAME(rs.getString("name"));
            ub.setLASTNAME(rs.getString("lastname"));
            ub.setPASSWORD(rs.getString("password"));
            list.add(ub);
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }




    return list;
}

以上是来自DAO Layer的代码。 userBaen是我的bean类。 我要显示的JSP页面正在加载而没有结果。 以下是我的JSP代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib  uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Manage Results</title>
</head>
<body>
<c:forEach items="${list}" var="l">
<tr>
                <td>${l.name}</td>
                <td>${l.lastname}</td>
                <td>${l.password}</td>
</tr>

</c:forEach>
</body>
</html>

先谢谢。

1 个答案:

答案 0 :(得分:0)

我修正了错误。 。 以下是我的DAO代码,对我有用

public ArrayList<userBean> Manage() 
    { 
        HttpServletRequest req = null;
        HttpServletResponse res;

        ArrayList<userBean> list = new ArrayList<userBean>();
        System.out.println("manage from dao");
        try
        {
            con=DbConnection.getConnection();
            ps=con.prepareStatement("select * from employee_info");
            rs=ps.executeQuery();
            while(rs.next())
            {
                ub= new userBean();
                ub.setNAME(rs.getString("name"));
                ub.setLASTNAME(rs.getString("lastname"));
                ub.setPASSWORD(rs.getString("password"));
                list.add(ub);
            }
        }

        catch(Exception e)
        {
            e.printStackTrace();
        }




        return list;
    }
控制器上的

public ModelAndView Manage(HttpServletRequest request, HttpServletResponse response)
    {
        Bean bean = new Bean();
        Dao d= new Dao();

        try
        {
            List<userBean> rows=d.Manage();
            request.setAttribute("rows", rows);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return new ModelAndView("manageResult");

    }

在JSP页面上

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
           <%@taglib  uri="http://www.springframework.org/tags/form" prefix="form" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <jsp:useBean id="TimeDetailBean" class="com.logic.bean.userBean"   scope="application" />
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Manage Results</title>
    </head>
    <body>
    <center>
    <h1>The Employee_Info Results </h1>
    <table>
    <tr><th>Name</th><th>Last name</th><th>Password</th></tr>
    <c:forEach items="${rows}" var="row">
    <tr>
    <td><input type="text" name="name" value=${row.NAME}></td><td><input type="text" name="lastname" value=${row.LASTNAME}></td><td><input type="text" name="password" value=${row.PASSWORD}></td><td><a href="update.do">UPDATE</a><td><a href="delete.do">DELETE</a></td>
    </tr>

    </c:forEach>
    </table>
    </center>
    </body>
    </html>