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


利用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM