IDEA+Maven平臺,Springboot渲染freemarker失敗的緣由和解決方法(Whitelabel Error Page type=Not Found, status=404)

利用idea和maven做爲開發環境,經過springboot+mysql+Jpa完成主要後端開發後,如今往工程裏引入Redis數據庫緩存和前端freemarker時,發現工程不識別前端freemark的ftl文件。html

通過半夜的奮戰把問題解決了。如今把錯誤消息,發生問題時的情景,重試的手段以及最終問題解決的方法在這裏分享一下。前端

 

畫面顯示的異常信息:
*************************************
Whitelabel Error Pagejava

This application has no explicit mapping for /error, so you are seeing this as a fallback.mysql

Thu Oct 26 13:42:36 GMT+08:00 2017
There was an unexpected error (type=Not Found, status=404).
No message available
**************************************web

controller:
==================
package com.michael.fm1.controller;spring

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;sql

import java.util.Map;數據庫

@Controller
public class hello {後端

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public ModelAndView hello(Map<String,Object> map){
map.put("key","value");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("web");
modelAndView.addObject(map);
return modelAndView;
}
}
===================瀏覽器

springbootApplication:
===================
package com.michael.fm1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Fm1Application {

public static void main(String[] args) {
SpringApplication.run(Fm1Application.class, args);
}
}
===================

application.properties
===================
空文件
===================

pom.xml
===================
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

<!--<dependency>-->
<!--<groupId>org.freemarker</groupId>-->
<!--<artifactId>freemarker</artifactId>-->
<!--</dependency>-->

</dependencies>
===================

resources/templates下的web.ftl
===================
<h1>this is a tittle</h1>
===================

問題解決的過程:
基於上面的錯誤信息,經歷了各類嘗試:
嘗試1 新建項目,只留上面最小量的代碼。瀏覽器訪問controller,渲染ftl。驗證失敗。
嘗試2 嘗試引入thymeLeaf,捨棄freemarker。瀏覽器訪問controller,渲染html。驗證失敗。
嘗試3 嘗試經過瀏覽器直接訪問靜態資源,能夠訪問。
嘗試4 經過修改application.properties中的配置參數。驗證失敗。
嘗試5 經過繼承WebMvcConfigurerAdapter的手段,本身初始化InternalResourceViewResolver。驗證失敗。

成功解決問題:想了想,主機上最近兩個月作了各類實驗,.m2裏可能下載了各類各樣的lib,其中有一些衝突了啊或者下載失敗了啊,都會致使莫名其妙的問題發生。代碼實在看不出問題,debug和看日誌也沒有什麼異常。。。果斷把m2刪除掉。從新mvn引入各個jar包,問題解決。web瀏覽器輸入:http://localhost:8080/hello顯示:this is a tittle

相關文章
相關標籤/搜索