天天看點

Spring Boot遇到的某些問題Spring Boot遇到的某些問題

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);
    }
}