springboot默认配置无法访问ftl文件

在使用springboot默认配置的情况下,通过返回ModelAndView无法访问到相应的ftl文件。

这是基本yml配置文件:

spring:
  jpa:
    show-sql: true
  jackson:
    default-property-inclusion: non_null
server:
  context-path: /sell

这是Controller文件:

@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController     {
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "2") Integer size,
                             Map<String, Object> map) {
        
        map.put("currentPage", page);
        map.put("size", size);
        return new ModelAndView("order/list", map);
    }
}

这是工程结构:

clipboard.png

这个问题出在哪儿呀,如果请求的是ResponseBody,是完全没有问题的,只有在请求相关的静态页面的时候无法访问到。

阅读 13.5k
9 个回答

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

spring.freemarker.template-loader-path=classpath:/templates/

这是我的配置

clipboard.png
这是项目结构,实际上在我debug的时候是可以进入到controller中的,在返回view的时候报404错误

return new ModelAndView("order/list", map);

这是报错信息:

 Whitelabel Error Page
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    
    Fri Jan 12 00:11:49 CST 2018
    There was an unexpected error (type=Not Found, status=404).
    No message available

没有加视图模板freemarker的依赖
<dependency>

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

</dependency>

就是依赖错了,微信点餐那个项目

新手上路,请多包涵

spring.freemarker.template-loader-path=classpath:/templates

需要配置application.yml文件
spring:
freemarker:
request-context-attribute: req
suffix: .ftl
content-type: text/html
cache: false
template-loader-path: classpath:/templates
charset: UTF-8
check-template-location: true
expose-request-attributes: false
expose-session-attributes: false

问题已解决

新手上路,请多包涵

image
试试这样配置,如果不行把 .ftl 改为ftlh

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题