【问题标题】:Java Servlet MVC, Show Database Query Result to JSP TableJava Servlet MVC,将数据库查询结果显示到 JSP 表
【发布时间】:2018-11-03 12:04:05
【问题描述】:

请帮助我了解MVC java servlet。我在多行多列上显示数据库查询结果时发现问题(加载所有数据,假设我在查询后得到 10 行),我想在JSP 表中加载所有列表学生。

如何在 servlet 中 setAttribute 和在 JSP 中 getAttribute?

我将查询结果设置为数组列表,这是我的代码:

Bean 类

package com.mvc.bean;

public class StudentBean {
    private String name;
    private String id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
} 

DAO 类

package com.mvc.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import com.mvc.bean.StudentBean;
import com.mvc.util.DBConnection;

public class StudentDao {
    public ArrayList<StudentBean> getStudent() {
        ArrayList<StudentBean> list_student = new ArrayList<StudentBean>();
        Connection con = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            con = DBConnection.createConnection();
            statement = con.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM tbl_student");
            while(resultSet.next()){
                StudentBean studentBean = new StudentBean();
                studentBean.setId(resultSet.getString("id"));
                studentBean.setName(resultSet.getString("name"));
                list_student.add(studentBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list_student;
    }
}

Servlet 类

package com.mvc.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mvc.bean.StudentBean;
import com.mvc.dao.StudentDao;

public class StudentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public StudentServlet() {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StudentDao studentDao = new StudentDao();
        ArrayList<StudentBean> studentList = studentDao.getStudent();

        //I have get value studentBean as array list
        //Here, how to request.setAttribute ? i want passing to jsp table
        request.getRequestDispatcher("/Student.jsp").forward(request, response);
    }
}

JSP

<!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>Student</title>
    </head>
    <body>
        <table>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
        <tr>
            //Here i want get array value from servlet, how to ?
            <td><%=request.getAttribute("?") %></td>
            <td<%=request.getAttribute("?") %>></td>
        </tr>
        </table>
    </body>
</html>

不然呢?

【问题讨论】:

    标签: java html jsp servlets model-view-controller


    【解决方案1】:

    您可以使用 java 代码:request.setAttribute("mylist", studentBean);

    在你的 JSP 中你需要使用request.getAttribute("mylist") 你会得到你的清单。

    对于表格,您需要制作循环以从列表中提取信息。

    【讨论】:

      【解决方案2】:

      已解决,在Servlet Class中添加了request.setAttribute。

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          StudentDao studentDao = new StudentDao();
          ArrayList<StudentBean> studentList = studentDao.getStudent();
      
          //added here
          request.setAttribute("studentList", studentList);
      
          request.getRequestDispatcher("/Student.jsp").forward(request, response);
      }
      

      并在 JSP 中获取studentList。

      在第一行添加@taglib prefix="c":

      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
      <!DOCTYPE html>
      

      在表格中使用forEach提取&lt;tr&gt;标签。

      .getID()和.getName()指的是Bean Class方法:

      <table>
          <tr>
              <th>Id</th>
              <th>Name</th>
          </tr>
          <c:forEach var="studentBean" items="${studentList}">
              <tr>  
                  <td>${studentBean.getID()}</td>
                  <td>${studentBean.getName()}</td>
              </tr>
          </c:forEach>
      </table>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-05
        • 2012-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多