![](/img/trans.png)
[英]SpringBoot+Thymeleaf: Property or field 'name' cannot be found on object of type
[英]SpringBoot with Thymeleaf - css not found
首先要说的是,我一直在寻找解决方案有一段时间了,现在我非常绝望。
当 Spring Boot 运行时,我无法从 html 页面访问 css 文件。
html.文件
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head lang="en">
<title th:text='#{Title}'>AntiIntruder</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" media="all" href="../assets/css/style.css" th:href="@{/css/style.css}" />
</head>
<body>
...
应用.java
@SpringBootApplication // adds @Configuration, @EnableAutoConfiguration, @ComponentScan
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/*");
}
}
文件夹结构:
我试过将css
文件夹放入static
文件夹和/或删除 addResourcesHandlers,通过相对路径和其他一些东西引用 css。 似乎没有什么可以解决这个问题。 如果您尝试解决此问题但没有找到解决方案,请也让我知道,以便我知道,我没有被忽略。
1.使用自定义资源路径
在您的Web配置中
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/assets/**")) {
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
}
}
将style.css
文件放在此文件夹中
src/main/resources/assets/css/
在你的意见之后
<link rel="stylesheet" type="text/css" th:href="@{/assets/css/style.css}" />
。
2.在弹簧引导中使用预定义路径
从Web配置中删除addResourceHandlers
将style.css
放在以下任何文件夹中
src/main/resources/META-INF/resources/assets/css
src/main/resources/resources/assets/css/
src/main/resources/static/assets/css/
src/main/resources/public/assets/css/
并在视图中
<link rel="stylesheet" type="text/css" th:href="@{/assets/css/style.css}" />
。
注意:您可以在此处删除assets
文件夹。 如果要这样做,请将其从预定义的资源文件夹中删除,也可以从view th:href
删除。 但我保持原样,因为你明确提到了问题中的assets/
路径。 所以我相信你需要在资源URL中拥有assets/
。
问题是Application.java
文件中的@EnableWebMvc
注释。 一旦我删除了那个,css就开始在localhost:8080/css/style.css
上可用,但是没有应用。 到目前为止,我还没有找到@EnableWebMvc
导致问题的原因。
然后我删除了一个映射到我已实现的/**
的控制器,以显示自定义错误页面。
@RequestMapping("/**")
public String notFound() {
return "errors/404";
}
除去这个之后,我的css正在工作。 =)
如果将css放在静态文件夹中,则不需要addResourceHandlers方法。
.../static/css/app.css
或者,如果您真的想将它们放在assets文件夹中:
.addResourceLocations("classpath:/assets/") <-- without the * at the end
.../assets/css/app/css
在这两种情况下,css都应该可以通过
th:href="@{/css/app.css}"
我的建议是将(再次) css
文件夹放在static
文件夹下,删除addResourcesHandlers并使用绝对路径到达css(例如/css/style.css
)。
将您的css文件夹放在resources / static文件夹中
对我来说,我不得不删除样式表的静态引用,以便在百里香中工作。 所以
<link rel="stylesheet" type="text/css" media="all" href="../assets/css/style.css" th:href="@{/css/style.css}">
成为
<link rel="stylesheet" th:href="@{/css/bootstrap.css}">
我花了好几个小时尝试各种配置和文件路径重命名。 不知道为什么,但这是唯一让我的css和js加载到Spring Boot 5中的东西。
就我而言,问题出在文件读取权限上。 我从另一个项目复制了文件“style.css”,浏览器无法读取它。 重新创建“style.css”后,一切正常。
首先像这样使用 WebMvcConfigurer 在您的配置 class 之一上实现
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
//and overide this configuration method like this
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/webjars/**", "/resources/**");
}
}
所以现在你可以像这样覆盖你的 HttpSecurity 配置
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter{
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/index").anonymous()
.antMatchers("/**/*.*").permitAll()
.anyRequest().authenticated();
}
}
this ("/**/*.*") regex text will allow files with anny extension
如果您使用的是像 bootstrap 这样的 webjars 库,请将此配置添加到您的 WebSecurityConfiguration
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
如果这个主题在 2022 年仍然相关。我遇到过类似的问题,我是这样解决的:
这个链接在/templates/file.html
<link rel="stylesheet" type="text/css" media="all" ref="../static/css/styles.css">
我添加了 application.properties
spring.mvc.static-path-pattern=/static/**
我的 css 路径是
src/main/resources/static/css/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.