Spring Boot:无法访问localhost(404)上的页面html

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

我正在尝试在Spring Boot网站上改编Controller示例。不幸的是我有错误404,当我尝试访问localhost:8080 / NewFile URL

我的EcomercController.java:

package com.techprimers.jpa.springjpahibernateexample.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class EcomercController {
    @RequestMapping(value = "/NewFile", method = RequestMethod.GET)
    public String NewFile(){
         return "NewFile";
    }
}

而且我也在porm.xml中添加了此依赖项

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

和我的路径包

com.exemple

----- com.exemple.web.EcomercController.java

com.exemple.SpringJpaHibernateExampleApplication

和我的文件NewFile.html在

resources-> static-> NewFile.html

java spring spring-boot thymeleaf http-error
4个回答
0
投票

您的html文件应位于templates文件夹中

resources->templates->NewFile.html

阅读Spring Boot Serving static content 以了解资源映射。


0
投票

将您的HTML文件移动到资源->模板文件夹中


0
投票

将您的html文件放置在以下任意一个中-

src/main/resources/resources/index.html
src/main/resources/static/index.html 
src/main/resources/public/index.html

而且因为您选择了此-

src/main/resources/resources/index.html

然后放在模板内。


0
投票

请确保在applicaton配置(在resources文件夹下)中没有上下文映射。我认为默认值为“ / api”,在这种情况下,您需要以以下方式访问URL:

localhost:8080 / api / NewFile

推荐:不要大写URI。请改用新文件或新文件。

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