为什么控制器中的 API 都不起作用?

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

我是 Spring Boot 的初学者。这是我的控制器

@Controller
public class StudentController {
    @Autowired
    StudentService studentService;

@GetMapping("/")
public ModelAndView getAllStudents() {
    ModelAndView mav = new ModelAndView("index");
    List<Student> studentList = studentService.getAll();
    mav.addObject("students", studentList);
    return mav;
}

@GetMapping("/add-student")
public ModelAndView addStudent() {
    ModelAndView mav = new ModelAndView("add_student");
    mav.addObject("command", new Student());
    return mav;
}

@PostMapping("/save-student")
public String saveStaff(@ModelAttribute Student student) {
    studentService.save(student);
    return "redirect:/";
}


@GetMapping("/student-update/{id}")
public ModelAndView updateStudent(@PathVariable("id") Integer id) {
    ModelAndView mav = new ModelAndView("add_student");
    Student student = studentService.getById(id);
    mav.addObject("command", student);
    return mav;
}

@GetMapping("/student-delete/{id}")
public String deleteStaff(@PathVariable("id") Integer id) {
    Student deleteStaff = studentService.getById(id);
    studentService.delete(deleteStaff);
    return "redirect:/";
}

这是项目结构,我的 JSP 文件与 application.properties 中指定的位置相同:

这是我的应用程序。属性:

server.port=8080
spring.datasource.url= jdbc:postgresql://localhost:5432/postgres
spring.datasource.username= postgres
spring.datasource.password= pgadmin
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=update

spring.mvc.view.prefix:/WEB-INF/jsp/

spring.view.suffix:.jsp
spring.view.view-names:jsp/*
spring.mvc.view.suffix=.jsp

对于 API

http://localhost:8080/add-student
响应是

{
    "timestamp": "2024-01-02T13:33:48.722+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/add-student/"
}

代码的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springjava.poc</groupId>
    <artifactId>Spring_Boot_CRUD_With_JSP</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Spring_Boot_CRUD_With_JSP</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>16</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

即使我明确设置了 server.servlet.context-path,然后相应地修改 API 并运行仍然会出现错误。代码在 IDE 中运行没有错误,但没有任何 API 给出结果。

spring-boot jsp model-view-controller
1个回答
0
投票

请指定以下内容:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

代替:

spring.mvc.view.prefix:/WEB-INF/jsp/

spring.view.suffix:.jsp
spring.view.view-names:jsp/*
spring.mvc.view.suffix=.jsp

如果您在 Spring Boot 应用程序中使用 JSP,似乎您需要 tomcat-embed-jasper 依赖项。

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

对于 jar 打包的应用程序,删除提供的部分,您可以通过查看

pom.xml
标签在您的
<packaging></packaging>
中检查它。

© www.soinside.com 2019 - 2024. All rights reserved.