freemarker:
freemarker所需要依赖:
<!-- 添加freemarker模版的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
application.properties中freemarker的配置:
## Freemarker 配置 spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.cache=false spring.freemarker.suffix=.ftl spring.freemarker.charset=UTF-8 spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.request-context-attribute=request
Controller中的配置:
所需要的页面:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 欢迎!${name} </body> </html>
springboot中运行时报错是如何跳转到错误页面:
package com.example.demo.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; @ControllerAdvice //所有Controller运行都会进过这个
public class ExceptionHandler { //如果报错执行这个方法 @org.springframework.web.bind.annotation.ExceptionHandler(RuntimeException.class) @ResponseBody public Map<String,Object> ExceptionHandler(){ Map<String,Object> map = new HashMap<>(); map.put("error:","500"); map.put("msg:","頁面報錯了,請稍後,在重試!!1"); return map; } }