JSP 为什么控制器中的API都不工作?

gdrx4gfi  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(107)

我是一个初学者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中指定的相同位置:x1c 0d1x
以下是我的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的给予结果。

gmxoilav

gmxoilav1#

请具体说明以下内容:

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


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

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


对于jar打包的应用程序,删除提供的部分,您可以通过查看<packaging></packaging>标记在pom.xml中检查它。

相关问题