【问题标题】:Spring Mvc Multiple record fetching ExampleSpring Mvc 多条记录获取示例
【发布时间】:2016-09-30 23:36:08
【问题描述】:

我是 Spring 的新手。我试图在 DAO 上获取多条记录,然后将其发送回服务,然后是控制器层,在控制器之后它需要显示在 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 层的代码。 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) 阅读网站规则,bcz 这个问题是不允许的。 2) 学习阅读一般说明,因为它可以帮助理解诸如此类的概念。 3)一旦你有一些你想要帮助的代码回来......
  • 感谢您解释否决票。很多人对问题投了反对票,但没有回答原因。很高兴看到有人真正关心教别人如何帮助别人帮助这个网站。

标签: java spring jsp servlets jdbc


【解决方案1】:

我修复了错误。 . 下面是我来自 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>

【讨论】:

    猜你喜欢
    • 2013-10-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多