首页 > 解决方案 > Spring Boot jsp 文件未找到错误 404 奇怪的前缀

问题描述

我目前正在学习 Spring/Spring Boot,并且正在尝试编写忘记密码功能。我的所有其他功能,如登录等都按预期工作,但由于某种原因重定向到重置密码页面不起作用,并给我以下错误,它会在其中查找带有奇怪前缀的 jsp。

出现意外错误(类型=未找到,状态=404)。

JSP 文件 [ /reset-password /WEB-INF/jsp/reset-password.jsp] 未找到

我的文件夹结构

我生成的链接如下所示:http://localhost:8080/reset-password/{a-random-uuid}

重置密码.jsp

<%@ include file="includes/header.jsp" %>
<div class="panel panel-primary">
  <div class="panel-heading">
    <h3 class="panel-title">Reset your password</h3>
  </div>
  <div class="panel-body">
    <form:form modelAttribute="resetPasswordForm" role="form">
      <form:errors cssClass="error" />
      <div class="form-group">
        <form:label path="password">Type new password</form:label>
        <form:password path="password" class="form-control"
                       placeholder="Password" />
        <form:errors cssClass="error" path="password" />
      </div>
      <div class="form-group">
        <form:label path="retypePassword">Retype new password</form:label>
        <form:password path="retypePassword" class="form-control"
                       placeholder="Retype password" />
        <form:errors cssClass="error" path="retypePassword" />
      </div>
      <button type="submit" class="btn btn-primary">Reset password</button>
    </form:form>
  </div>
</div>
<%@include file="includes/footer.jsp"%>

我的重置控制器:

[imports]

@Controller
@RequestMapping("/reset-password/{resetPasswordCode}")
public class ResetPasswordController {

    private final UserCommandService userCommandService;

    public ResetPasswordController(UserCommandService userCommandService) {
        this.userCommandService = userCommandService;
    }

    @GetMapping
    public String forgotPassword(Model model){
        model.addAttribute(new ResetPasswordForm());
        return "reset-password";
    }

    @PostMapping
    public String resetPassword(@PathVariable String resetPasswordCode, @Validated ResetPasswordForm resetPasswordForm, BindingResult result, RedirectAttributes redirectAttributes){
        if (result.hasErrors()) {
            return "reset-password";
        }
        try{
            userCommandService.resetPassword(resetPasswordCode, resetPasswordForm.getPassword());
            MyUtils.flash(redirectAttributes, "success", "Password was changed");
            return "redirect:/login";
        } catch (NoSuchElementException e) {
            result.reject("Url is invalid");
            return "reset-password";
        }
    }
}

重置密码表

package com.learningspring.springdiproject.dto;

import constraints.Password;
import constraints.RetypePassword;

@RetypePassword
public class ResetPasswordForm {

    @Password
    private String password;

    @Password
    private String retypePassword;


    public String getRetypePassword() {
        return retypePassword;
    }

    public void setRetypePassword(String retypePassword) {
        this.retypePassword = retypePassword;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

属性文件

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

spring.datasource.url=jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
spring.datasource.username= spring
spring.datasource.password= spring
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.javax.persistence.validation.mode= none

最后是我的 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.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.learningspring</groupId>
    <artifactId>springdiproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>springdiproject</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>5.4.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <compilerArgs>
                        <arg>-parameters</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我很确定我的 pom 和控制器是正确的,因为我看不出这个控制器和我的其他控制器之间的区别。

注意:Password 和 Retype password 只是自定义约束,检查密码的大小以及两个密码是否为数学形式。

我想在某个地方,Viewcontroller 搞砸了,把前缀放在那里。但我不知道这可能来自哪里。其他有同样问题的人在他们的 pom 中没有碧玉或错误的文件夹结构,但我仔细检查了这些错误。

标签: javaspringspring-bootspring-mvcjsp

解决方案


尝试将以下属性更改为以/

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

这将允许 spring 搜索webapp/WEB-INF/jsp路径的子文件夹。


推荐阅读