java - Spring项目-在jsp页面中显示mysql表数据

标签 java mysql spring

亲爱的专业人士,我正在尝试在 jsp 页面(Netbeans、Spring 项目)中显示 mysql 表中的数据。为此,我创建了模型类:

    public class Impression {    
    private int id;
    private String username;
    private String text;    

    public static List<Impression> listAllImpressions() throws SQLException, ClassNotFoundException {
        List<Impression> listImpression = new ArrayList<>();        
        Class.forName("com.mysql.jdbc.Driver");

        try (java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/guestbook", "root", "");) {
            Statement st = conn.createStatement();
            st.executeQuery("SELECT impression_id, username, text FROM impression");
            ResultSet rs = st.getResultSet();
            while (rs.next()) {
                int id  = rs.getInt("impression_id");
                String username = rs.getString("username");
                String text = rs.getString("text");
                Impression impression = new Impression(id, "aaa", text);
                listImpression.add(impression);
            }
        }        
        return listImpression;
    }
}

然后创建了一个 Controller :

@Controller
public class ImpressionController {

    @RequestMapping(value = "/newjsp", method = RequestMethod.GET)
    public String allImpressions(@ModelAttribute("impressions") Impression impression, ModelMap model) throws ClassNotFoundException, SQLException {
        Impression impressions = new Impression();
        List<Impression> listImpressions = Impression.listAllImpressions();
        return "newjsp";
    }

当我尝试在 jsp 页面(newjsp)中显示数据时,我什么也没有得到:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<!DOCTYPE html>
<html>
    <body>
    <center>
        <h1>List of user impressions</h1>
    </center>
    <div align="center">
        <table border="1" cellpadding="3">
            <tr>
                <th>ID</th>
                <th>Username</th>
                <th>Text</th>
            </tr>
            <c:forEach var="impressions" items="${listAllImpressions}">
                <tr>
                    <td><c:out value="${impressions.id}" /></td>
                    <td><c:out value="${impressions.username}" /></td>
                    <td><c:out value="${impressions.text}" /></td> 
                </tr>
            </c:forEach>
        </table>
    </div>   
</body>
</html>

我对 Java Spring MVC 还很陌生。任何帮助将不胜感激。 :)

最佳答案

您可能应该首先验证列表是否确实包含项目。

关于java - Spring项目-在jsp页面中显示mysql表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57963477/

相关文章:

java - 什么时候使用 Volatile 修饰符?

mysql - 为了最小化笛卡尔积,在 FROM 子句中使用子查询是否更好?

mysql - 在数据库中存储两个位置之间的路线

java - 在 Spring 中处理空的 RequestBody

java - 在 Redis 重启时自动将 Storm Topology 重新连接到 Redis Cluster

java - 统计分析

MySQL 计数不会与我的其余数据一起返回空计数。

javascript - 从 Angular 传递时,Spring Boot 无法识别路径变量数据

java - 什么时候在 Spring 中使用构造函数注入(inject)?

java - 随机掷2个骰子(java)