在春季启动项目中将CSS之类的静态文件放在哪里? [英] Where to put static files such as CSS in a spring-boot project?
问题描述
在我当前的spring-boot项目中,我的观点有这一行:
In my current spring-boot project, my views have this line:
<link href="signin.css" rel="stylesheet"/>
引用静态CSS文件.运行项目并访问引用此文件的视图之一时,会收到404 not found错误或403未经授权的错误,具体取决于我将文件放在项目中的位置.
to reference a static css file. When I run the project, and access one of the views which reference this file, I get a 404 not found error or a 403 unauthorized error, depending where I put the file inside the project.
到目前为止,我已经尝试过:
I try this so far:
src/main/resources/static/css (with this, I use css/signin.css instead of signin.css)
src/main/resources/templates/static/css (with this, I use css/signin.css instead of signin.css)
src/src/main/resources/templates/acesso (same folder of the html file)
在哪里存储这种类型的文件?
what the right place to store this type of files?
推荐答案
src/main/resources/static
下方的任何地方都是放置CSS,JavaScript和图像等静态内容的适当位置.静态目录从/
提供.例如,src/main/resources/static/signin.css
将由/signin.css
提供,而src/main/resources/static/css/signin.css
将由/css/signin.css
提供.
Anywhere beneath src/main/resources/static
is an appropriate place for static content such as CSS, JavaScript, and images. The static directory is served from /
. For example, src/main/resources/static/signin.css
will be served from /signin.css
whereas src/main/resources/static/css/signin.css
will be served from /css/signin.css
.
src/main/resources/templates
文件夹用于视图模板,这些模板将通过模板引擎(如Thymeleaf,Freemarker或Velocity等)转换为HTML.您不应在此目录中放置静态内容.
The src/main/resources/templates
folder is intended for view templates that will be turned into HTML by a templating engine such as Thymeleaf, Freemarker, or Velocity, etc. You shouldn't place static content in this directory.
还要确保您没有在应用程序中使用@EnableWebMvc
,因为这将禁用Spring Boot对Spring MVC的自动配置.
Also make sure you haven't used @EnableWebMvc
in your application as that will disable Spring Boot's auto-configuration of Spring MVC.
这篇关于在春季启动项目中将CSS之类的静态文件放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!