Spring MVC 找不到Spring引导HTML资源

rnmwe5a2  于 2023-11-22  发布在  Spring
关注(0)|答案(1)|浏览(166)

我无法访问我的静态html文件在Spring Boot 。我尝试了不同的配置,但我仍然得到资源找不到错误
首先,我尝试使用JSP,它不工作,然后我切换到thymeleaf它确实工作,这是因为JPS的限制,最后我使用html。
我的控制器工作正常,我使用RestControllerController进行单独的测试运行,它们工作正常,thymeleaf需要在类级别声明RequestMapping才能工作,但在html的情况下,我被卡住了,然后我发现了这个

spring.mvc.view.prefix: /resources/**
spring.web.resources.chain.strategy.content.enabled=true
spring.web.resources.chain.strategy.content.paths=/**
spring.mvc.contentnegotiation.favor-parameter=true

字符串
我能够访问静态资源,但没有控制器,他们没有与控制器和我得到资源找不到错误。
为了您的信息,我尝试将html文件放置在main/webapps/WEB-INF/,**/resources/static/,/resources/templates/**中,目前我能够访问它们,因为它们位于resources/static/中,并且我在/resources/public/error/404.html中有404.html每次使用端点时,我都会获得404.html,但控制器仍然无法工作
这是我的控制器

@Controller
public class AssignmentController {

    @GetMapping("home")
    public String home() {
        return "home";
    }

    @GetMapping("/")
    public String main() {
        return "index";
    }

    @GetMapping("login")
    public String myLogin() {
        return "login";
    }         
}


要清楚的是,有效的URL是http://localhost:8080/index. html/,应该手动输入,但当我使用http://localhost:8080/home, http://localhost:8080/索引或http://localhost:8080/时,我得到错误页面
而我的最后一次尝试是使用webmvcconfigurer

@Override
        public void configureViewResolvers(ViewResolverRegistry registry) {
            InternalResourceViewResolver resourceViewResolver = new InternalResourceViewResolver();
            resourceViewResolver.setPrefix("/resources/**/");
            resourceViewResolver.setSuffix(".html");
            registry.viewResolver(resourceViewResolver);
        }


这是日志

DEBUG 12920 --- [nio-8080-exec-9] o.s.w.servlet.view.InternalResourceView  : View name 'login', model {}
 DEBUG 12920 --- [nio-8080-exec-9] o.s.w.servlet.view.InternalResourceView  : Forwarding to [/resources/**/login.html]
 DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/resources/**/login.html", parameters={}
 DEBUG 12920 --- [nio-8080-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
 DEBUG 12920 --- [nio-8080-exec-9] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
 DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Exiting from "FORWARD" dispatch, status 404
 DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
 DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
 DEBUG 12920 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
 DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404


我用的是spring Boot 3.1.5,我的pom

<?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>3.1.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.sunbasedata.qa2</groupId>
    <artifactId>sunbase</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sunbase</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>21</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.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/foodbox
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.sql.init.mode=NEVER
spring.jpa.hibernate.ddl-auto=update

对于像这样的基本应用程序,当您使用@Controller并将html文件放置在resources/static/文件夹中时,问题可能会重现

vmjh9lq9

vmjh9lq91#

我认为你在你的项目中有一个错误的配置。服务静态文件是最简单的事情。
我尝试了你的github示例(禁用数据库,因为没有身体有你的数据库)和HTML文件可用


的数据
我给你们看一个百里香叶的样本

百里香叶

我用thymeleaf开始了一个旧的模板
https://github.com/jrichardsz/spring-boot-templates/tree/master/013-thymeleaf-sqlite-crud



我在静态文件夹中添加了一个foo.txt(../src/main/resources/static/foo.txt



重新启动后,文件准备就绪:


相关问题