【问题标题】:Spring Boot Thymeleaf static content doesn´t loadSpring Boot Thymeleaf 静态内容不加载
【发布时间】:2017-04-05 05:30:00
【问题描述】:

我刚刚开始了一个新的 Spring Boot(使用 Spring Security)和 Thymeleaf 项目。由于我想集成静态资源,我尝试了不同的文件结构,但没有一个,Spring 将文件绑定到其中。 由于我没有更改静态资源路径,我的控制台在启动时输出Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

我尝试了不同的文件结构但没有成功。

src
 |--main
    |--java
    |--resources
       |--static
          |--css
             |--bootstrap.min.css
             |--custom_test.css
          |--templates
             |--....

还有

src
 |--main
    |--java
    |--resources
       |--webjars          
          |--static
             |--css
                |--bootstrap.min.css
                |--custom_test.css
       |--templates
          |--....

src
 |--main
    |--java
    |--resources
       |--public
          |--css
             |--bootstrap.min.css
             |--custom_test.css
          |--templates
             |--....

在 HTML/Thymeleaf 中我尝试过

    <link rel="stylesheet" type="text/css" href="webjars/static/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="webjars/static/css/custom_test.css"/>

    <link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.min.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/custom_test.css}"/>

到目前为止,这些都没有奏效。 我真的希望你能帮助我和其他有同样问题的人。 提前致谢!

【问题讨论】:

标签: java spring spring-mvc spring-boot thymeleaf


【解决方案1】:

你的问题可能是 Spring Security 的配置问题。首先,检查您的浏览器的开发者工具,看看您在客户端上没有得到的资源的响应是什么。响应状态码可能是 302 或 401,具体取决于您当前的配置。

如果是这种情况,您需要为您的项目添加一个额外的配置,如下所示:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{        
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/favicon.ico").permitAll()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/js/**").permitAll()
                .antMatchers("/static/**").permitAll()
                .antMatchers("/images/**").permitAll()
                .antMatchers("/blog/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll();
    }
// ...
}

【讨论】:

  • 这确实是一个 302 响应代码。非常感谢您的帮助先生!
【解决方案2】:

您可以尝试以下一些可能的修复:

  • 如果您使用 Eclipse 作为您的 IDE,并且您将静态资源添加到静态子文件夹中,您应该刷新 Eclipse 中的项目文件夹以加载新资源。
  • 另一个可能的原因可能是您的本地引导版本未正确加载,请尝试使用早期版本。
  • 对 css 和 js 文件使用在线版本的引导程序,以查看本地引导程序文件是否存在问题。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-19
    • 2017-03-25
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 2018-02-10
    • 2017-07-26
    • 2020-01-26
    相关资源
    最近更新 更多