zoukankan      html  css  js  c++  java
  • 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;  
       }
     }
    

      

      

  • 相关阅读:
    jsp第三次作业
    软件测试第一次
    jsp第二次作业
    JSP第七次作业
    JSP第六次作业
    JSP第五次作业
    软件测试第二次作业
    JSP第四次作业(二)
    JSP第四次作业(一)
    JSP第三次作业
  • 原文地址:https://www.cnblogs.com/wishsaber/p/12016382.html
Copyright © 2011-2022 走看看