【问题标题】:Spring Boot and Thymeleaf Resource folder IssueSpring Boot 和 Thymeleaf 资源文件夹问题
【发布时间】:2014-11-21 23:44:02
【问题描述】:

我正在尝试在我的 Spring Boot 项目中设置资源文件夹。但无法做到。请帮忙。我正在尝试整合百里香。

我可以得到 index.html 但我无法在 index.html 中包含资源 /css/mobile-angular-ui-desktop.min.css 文件。 它给了我 404 Page Not found 错误。

index.html

<!DOCTYPE html>

<html>
<head>
<title>Provision Admin</title>

<link rel="stylesheet" th:href="@{/css/mobile-angular-ui-base.min.css}"
    href="../static/css/mobile-angular-ui-base.min.css" />
<link rel="stylesheet"
    href="/css/mobile-angular-ui-desktop.min.css" />

<script type="text/javascript">
    var appContext = '/profilebatch';

</script>


</head>
<body>

</body>
</html>

我设置了以下安全性。

@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    protected static class ApplicationSecurity extends
            WebSecurityConfigurerAdapter {

        @Autowired
        private SecurityProperties security;

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable().authorizeRequests().antMatchers("/css/**")
                    .permitAll().antMatchers("**/favicon.ico").permitAll()
                    .antMatchers("/secure/**").fullyAuthenticated().and()
                    .formLogin().loginPage("/login").failureUrl("/login?error")
                    .permitAll().and().logout()
                    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                    .logoutSuccessUrl("/login");
        }

        @Override
        public void configure(AuthenticationManagerBuilder auth)
                throws Exception {
            auth.inMemoryAuthentication().withUser("user").password("user")
                    .roles("USER").and().withUser("admin").password("admin")
                    .roles("ADMIN");
        }

    }

【问题讨论】:

  • 实际呈现的 HTML 中的href 是什么样的?

标签: spring spring-boot thymeleaf


【解决方案1】:

最后一切正常。 我在我的配置中删除了@EnableWebMvc。

【讨论】:

    【解决方案2】:

    你没有桌面的 th:href,如果你可以访问 base,那么这是你的问题:

    th:href="@{/css/mobile-angular-ui-desktop.min.css}"
    

    【讨论】:

    • 请分享更多细节,日志中是否有任何错误?
    • 请确认基础版工作正常,而您唯一的问题是桌面版,请使用萤火虫或开发人员工具确定。如果基础的工作桌面也必须工作。
    • 好的,现在是逻辑,我认为您正在使用 @enablewebmvc 并扩展了 WebMvcConfigurerAdapter,因此您需要覆盖 addResourceHandlers
    【解决方案3】:

    静态资源默认放置在 /public 而不是 /src/main/resources/static

    【讨论】:

    • 我试过了,但还是有问题。我已将我的 Rest 服务的 ContentNegotiation 默认设置为 APPLICATION_JSON。我同时拥有 Rest 和 Web Thymeleaf。排除 ContentNegotiation 文件夹的任何想法
    • Spring Boot 也将在 /static 之外提供服务。
    • 你能解释一下吗?
    猜你喜欢
    • 2017-02-25
    • 2018-05-28
    • 2019-03-25
    • 2017-03-13
    • 2015-03-08
    • 2017-07-13
    • 2017-07-13
    • 1970-01-01
    • 2019-04-15
    相关资源
    最近更新 更多