Spring Boot遇到的某些问题
1.关于templates的html包格式问题:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<!--以下两项需要如果不配置,解析themleaft 会有问题-->
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version>
</properties>
Tomcat配置Context 标签以后Tomcat启动不了
因为项目要访问本地硬盘的文件所以要去Tomcat的server.xml里配置Context ,
<Context path="/image" docBase="F:\file\image" debug="0" reloadable="true"></Context>
结果弄了一个多小时发现是docBase的路径不存在。因为我们是上传文件才会建立文件夹,导致一直找不到问题出在哪,记录一下
2.关于thymeleaf引入js.Css等:
<head th:fragment="header">
<meta charset="UTF-8" />
<title th:text="#{head.title}"></title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="shortcut icon" th:href="@{/static/img/favicon.gif}" type="image/gif" />
<link rel="stylesheet" th:href="@{/resources/css/bootstrap.min.css}" />
<link rel="stylesheet" th:href="@{/resources/css/jquery.ui.all.css}" />
<link rel="stylesheet" th:href="@{/resources/css/jquery.ui.customer.css}" />
<script th:src="@{/resources/js/jquery-1.9.1.min.js}"></script>
<script th:src="@{/resource/js/bootstrap.min.js}"></script>
</head>
下面在你要的页面 引入这个片段就行<head th:include="theme/fragments::header" /> 他就相当于jsp 中我们常用的<%@ include file="/WEB-INF/jsp/public/header.jspf"%>
3.如果静态资源被拦截或者不生效,请尝试以下配置:
增加配置类:
package com.home.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
/**
* 配置静态访问资源
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
}