簡體   English   中英

Spring Boot:無法訪問本地主機上的頁面 html (404)

[英]Spring Boot: Cannot access page html on localhost (404)

我正在嘗試調整 Spring Boot 網站上的控制器示例。 不幸的是,當我嘗試訪問 localhost:8080/NewFile URL 時出現錯誤 404

我的 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.web.EcomercController.java

com.example.SpringJpaHibernateExampleApplication

和我的文件 NewFile.html

資源->靜態->NewFile.html

將您的 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

然后放在模板里面。

您的 html 文件應該在templates文件夾中

resources->templates->NewFile.html

閱讀Spring Boot Serving 靜態內容以了解資源映射。

將您的 Html 文件移動到 resources->templates 文件夾

確保在應用程序配置(在資源文件夾下)中沒有上下文映射。 我認為默認值是“/api”,在這種情況下,您需要訪問 URL:

本地主機:8080/api/NewFile

建議:不要大寫您的 URI。 改用 new-file 或 newfile 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM