springboot開發web應用

我們知道,在web開發時,一般都會涉及到很多的靜態資源,如js、image、css文件等。

SpringBoot的默認的靜態文件目錄是:

  • /static
  • /public
  • /resources
  • /META-INF/resources

 

所以一般上我們只需要把靜態文件放入前面的四個任一一個即可。默認都放在static下,對應路徑即爲:src/main/resources/static。

而從官網文檔裏也可以獲悉,爲了實現動態的html,SpringBoot是通過模版引擎進行頁面結果渲染的,主要有以下幾種:

 

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

對於模版引擎而言,SpringBoot默認存放模版文件的路徑爲src/main/resources/templates,當然也可以通過配置文件進行修改的。因爲不同的模版引擎對應的配置屬性是不一樣,所以在具體講解模版引擎時,會提到的。

當然了,使用jsp也是可以的,但官方已經不建議使用JSP了,本文也會講解下SpringBoot下JSP的支持的,畢竟有很多老的項目還是使用JSP居多的。

知道了以上的一些默認配置和知識點後,就可以進行模版引擎的集成使用了。本章節主要講解下常用的FreeMarker、Thymeleaf及JSP三個的集成和使用,其他的基本用法都一樣,就是各模版引擎的語法的差異了。

整合 Freemarker

FreeMarker是一款模板引擎: 即一種基於模板和要改變的數據,並用來生成輸出文本(HTML網頁,電子郵件,配置文件,源代碼等)的通用工具。

 

添加POM依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

1.application.properties配置加入相關配置:

# 緩存配置 開發階段應該配置爲false 因爲經常會改
spring.freemarker.cache=false
# 模版後綴名 默認爲ftl
spring.freemarker.suffix=.ftl
# 文件編碼
spring.freemarker.charset=UTF-8
# 模版加載的目錄
spring.freemarker.template-loader-path=classpath:/templates/
# 配置
# locale    該選項指定該模板所用的國家/語言選項
# number_format    指定格式化輸出數字的格式:currency、
# boolean_format    指定兩個布爾值的語法格式,默認值是true,false
# date_format,time_format,datetime_format    定格式化輸出日期的格式
# time_zone    設置格式化輸出日期時所使用的時區
# 數字千分位標識
spring.freemarker.settings.number_format=,##0.00 

題外話:詳細的配置可參org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties類

2.編寫控制層

FreemarkerController.java:

//因爲是返回頁面 所以不能是@RestController
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
    
    //正常和springmvc設置返回參數是意義的用法了
    @GetMapping("/map")
    public String index(String name,ModelMap map) {
        map.addAttribute("name", name);
        map.addAttribute("from", "lqdev.cn");
        //模版名稱,實際的目錄爲:src/main/resources/templates/freemarker.html
        return "freemarker";
    }
    
    @GetMapping("/mv")
    public String index(String name,ModelAndView mv) {
        mv.addObject("name", name);
        mv.addObject("from", "lqdev.cn");
        //模版名稱,實際的目錄爲:src/main/resources/templates/freemarker.html
        return "freemarker";
    }
}

3.編寫模版文件

在resources目錄下創建templates目錄,然後新建freemarker.ftl:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>freemarker簡單示例</title>
</head>
<body>
<h1>Hello Freemarker</h1>
<!-- 這裏注意:默認變量都不能爲null的, 當參數爲null時,會發生異常的 -->
<!-- 這裏後面幾個"!"避免下,這樣就是空白了 -->
<h2>名稱:${name!},來自:${from}</h2>
</body>
</html>

4.啓動應用,訪問:http://127.0.0.1:8080/freemarker/mv或者 http://127.0.0.1:8080/freemarker/map就能查看頁面了。

關於一些Freemarker的語法這裏就不說明了,大家可到官網查看下:https://freemarker.apache.org/docs/index.html或者,中文參考(可能版本不是最新):http://freemarker.foofun.cn/toc.html

整合 Thymeleaf

Thymeleaf是一個XML/XHTML/HTML5模板引擎,可用於Web與非Web環境中的應用開發。Thymeleaf的主要目標在於提供一種可被瀏覽器正確顯示的、格式良好的模板創建方式,因此也可以用作靜態建模。你可以使用它創建經過驗證的XML與HTML模板。相對於編寫邏輯或代碼,開發者只需將標籤屬性添加到模板中即可。接下來,這些標籤屬性就會在DOM(文檔對象模型)上執行預先制定好的邏輯。

添加pom依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

1.application.properties配置加入相關配置:

# 啓用緩存:建議生產開啓
spring.thymeleaf.cache=false 
# 建議模版是否存在
spring.thymeleaf.check-template-location=true 
# 是否啓用
spring.thymeleaf.enabled=true 
# 模版編碼
spring.thymeleaf.encoding=UTF-8 
# 應該從解析中排除的視圖名稱列表(用逗號分隔)
spring.thymeleaf.excluded-view-names= 
# 模版模式
spring.thymeleaf.mode=HTML5 
# 模版存放路徑
spring.thymeleaf.prefix=classpath:/templates/ 
# 模版後綴
spring.thymeleaf.suffix=.html

2.編寫控制層

ThymeleafController.java:

@Controller 	//不是@RestController
@RequestMapping("/thymeleaf")
public class ThymeleafController {

    // 正常和springmvc設置返回參數是意義的用法了
    @GetMapping("/map")
    public String index(String name, ModelMap map) {
        map.addAttribute("name", name);
        map.addAttribute("from", "lqdev.cn");
        // 模版名稱,實際的目錄爲:src/main/resources/templates/thymeleaf.html
        return "thymeleaf";
    }

    @GetMapping("/mv")
    public ModelAndView index(String name) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("name", name);
        mv.addObject("from", "lqdev.cn");
        // 模版名稱,實際的目錄爲:src/main/resources/templates/thymeleaf.html
        mv.setViewName("thymeleaf");
        return mv;
    }
}

3.編寫模版文件

thymeleaf.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
    <meta charset="UTF-8" />
    <title>thymeleaf簡單示例</title>
</head>
<body>
<h1>Hello thymeleaf</h1>
<!-- 這裏注意:拼接時變量要單獨使用${param},其他的常量使用''包裹 -->
<h2 th:text="'名稱:'+${name}+',來自:'+${from}">默認值</h2>
</body>
</html>

4.啓動應用,訪問:http://127.0.0.1:8080/thymeleaf/mv?name=xmr 或者 http://127.0.0.1:8080/thymeleaf/map?name=xmr 就能查看頁面了。

JSP支持

雖然SpringBoot官方已經不建議使用jsp了。但在一些老的項目遷移時,jsp的支持是毋庸置疑的。所以還是需要兼容的。

添加pom依賴加入

<!-- spring boot 內置tomcat jsp支持 -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

注意:創建SpringBoot整合JSP,一定要爲war類型,否則會找不到頁面

1.application.properties配置加入相關配置:

#jsp 支持
spring.mvc.view.suffix=.jsp
spring.mvc.view.prefix=/WEB-INF/jsp/

2.編寫控制層

JspController.java

@Controller
@RequestMapping("/jsp")
public class JspController {
    
    //正常和springmvc設置返回參數是意義的用法了
        @GetMapping("/map")
        public String index(String name,ModelMap map) {
            map.addAttribute("name", name);
            map.addAttribute("from", "lqdev.cn");
            //模版名稱,實際的目錄爲:src/main/webapp/jsp/index.html
            return "index";
        }
        
        @GetMapping("/mv")
        public ModelAndView index(String name) {
            ModelAndView mv = new ModelAndView();
            mv.addObject("name", name);
            mv.addObject("from", "lqdev.cn");
            //模版名稱,實際的目錄爲:src/main/webapp/jsp/index.html
            mv.setViewName("index");
            return mv;
        }
}

3.在webapp/WEB-INF/jsp目錄下編寫jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charsetUTF-8">
<title>jsp示例</title>
</head>
<body>
<h1>Hello Jsp</h1>
<h2 >名稱:${name},來自:${from}</h2>
</body>
</html>

5.啓動應用,訪問:http://127.0.0.1:8080/jsp/mv?name=xmr 或者 http://127.0.0.1:8080/jsp/map?name=xmr 就能查看頁面了。

 

由於本人水平有限,寫作中多有不足,還請不吝賜教

 

參考:

https://my.oschina.net/xiedeshou/blog/1923553

https://www.cnblogs.com/moonlightL/p/7891806.html

http://blog.didispace.com/springbootweb/

http://www.ityouknow.com/springboot/2016/02/03/spring-boot-web.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章