使用Spring Boot開發(fā)WEB頁面

前面一章我們用Spring Boot開發(fā)了一個(gè)RESTful,本章我們開發(fā)一個(gè)WEB界面。

目前Spring官方已經(jīng)不推薦使用JSP來開發(fā)WEB了,而是推薦使用如下幾種模板引擎來開發(fā):

  • Thymeleaf(Spring官方推薦)
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

目前業(yè)界使用最廣泛的還是FreeMaker和Velocity,我現(xiàn)在就以FreeMaker為例,介紹如何和Spring Boot集成。

添加FreeMaker依賴

首先在pom.xml中添加FreeMaker的依賴

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

編寫一個(gè)welcome.ftl

Spring Boot默認(rèn)存放模板的路徑在src/main/resources/templates,不過我們也可以自行修改,文章最后會(huì)有一個(gè)Sping MVC的配置文件示例。

<!DOCTYPE html>
<html>
<body>
Hello,${name}.歡迎閱讀《${bookTitle}》
</body>
</html>

編寫Controller來渲染模板

@Controller
public class HelloController {

    @Value("${userName}")
    private String userName;

    @Value("${bookTitle}")
    private String bookTitle;

    @RequestMapping("/")
    public String index(ModelMap map) {
        // 加入一個(gè)屬性,用來在模板中讀取
        map.addAttribute("name", userName);
        map.addAttribute("bookTitle", bookTitle);
        // return模板文件的名稱,對(duì)應(yīng)src/main/resources/templates/welcome.html
        return "welcome";
    }
}

上述代碼中的userName和bookTitle我們?cè)?code>application.properties中做了設(shè)置

userName=Alex
bookTitle=Spring Boot入門教程

最后啟動(dòng)Spring Boot,輸入http://localhost:8080/,頁面如下所示

FreeMaker示例

靜態(tài)資源加載

我們?cè)陂_發(fā)WEB頁面的時(shí)候,還需要加載很多靜態(tài)資源,比如js、圖片、css等文件,那這些文件應(yīng)該放在哪里呢?

我們?cè)趩?dòng)Spring Boot的時(shí)候,可以看到控制臺(tái)輸出如下一些信息

2016-09-20 15:18:55.445  INFO 16210 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-20 15:18:55.445  INFO 16210 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-20 15:18:55.474  INFO 16210 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

其中默認(rèn)配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources)
其中默認(rèn)配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/
PS:上面的 static、public、resources 等目錄都在 classpath: 下面(例如 src/main/resources/static)。

假如我們的文件是如下圖放置,當(dāng)我們輸入http://localhost:8080/dog.jpg的時(shí)候,顯示的是哪張圖片呢?大家可以自行驗(yàn)證一下,這邊先告訴大家Spring Boot的加載順序是
META-INF/resources > resources > static > public

靜態(tài)資源加載順序

小結(jié)

Spring Boot為我們WEB開發(fā)已經(jīng)準(zhǔn)備好了很多默認(rèn)的配置,一般來說我們已經(jīng)夠用了,如有需要可自行修改,部分常用配置如下,更多常用的配置可參見SpringBoot常用配置

mvc

  • spring.mvc.async.request-timeout
    設(shè)定async請(qǐng)求的超時(shí)時(shí)間,以毫秒為單位,如果沒有設(shè)置的話,以具體實(shí)現(xiàn)的超時(shí)時(shí)間為準(zhǔn),比如tomcat的servlet3的話是10秒.
  • spring.mvc.date-format
    設(shè)定日期的格式,比如dd/MM/yyyy.
  • spring.mvc.favicon.enabled
    是否支持favicon.ico,默認(rèn)為: true
  • spring.mvc.ignore-default-model-on-redirect
    在重定向時(shí)是否忽略默認(rèn)model的內(nèi)容,默認(rèn)為true
  • spring.mvc.locale
    指定使用的Locale.
  • spring.mvc.message-codes-resolver-format
    指定message codes的格式化策略(PREFIX_ERROR_CODE,POSTFIX_ERROR_CODE).

view

  • spring.view.prefix
    設(shè)定mvc視圖的前綴.
  • spring.view.suffix
    設(shè)定mvc視圖的后綴.

resource

  • spring.resources.add-mappings
    是否開啟默認(rèn)的資源處理,默認(rèn)為true
  • spring.resources.cache-period
    設(shè)定資源的緩存時(shí)效,以秒為單位.
  • spring.resources.chain.cache
    是否開啟緩存,默認(rèn)為: true
  • spring.resources.chain.enabled
    是否開啟資源 handling chain,默認(rèn)為false
  • spring.resources.chain.html-application-cache
    是否開啟h5應(yīng)用的cache manifest重寫,默認(rèn)為: false
  • spring.resources.chain.strategy.content.enabled
    是否開啟內(nèi)容版本策略,默認(rèn)為false
  • spring.resources.chain.strategy.content.paths
    指定要應(yīng)用的版本的路徑,多個(gè)以逗號(hào)分隔,默認(rèn)為:[/**]
  • spring.resources.chain.strategy.fixed.enabled
    是否開啟固定的版本策略,默認(rèn)為false
  • spring.resources.chain.strategy.fixed.paths
    指定要應(yīng)用版本策略的路徑,多個(gè)以逗號(hào)分隔
  • spring.resources.chain.strategy.fixed.version
    指定版本策略使用的版本號(hào)
  • spring.resources.static-locations
    指定靜態(tài)資源路徑,默認(rèn)為classpath:[/META-INF/resources/,/resources/, /static/, /public/]以及context:/

freemarker

  • spring.freemarker.allow-request-override
    指定HttpServletRequest的屬性是否可以覆蓋controller的model的同名項(xiàng)
  • spring.freemarker.allow-session-override
    指定HttpSession的屬性是否可以覆蓋controller的model的同名項(xiàng)
  • spring.freemarker.cache
    是否開啟template caching.
  • spring.freemarker.charset
    設(shè)定Template的編碼.
  • spring.freemarker.check-template-location
    是否檢查templates路徑是否存在.
  • spring.freemarker.content-type
    設(shè)定Content-Type.
  • spring.freemarker.enabled
    是否允許mvc使用freemarker.
  • spring.freemarker.expose-request-attributes
    設(shè)定所有request的屬性在merge到模板的時(shí)候,是否要都添加到model中.
  • spring.freemarker.expose-session-attributes
    設(shè)定所有HttpSession的屬性在merge到模板的時(shí)候,是否要都添加到model中.
  • spring.freemarker.expose-spring-macro-helpers
    設(shè)定是否以springMacroRequestContext的形式暴露RequestContext給Spring’s macro library使用
  • spring.freemarker.prefer-file-system-access
    是否優(yōu)先從文件系統(tǒng)加載template,以支持熱加載,默認(rèn)為true
  • spring.freemarker.prefix
    設(shè)定freemarker模板的前綴.
  • spring.freemarker.request-context-attribute
    指定RequestContext屬性的名.
  • spring.freemarker.settings
    設(shè)定FreeMarker keys.
  • spring.freemarker.suffix
    設(shè)定模板的后綴.
  • spring.freemarker.template-loader-path
    設(shè)定模板的加載路徑,多個(gè)以逗號(hào)分隔,默認(rèn): ["classpath:/templates/"]
  • spring.freemarker.view-names
    指定使用模板的視圖列表.

velocity

  • spring.velocity.allow-request-override
    指定HttpServletRequest的屬性是否可以覆蓋controller的model的同名項(xiàng)
  • spring.velocity.allow-session-override
    指定HttpSession的屬性是否可以覆蓋controller的model的同名項(xiàng)
  • spring.velocity.cache
    是否開啟模板緩存
  • spring.velocity.charset
    設(shè)定模板編碼
  • spring.velocity.check-template-location
    是否檢查模板路徑是否存在.
  • spring.velocity.content-type
    設(shè)定ContentType的值
  • spring.velocity.date-tool-attribute
    設(shè)定暴露給velocity上下文使用的DateTool的名
  • spring.velocity.enabled
    設(shè)定是否允許mvc使用velocity
  • spring.velocity.expose-request-attributes
    是否在merge模板的時(shí)候,將request屬性都添加到model中
  • spring.velocity.expose-session-attributes
    是否在merge模板的時(shí)候,將HttpSession屬性都添加到model中
  • spring.velocity.expose-spring-macro-helpers
    設(shè)定是否以springMacroRequestContext的名來暴露RequestContext給Spring’s macro類庫使用
  • spring.velocity.number-tool-attribute
    設(shè)定暴露給velocity上下文的NumberTool的名
  • spring.velocity.prefer-file-system-access
    是否優(yōu)先從文件系統(tǒng)加載模板以支持熱加載,默認(rèn)為true
  • spring.velocity.prefix
    設(shè)定velocity模板的前綴.
  • spring.velocity.properties
    設(shè)置velocity的額外屬性.
  • spring.velocity.request-context-attribute
    設(shè)定RequestContext attribute的名.
  • spring.velocity.resource-loader-path
    設(shè)定模板路徑,默認(rèn)為: classpath:/templates/
  • spring.velocity.suffix
    設(shè)定velocity模板的后綴.
  • spring.velocity.toolbox-config-location
    設(shè)定Velocity Toolbox配置文件的路徑,比如 /WEB-INF/toolbox.xml.
  • spring.velocity.view-names
    設(shè)定需要解析的視圖名稱.

thymeleaf

  • spring.thymeleaf.cache
    是否開啟模板緩存,默認(rèn)true
  • spring.thymeleaf.check-template-location
    是否檢查模板路徑是否存在,默認(rèn)true
  • spring.thymeleaf.content-type
    指定Content-Type,默認(rèn)為: text/html
  • spring.thymeleaf.enabled
    是否允許MVC使用Thymeleaf,默認(rèn)為: true
  • spring.thymeleaf.encoding
    指定模板的編碼,默認(rèn)為: UTF-8
  • spring.thymeleaf.excluded-view-names
    指定不使用模板的視圖名稱,多個(gè)以逗號(hào)分隔.
  • spring.thymeleaf.mode
    指定模板的模式,具體查看StandardTemplateModeHandlers,默認(rèn)為: HTML5
  • spring.thymeleaf.prefix
    指定模板的前綴,默認(rèn)為:classpath:/templates/
  • spring.thymeleaf.suffix
    指定模板的后綴,默認(rèn)為:.html
  • spring.thymeleaf.template-resolver-order
    指定模板的解析順序,默認(rèn)為第一個(gè).
  • spring.thymeleaf.view-names
    指定使用模板的視圖名,多個(gè)以逗號(hào)分隔.

groovy模板

  • spring.groovy.template.allow-request-override
    指定HttpServletRequest的屬性是否可以覆蓋controller的model的同名項(xiàng)
  • spring.groovy.template.allow-session-override
    指定HttpSession的屬性是否可以覆蓋controller的model的同名項(xiàng)
  • spring.groovy.template.cache
    是否開啟模板緩存.
  • spring.groovy.template.charset
    指定Template編碼.
  • spring.groovy.template.check-template-location
    是否檢查模板的路徑是否存在.
  • spring.groovy.template.configuration.auto-escape
    是否在渲染模板時(shí)自動(dòng)排查model的變量,默認(rèn)為: false
  • spring.groovy.template.configuration.auto-indent
    是否在渲染模板時(shí)自動(dòng)縮進(jìn),默認(rèn)為false
  • spring.groovy.template.configuration.auto-indent-string
    如果自動(dòng)縮進(jìn)啟用的話,是使用SPACES還是TAB,默認(rèn)為: SPACES
  • spring.groovy.template.configuration.auto-new-line
    渲染模板時(shí)是否要輸出換行,默認(rèn)為false
  • spring.groovy.template.configuration.base-template-class
    指定template base class.
  • spring.groovy.template.configuration.cache-templates
    是否要緩存模板,默認(rèn)為true
  • spring.groovy.template.configuration.declaration-encoding
    在寫入declaration header時(shí)使用的編碼
  • spring.groovy.template.configuration.expand-empty-elements
    是使用<br/>這種形式,還是<br></br>這種展開模式,默認(rèn)為: false)
  • spring.groovy.template.configuration.locale
    指定template locale.
  • spring.groovy.template.configuration.new-line-string
    當(dāng)啟用自動(dòng)換行時(shí),換行的輸出,默認(rèn)為系統(tǒng)的line.separator屬性的值
  • spring.groovy.template.configuration.resource-loader-path
    指定groovy的模板路徑,默認(rèn)為classpath:/templates/
  • spring.groovy.template.configuration.use-double-quotes
    指定屬性要使用雙引號(hào)還是單引號(hào),默認(rèn)為false
  • spring.groovy.template.content-type
    指定Content-Type.
  • spring.groovy.template.enabled
    是否開啟groovy模板的支持.
  • spring.groovy.template.expose-request-attributes
    設(shè)定所有request的屬性在merge到模板的時(shí)候,是否要都添加到model中.
  • spring.groovy.template.expose-session-attributes
    設(shè)定所有request的屬性在merge到模板的時(shí)候,是否要都添加到model中.
  • spring.groovy.template.expose-spring-macro-helpers
    設(shè)定是否以springMacroRequestContext的形式暴露RequestContext給Spring’s macro library使用
  • spring.groovy.template.prefix
    指定模板的前綴.
  • spring.groovy.template.request-context-attribute
    指定RequestContext屬性的名.
  • spring.groovy.template.resource-loader-path
    指定模板的路徑,默認(rèn)為: classpath:/templates/
  • spring.groovy.template.suffix
    指定模板的后綴
  • spring.groovy.template.view-names
    指定要使用模板的視圖名稱.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,197評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,415評(píng)論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,104評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,884評(píng)論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,647評(píng)論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,130評(píng)論 1 323
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,208評(píng)論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,366評(píng)論 0 288
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,887評(píng)論 1 334
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,737評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,939評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,478評(píng)論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,174評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,586評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,827評(píng)論 1 283
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,608評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,914評(píng)論 2 372

推薦閱讀更多精彩內(nèi)容