zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    activeMQ
    读写xml
    PLSQL
    oracle语法
    cxf远程调用服务
    FastDFS在linux下的安装和整合nginx实现上传图片和url访问
    dubbo和zookeeper的应用
    solr和Lucene的配置方式和应用
    win10 下安装 MongoDB 数据库支持模块(python)
    nodeJs 对 Mysql 数据库的 curd
  • 原文地址:https://www.cnblogs.com/chongpf/p/7736781.html
Copyright © 2011-2022 走看看