Spring MVC 找不到Spring引导HTML资源

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

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

  1. spring.mvc.view.prefix: /resources/**
  2. spring.web.resources.chain.strategy.content.enabled=true
  3. spring.web.resources.chain.strategy.content.paths=/**
  4. 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,但控制器仍然无法工作
这是我的控制器

  1. @Controller
  2. public class AssignmentController {
  3. @GetMapping("home")
  4. public String home() {
  5. return "home";
  6. }
  7. @GetMapping("/")
  8. public String main() {
  9. return "index";
  10. }
  11. @GetMapping("login")
  12. public String myLogin() {
  13. return "login";
  14. }
  15. }


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

  1. @Override
  2. public void configureViewResolvers(ViewResolverRegistry registry) {
  3. InternalResourceViewResolver resourceViewResolver = new InternalResourceViewResolver();
  4. resourceViewResolver.setPrefix("/resources/**/");
  5. resourceViewResolver.setSuffix(".html");
  6. registry.viewResolver(resourceViewResolver);
  7. }


这是日志

  1. DEBUG 12920 --- [nio-8080-exec-9] o.s.w.servlet.view.InternalResourceView : View name 'login', model {}
  2. DEBUG 12920 --- [nio-8080-exec-9] o.s.w.servlet.view.InternalResourceView : Forwarding to [/resources/**/login.html]
  3. DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/resources/**/login.html", parameters={}
  4. 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 [/]]
  5. DEBUG 12920 --- [nio-8080-exec-9] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
  6. DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404
  7. DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
  8. DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
  9. 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)
  10. DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404


我用的是spring Boot 3.1.5,我的pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.1.5</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.sunbasedata.qa2</groupId>
  12. <artifactId>sunbase</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>sunbase</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>21</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-jpa</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-devtools</artifactId>
  31. <scope>runtime</scope>
  32. <optional>true</optional>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.mysql</groupId>
  36. <artifactId>mysql-connector-j</artifactId>
  37. <scope>runtime</scope>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.projectlombok</groupId>
  41. <artifactId>lombok</artifactId>
  42. <optional>true</optional>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-test</artifactId>
  47. <scope>test</scope>
  48. </dependency>
  49. </dependencies>
  50. <build>
  51. <plugins>
  52. <plugin>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-maven-plugin</artifactId>
  55. <configuration>
  56. <image>
  57. <builder>paketobuildpacks/builder-jammy-base:latest</builder>
  58. </image>
  59. <excludes>
  60. <exclude>
  61. <groupId>org.projectlombok</groupId>
  62. <artifactId>lombok</artifactId>
  63. </exclude>
  64. </excludes>
  65. </configuration>
  66. </plugin>
  67. </plugins>
  68. </build>
  69. </project>
  1. spring.datasource.username=root
  2. spring.datasource.password=
  3. spring.datasource.url=jdbc:mysql://localhost:3306/foodbox
  4. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  5. spring.sql.init.mode=NEVER
  6. 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



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


展开查看全部

相关问题