我想在我的 Spring Boot 项目中使用 JSTL 来查看数据库 mysql 中的数据。我尝试在 pom.xml 中使用依赖项,但是当我使用此标签 <%@ taglib prefix="c" uri="https://jakarta.ee/jstl/core" %> 时,这是错误。我使用 JSTL Jakarta 版本 2.0.0 和 JSTL Jakarta Servlet API 6.0.0 但这不起作用。谁能帮帮我吗!!
基于 Jakarta EE 10 的 Spring Boot 3.x
JSTL
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
对于数据库
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
src/main/resources/application.properties
spring.application.name=hello-jsp
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://localhost:3306/yourDB
spring.datasource.username=your-user
spring.datasource.password=your-password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.sql.DataSource;
import java.util.Map;
@Controller
public class HelloJDBCJSPController {
@Autowired
private DataSource dataSource;
@RequestMapping(value = "/helloDS", method = RequestMethod.GET)
public ModelAndView getDataSource(Map<String, Object> model) {
System.out.println("HelloJDBCJSPController >>>> dataSource = " + dataSource);
model.put("dataSource", dataSource);
// /WEB-INF/jsp/index-tag-003.jsp
return new ModelAndView("index-tag-003");
}
}
src/webapp/WEB-INF/jsp/index-tag-003.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="javax.sql.DataSource" %>
<%@ taglib uri="jakarta.tags.sql" prefix="sql" %>
<%@ taglib uri="jakarta.tags.core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Taglib 003 Example of sql tag</title>
</head>
<body>
<sql:query dataSource="${requestScope.dataSource}" var="result">
SELECT * FROM STUDENTS;
</sql:query>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.id}"/></td>
<td><c:out value="${row.name}"/></td>
<td><c:out value="${row.email}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
正确的 uri 在这里:
<%@ taglib uri="jakarta.tags.sql" prefix="sql" %>
<%@ taglib uri="jakarta.tags.core" prefix="c" %>
Spring Boot war 在 Tomcat 10.1 服务器上运行。
参考数据库MYSQL:
CREATE TABLE STUDENTS (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT,
grade VARCHAR(10),
email VARCHAR(100),
enrollment_date DATE
);
INSERT INTO STUDENTS (name, age, grade, email, enrollment_date)
VALUES
('Alice', 20, 'A', '[email protected]', '2023-01-15'),
('Bob', 22, 'B', '[email protected]', '2023-02-10'),
('Charlie', 21, 'A', '[email protected]', '2023-03-12'),
('David', 23, 'C', '[email protected]', '2023-04-08'),
('Eva', 20, 'B', '[email protected]', '2023-05-05');