如何在我的 Spring boot 项目中使用 JSTL

问题描述 投票:0回答:1

我想在我的 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 但这不起作用。谁能帮帮我吗!!

任何人都可以帮助我吗!!在此处输入图像描述在此处输入图像描述

java spring spring-boot eclipse jstl
1个回答
0
投票

基于 Jakarta EE 10 的 Spring Boot 3.x

  • 雅加达Servlet 6.0
  • 雅加达标准标签库3.0
  • Spring Boot 3.x
  • JDK 17

pom.xml

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

HelloJDBCJSPController.java

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");
    }

}

index-tag-003.jsp

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 服务器上运行。

001.png

参考数据库MYSQL:

初始化.sql

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');
© www.soinside.com 2019 - 2024. All rights reserved.