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没办法,还有杀手锏,去编译目录找,看对应路径文件是否存在,不行删了重新编译