spring boot
Posted wishsaber
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot相关的知识,希望对你有一定的参考价值。
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; } }
以上是关于spring boot的主要内容,如果未能解决你的问题,请参考以下文章
一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式
一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式
一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式