在spring启动项目中,将CSS等静态文件放在何处?

在spring启动项目中,将CSS等静态文件放在何处?,spring,spring-boot,Spring,Spring Boot,在我当前的spring boot项目中,我的视图有以下几行: <link href="signin.css" rel="stylesheet"/> <link rel="stylesheet" href="/css/signin.css" /> 存储此类文件的正确位置是什么?src/main/resources/static下面的任何位置都适合放置CSS、JavaScript和图像等静态内容。静态目录由/提供。例如,src/main/resources/static/

在我当前的spring boot项目中,我的视图有以下几行:

<link href="signin.css" rel="stylesheet"/>
<link rel="stylesheet" href="/css/signin.css" />

存储此类文件的正确位置是什么?

src/main/resources/static下面的任何位置都适合放置CSS、JavaScript和图像等静态内容。静态目录由
/
提供。例如,
src/main/resources/static/signin.css
将从
/signin.css
提供服务,而
src/main/resources/static/css/signin.css
将从
/css/signin.css
提供服务

src/main/resources/templates
文件夹用于查看模板,这些模板将由模板引擎(如Thymeleaf、Freemarker或Velocity等)转换为HTML。您不应在此目录中放置静态内容


还要确保您没有在应用程序中使用
@EnableWebMvc
,因为这将禁用Spring Boot对Spring MVC的自动配置。

我发现,如果我将signin.css放在src/main/resources/META-INF/resources/static/css/signin.css下,那么我可以使用/css/signin.css访问它


不知道为什么。

在我的例子中,引用css和js文件夹中的文件如下:

<script src="js/bootstrap.min.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/carousel.css" rel="stylesheet">
@Configuration
@EnableWebSecurity
public class MainSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home", "/js/**", "/css/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }
}
我的意思是,如果我把css和js文件夹放在resources文件夹中(在main下),它会是:

<link href="../resources/css/carousel.css" rel="stylesheet">
<link href="../resources/css/bootstrap.min.css" rel="stylesheet">
<script src="../resources/js/bootstrap.min.js"></script>


如果我将它们放在静态文件夹中(在参考资料下),它将不起作用。

禁用
@EnableWebMvc
帮助我解决了这个问题。

您可以使用Thymeleaf

范例

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/css/signin.css}"/>
</head>
<body>
</body>
</html>
<link rel="stylesheet" href="../static/css/w3.css" th:href="@{/css/w3.css}">

如果该键设置为false,spring boot应用程序将不会加载任何资源。

除了将其放置在
src/main/resources/static
下的任何位置,并且不使用
@EnableWebMvc
,您需要授权访问您的
js
css
文件夹,特别是如果您的类路径中有
spring boot security
。您将添加如下内容:

<script src="js/bootstrap.min.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/carousel.css" rel="stylesheet">
@Configuration
@EnableWebSecurity
public class MainSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home", "/js/**", "/css/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }
}
然后在HTML中:

<link rel="stylesheet"  href="/css/bootstrap.min.css">
<script src="/js/bootstrap.min.js"></script>

我使用Spring Boot 2.0.2

我仍然在我的
webConfig.java
中保留
@EnableWebMvc
注释,并重写
addResourceHandlers()
方法,以帮助浏览器通过http请求访问css和javascript文件

这是webapp目录的结构

webConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "example.com")
public class WebConfig implements WebMvcConfigurer {

    // =======================================
    // =             Bean Config             =
    // =======================================

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");

        return resolver;
    }


    // =======================================
    // =          Override Methods           =
    // =======================================

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pdfs/**")
                .addResourceLocations("/WEB-INF/pdfs/");

        registry.addResourceHandler("/css/**")
                .addResourceLocations("/WEB-INF/css/");

        registry.addResourceHandler("/js/**")
                .addResourceLocations("/WEB-INF/js/");
    }
}
index.jsp头标记:

<head>
    <title>Title</title>

    <!-- Custom styles for this template -->
    <link href="css/cover.css" rel="stylesheet">
</head>

标题
希望对您有所帮助。

当我使用:

src/main/resources/static/css.
我的观点是这样的:

<link href="signin.css" rel="stylesheet"/>
<link rel="stylesheet" href="/css/signin.css" />

我使用带Thymeleaf的弹簧靴

这是目录的结构,类似于@Response

范例

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/css/signin.css}"/>
</head>
<body>
</body>
</html>
<link rel="stylesheet" href="../static/css/w3.css" th:href="@{/css/w3.css}">

  • src/resource/static/css
  • src/resource/static/js
  • src/resource/static/images

如果使用spring boot,请不要使用@EnableWebMvc,并检查spring安全性。

将所需文件放入spring boot的静态文件夹中。然后您可以直接访问jsp中所需的文件


希望它能对我有所帮助。

我运气不好,这里发布的解决方案都不适合我:/spring boot是否需要进行任何配置才能使用src/main/resources/static作为默认位置?除非我从带有完整url的CDN加载css,否则它不会获取我的css。不,这是默认位置。只需确保您没有在应用程序中使用
@EnableWebMvc
,因为这将禁用Spring Boot对Spring MVC的自动配置。嗨@AndyWilkinson,当我必须在我的Spring Boot应用程序中使用EnableWebMvc时,我可以知道如何处理这个问题吗?为什么你必须使用
@EnableWebMvc
,而不是
WebMVCConfigureAdapter
?我真的觉得这样很有效吗?我不能让它与bootstrap.min.css或bootstrap.min.js这样的名称一起工作,而与basename中没有点的名称(如somestyle.css或somescript.js)一起工作则非常完美。看,现在可能没用了。这个问题是在2014年提出的。试着回答最近的问题。将帮助你和我……@Ashish451如果不是这样的话,很抱歉,但是如果答案是好的并且有贡献,那么回答老问题并没有错:)。不工作。尝试使用这个
registry.addResourceHandler(“/WEB-INF/**”).addResourceLocations(“classpath:/WEB-INF/”)这不能按原样工作。你必须把它放在这个文件夹里/src/main/resources/static/css/signing.css最佳答案这对我来说很有效。但是从安全角度来看,这不是不安全吗?@swapnilligle如果你想确保某些资源的安全,请修改你的antMatchers。好的,谢谢!但是公开js和/或CSS资源是常见的/好的做法吗?简单地说,就像这个示例一样,如果忘记了,请删除thymleaf声明中的static关键字。此示例运行良好。