SpringBoot整合freemarker无需编译即可加载,解决404问题

ps:这种404问题着实烦,还是那句话杀手锏就是看配置文件是否正确和编译目录文件是否存在、正确

更新 1、解决每次修改freemarker文件需要重启项目,将freemarker配置template-loader-path改为使用windows绝对路径加载

 template-loader-path: file:C:/Users/user/IdeaProjects/demo/src/main/resources/templates

1、导入依赖

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

2、加配置

spring:
  mvc:
    static-path-pattern: /static/**
  freemarker:
    expose-request-attributes: true
    expose-session-attributes: true
    request-context-attribute: request
    cache: false
    charset: UTF-8
    check-template-location: true
    content-type: text/html
    template-loader-path: classpath:/templates
    suffix: .ftl

3、编写controller测试

@Controller
public class PageController {
    @Autowired
    private FreeMarkerConfigurer configurer;
    @RequestMapping("/userPage")
    public String userPage(){
        return "user";
    }
}

根据以上代码和配置这个path /userPage 会返回 resources/templates/user.ftl
在这里插入图片描述

4、出现的相关问题

1、配置错误导致404
在这里插入图片描述
重点是我圈起来的,这个配置有个类似的在mvc下有spring.mvc.view.suffix= .ftl ,使用该配置访问404, 必须按照上图配置写
2、访问静态资源(js、css)404
有的说是因为 mvc配置问题,有的说spring2.x后拦截静态资源,需要注入相关configure配置,我试过都没用。最后是重启项目就好了,猜测应该是改了配置项目没编译过来,我最后的配置就是上图的配置。

如果实在是404没办法,还有杀手锏,去编译目录找,看对应路径文件是否存在,不行删了重新编译
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值