gpt4 book ai didi

spring-boot - Spring Boot 不使用 Thymeleaf 提供静态内容

转载 作者:行者123 更新时间:2023-12-04 03:08:18 25 4
gpt4 key购买 nike

我有以下 thymeleaf html 页面:

<head th:fragment="header">

<meta charset="utf-8" />
<link rel="stylesheet" href="../../css/main.css" th:href="@{/css/main.css}" />
<title th:text="#{device.page.title}">Title</title>
</head>

<body>
<div>
<h1 th:text="#{device.table.caption}"></h1>
<hr class="fineline"/>
Select devices using the checkboxes, you can update the client version or add client commands.
<form action="#" th:action="@{/devices/modify}" th:object="${deviceCommand}" method="post">
<table border="0" cellpadding="0" cellspacing="0" class="touchTable">
<!--<thead> -->
<tr>
<td scope="col" th:text="#{device.check.label}">Select</td>
<td width="300" scope="col"><span th:text="#{device.id.label}"></span>&nbsp;(<span th:text="#{device.retailer.name.label}"></span>)</td>
<td scope="col" th:text="#{device.current.label}">Curr Version</td>
<td scope="col" th:text="#{device.next.label}">Next Version</td>
<td scope="col" th:text="#{device.commands.label}">Commands</td>
</tr>
<!--</thead>-->
<!--<tbody> -->
<tr th:each="d : ${devices}">
<td><input type="checkbox" th:field="*{deviceModificationIds}" th:value="${d.id}"/></td>
<td><span th:text="${d.id}"></span>&nbsp;(<span th:text="${d.retailerName}"></span>)</td>
<td th:text="${d.currentClientVersion}">Washington</td>
<td th:text="${d.nextClientVersion}">gwash</td>
<td th:text="${d.commands}">gwash</td>
</tr>
<tr>
<td colspan="2"></td>
<td><span th:text="#{device.change.version.label}"></span><br/><input type="text" th:field="*{newVersion}"/></td>
<td><span th:text="#{device.add.command.label}"></span><br/><input type="text" th:field="*{newCommand}"/></td>
<td><br/><button type="submit" th:text="#{device.modify.action.button}">Action</button></td>
</tr>
<!--</tbody> -->
</table>
</form>


</div>
</body>

问题出在 css 样式表上。基本上 spring 似乎无法找到它,尽管我将文件放在/resources/static/css/main.css 中

它返回错误(在日志中):
o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/css/main.css] in DispatcherServlet with name 'dispatcherServlet'

现在 doco 都说 Spring Boot 应该自动为/resources/static 中的东西提供服务

这是我的 WebConfig:
@Configuration
@ComponentScan("com.txxxxcorp.txxxxpoint.resource")
@EnableWebMvc
public class WebConfig {

@Bean
MultipartConfigElement multipartConfigElement() {
MultiPartConfigFactory factory = new MultiPartConfigFactory();
factory.setMaxFileSize("4096KB");
factory.setMaxRequestSize("4096KB");
return factory.createMultipartConfig();
}

@Bean
public ViewResolver viewResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setTemplateMode("XHTML");
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".html");

SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver);

ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(engine);

String[] excludedViews = new String[]{
"/resources/static/**"};
viewResolver.setExcludedViewNames(excludedViews);

return viewResolver;
}

@Bean
public EmbeddedServletContainerCustomizer servletContainerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer servletContainer) {
((TomcatEmbeddedServletContainerFactory) servletContainer).addConnectorCustomizers(
new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
AbstractHttp11Protocol httpProtocol = (AbstractHttp11Protocol) connector.getProtocolHandler();
httpProtocol.setCompression("on");
httpProtocol.setCompressionMinSize(256);
String mimeTypes = httpProtocol.getCompressableMimeTypes();
String mimeTypesWithJson = mimeTypes + "," + MediaType.APPLICATION_JSON_VALUE;
httpProtocol.setCompressableMimeTypes(mimeTypesWithJson);
}
}
);
}
};
}

@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages");
return source;
}

出于某种原因,编辑器不允许我插入 spring 安全配置而不提示它的格式不正确(IntelliJ、Maven 和 Spring Boot 不同意这一点,因为它可以编译和工作),请放心,我已经允许/css/要经过的 main.css 路径

有谁知道为什么我无法解析 css 文件?

最佳答案

您使用 @EnableWebMvc 的事实关闭了 Spring Boot 的 MVC 自动配置(以及静态资源处理)。

要启用静态资源处理,最好的解决方案是删除 @EnableWebMvc 并让 Spring Boot 做它最擅长的事情 - 自动配置。

更改后,您应该进行一些回归测试以确保没有其他问题

关于spring-boot - Spring Boot 不使用 Thymeleaf 提供静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25657318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com