一.初識SpringBoot(入門級02)

1.1 Freemarker 其他用法


@RequestMapping("/freemarkerIndex")
public String index(Map result) {

目前創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機、網(wǎng)站托管維護、企業(yè)網(wǎng)站設(shè)計、龍口網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

  result.put("name", "yushengjun");
  result.put("sex", "0");
  List listResult = new ArrayList();
  listResult.add("zhangsan");
  listResult.add("lisi");
  listResult.add("itmayiedu");
  result.put("listResult", listResult);
  return "index";

}

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>首頁</title>
</head>
<body>
${name}
<#if sex=="1">

<#elseif sex=="2">

<#else>
其他

  </#if>      
 <#list userlist as user>
   ${user}
 </#list>

</body>
</html>

1.2 Freemarker配置
新建application.properties文件

spring.freemarker.allow-request-override=false
    spring.freemarker.cache=true
    spring.freemarker.check-template-location=true
    spring.freemarker.charset=UTF-8
    spring.freemarker.content-type=text/html
    spring.freemarker.expose-request-attributes=false
    spring.freemarker.expose-session-attributes=false
    spring.freemarker.expose-spring-macro-helpers=false
    #spring.freemarker.prefix=
    #spring.freemarker.request-context-attribute=
    #spring.freemarker.settings.*=
    spring.freemarker.suffix=.ftl
    spring.freemarker.template-loader-path=classpath:/templates/
    #comma-separated list
    #spring.freemarker.view-names= # whitelist of view names that can be resolved

1.3 使用JSP渲染W(wǎng)eb視圖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot 核心組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>

1.4 在application.properties創(chuàng)建以下配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

1.5 后臺代碼@Controller
br/>@Controller

   @RequestMapping("/index")
   public String index() {
         return "index";
    }

}

注意:創(chuàng)建SpringBoot整合JSP,一定要為war類型,否則會找不到頁面.
不要把JSP頁面存放在resources// jsp 不能被訪問到

1.6 全局捕獲異常
@ExceptionHandler 表示攔截異常
? @ControllerAdvice 是 controller 的一個輔助類,最常用的就是作為全局異常處理的切面類
? @ControllerAdvice 可以指定掃描范圍
? @ControllerAdvice 約定了幾種可行的返回值,如果是直接返回 model 類的話,需要使用 @ResponseBody 進行 json 轉(zhuǎn)換
o 返回 String,表示跳到某個 view
o 返回 modelAndView
o 返回 model + @ResponseBody

@ControllerAdvice
public class GlobalExceptionHandler {

        @ExceptionHandler(RuntimeException.class)
        @ResponseBody
        public Map<String, Object> exceptionHandler() {
            //開發(fā)中,一般都會講錯誤日志記錄在日志中,全局捕獲異常使用AOP技術(shù),采用異常通知
            //如果每個方法都可能發(fā)生異常,每個方法上都加上try
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("errorCode", "500");
                    map.put("errorMsg", "全局捕獲系統(tǒng)錯誤!");
                    return map;
        }

}

文章名稱:一.初識SpringBoot(入門級02)
當(dāng)前網(wǎng)址:http://muchs.cn/article32/joggpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、企業(yè)建站定制開發(fā)、關(guān)鍵詞優(yōu)化、虛擬主機網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)