利用idea和maven作為開發環境,通過springboot+mysql+Jpa完成主要後端開發後,現在往工程里引入Redis資料庫緩存和前端freemarker時,發現工程不識別前端freemark的ftl文件。 經過半夜的奮戰把問題解決了。現在把錯誤消息,發生問題時的情景,重試的手段以及最終問 ...
利用idea和maven作為開發環境,通過springboot+mysql+Jpa完成主要後端開發後,現在往工程里引入Redis資料庫緩存和前端freemarker時,發現工程不識別前端freemark的ftl文件。
經過半夜的奮戰把問題解決了。現在把錯誤消息,發生問題時的情景,重試的手段以及最終問題解決的方法在這裡分享一下。
畫面顯示的異常信息:
*************************************
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Oct 26 13:42:36 GMT+08:00 2017
There was an unexpected error (type=Not Found, status=404).
No message available
**************************************
controller:
==================
package com.michael.fm1.controller;
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;
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