在前面一章我們用Spring Boot開發了一個RESTful,本章我們開發一個WEB界面。
目前Spring官方已經不推薦使用JSP來開發WEB了,而是推薦使用如下幾種模板引擎來開發:
- Thymeleaf(Spring官方推薦)
- FreeMarker
- Velocity
- Groovy
- Mustache
目前業界使用最廣泛的還是FreeMaker和Velocity,我現在就以FreeMaker為例,介紹如何和Spring Boot集成。
添加FreeMaker依賴
首先在pom.xml中添加FreeMaker的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
編寫一個welcome.ftl
Spring Boot默認存放模板的路徑在src/main/resources/templates
,不過我們也可以自行修改,文章最后會有一個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) {
// 加入一個屬性,用來在模板中讀取
map.addAttribute("name", userName);
map.addAttribute("bookTitle", bookTitle);
// return模板文件的名稱,對應src/main/resources/templates/welcome.html
return "welcome";
}
}
上述代碼中的userName和bookTitle我們在application.properties
中做了設置
userName=Alex
bookTitle=Spring Boot入門教程
最后啟動Spring Boot,輸入http://localhost:8080/,頁面如下所示
靜態資源加載
我們在開發WEB頁面的時候,還需要加載很多靜態資源,比如js、圖片、css等文件,那這些文件應該放在哪里呢?
我們在啟動Spring Boot的時候,可以看到控制臺輸出如下一些信息
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]
其中默認配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources)
其中默認配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/
PS:上面的 static、public、resources 等目錄都在 classpath: 下面(例如 src/main/resources/static)。
假如我們的文件是如下圖放置,當我們輸入http://localhost:8080/dog.jpg的時候,顯示的是哪張圖片呢?大家可以自行驗證一下,這邊先告訴大家Spring Boot的加載順序是
META-INF/resources > resources > static > public
小結
Spring Boot為我們WEB開發已經準備好了很多默認的配置,一般來說我們已經夠用了,如有需要可自行修改,部分常用配置如下,更多常用的配置可參見SpringBoot常用配置
mvc
- spring.mvc.async.request-timeout
設定async請求的超時時間,以毫秒為單位,如果沒有設置的話,以具體實現的超時時間為準,比如tomcat的servlet3的話是10秒. - spring.mvc.date-format
設定日期的格式,比如dd/MM/yyyy. - spring.mvc.favicon.enabled
是否支持favicon.ico,默認為: true - spring.mvc.ignore-default-model-on-redirect
在重定向時是否忽略默認model的內容,默認為true - spring.mvc.locale
指定使用的Locale. - spring.mvc.message-codes-resolver-format
指定message codes的格式化策略(PREFIX_ERROR_CODE,POSTFIX_ERROR_CODE).
view
- spring.view.prefix
設定mvc視圖的前綴. - spring.view.suffix
設定mvc視圖的后綴.
resource
- spring.resources.add-mappings
是否開啟默認的資源處理,默認為true - spring.resources.cache-period
設定資源的緩存時效,以秒為單位. - spring.resources.chain.cache
是否開啟緩存,默認為: true - spring.resources.chain.enabled
是否開啟資源 handling chain,默認為false - spring.resources.chain.html-application-cache
是否開啟h5應用的cache manifest重寫,默認為: false - spring.resources.chain.strategy.content.enabled
是否開啟內容版本策略,默認為false - spring.resources.chain.strategy.content.paths
指定要應用的版本的路徑,多個以逗號分隔,默認為:[/**] - spring.resources.chain.strategy.fixed.enabled
是否開啟固定的版本策略,默認為false - spring.resources.chain.strategy.fixed.paths
指定要應用版本策略的路徑,多個以逗號分隔 - spring.resources.chain.strategy.fixed.version
指定版本策略使用的版本號 - spring.resources.static-locations
指定靜態資源路徑,默認為classpath:[/META-INF/resources/,/resources/, /static/, /public/]以及context:/
freemarker
- spring.freemarker.allow-request-override
指定HttpServletRequest的屬性是否可以覆蓋controller的model的同名項 - spring.freemarker.allow-session-override
指定HttpSession的屬性是否可以覆蓋controller的model的同名項 - spring.freemarker.cache
是否開啟template caching. - spring.freemarker.charset
設定Template的編碼. - spring.freemarker.check-template-location
是否檢查templates路徑是否存在. - spring.freemarker.content-type
設定Content-Type. - spring.freemarker.enabled
是否允許mvc使用freemarker. - spring.freemarker.expose-request-attributes
設定所有request的屬性在merge到模板的時候,是否要都添加到model中. - spring.freemarker.expose-session-attributes
設定所有HttpSession的屬性在merge到模板的時候,是否要都添加到model中. - spring.freemarker.expose-spring-macro-helpers
設定是否以springMacroRequestContext的形式暴露RequestContext給Spring’s macro library使用 - spring.freemarker.prefer-file-system-access
是否優先從文件系統加載template,以支持熱加載,默認為true - spring.freemarker.prefix
設定freemarker模板的前綴. - spring.freemarker.request-context-attribute
指定RequestContext屬性的名. - spring.freemarker.settings
設定FreeMarker keys. - spring.freemarker.suffix
設定模板的后綴. - spring.freemarker.template-loader-path
設定模板的加載路徑,多個以逗號分隔,默認: ["classpath:/templates/"] - spring.freemarker.view-names
指定使用模板的視圖列表.
velocity
- spring.velocity.allow-request-override
指定HttpServletRequest的屬性是否可以覆蓋controller的model的同名項 - spring.velocity.allow-session-override
指定HttpSession的屬性是否可以覆蓋controller的model的同名項 - spring.velocity.cache
是否開啟模板緩存 - spring.velocity.charset
設定模板編碼 - spring.velocity.check-template-location
是否檢查模板路徑是否存在. - spring.velocity.content-type
設定ContentType的值 - spring.velocity.date-tool-attribute
設定暴露給velocity上下文使用的DateTool的名 - spring.velocity.enabled
設定是否允許mvc使用velocity - spring.velocity.expose-request-attributes
是否在merge模板的時候,將request屬性都添加到model中 - spring.velocity.expose-session-attributes
是否在merge模板的時候,將HttpSession屬性都添加到model中 - spring.velocity.expose-spring-macro-helpers
設定是否以springMacroRequestContext的名來暴露RequestContext給Spring’s macro類庫使用 - spring.velocity.number-tool-attribute
設定暴露給velocity上下文的NumberTool的名 - spring.velocity.prefer-file-system-access
是否優先從文件系統加載模板以支持熱加載,默認為true - spring.velocity.prefix
設定velocity模板的前綴. - spring.velocity.properties
設置velocity的額外屬性. - spring.velocity.request-context-attribute
設定RequestContext attribute的名. - spring.velocity.resource-loader-path
設定模板路徑,默認為: classpath:/templates/ - spring.velocity.suffix
設定velocity模板的后綴. - spring.velocity.toolbox-config-location
設定Velocity Toolbox配置文件的路徑,比如 /WEB-INF/toolbox.xml. - spring.velocity.view-names
設定需要解析的視圖名稱.
thymeleaf
- spring.thymeleaf.cache
是否開啟模板緩存,默認true - spring.thymeleaf.check-template-location
是否檢查模板路徑是否存在,默認true - spring.thymeleaf.content-type
指定Content-Type,默認為: text/html - spring.thymeleaf.enabled
是否允許MVC使用Thymeleaf,默認為: true - spring.thymeleaf.encoding
指定模板的編碼,默認為: UTF-8 - spring.thymeleaf.excluded-view-names
指定不使用模板的視圖名稱,多個以逗號分隔. - spring.thymeleaf.mode
指定模板的模式,具體查看StandardTemplateModeHandlers,默認為: HTML5 - spring.thymeleaf.prefix
指定模板的前綴,默認為:classpath:/templates/ - spring.thymeleaf.suffix
指定模板的后綴,默認為:.html - spring.thymeleaf.template-resolver-order
指定模板的解析順序,默認為第一個. - spring.thymeleaf.view-names
指定使用模板的視圖名,多個以逗號分隔.
groovy模板
- spring.groovy.template.allow-request-override
指定HttpServletRequest的屬性是否可以覆蓋controller的model的同名項 - spring.groovy.template.allow-session-override
指定HttpSession的屬性是否可以覆蓋controller的model的同名項 - spring.groovy.template.cache
是否開啟模板緩存. - spring.groovy.template.charset
指定Template編碼. - spring.groovy.template.check-template-location
是否檢查模板的路徑是否存在. - spring.groovy.template.configuration.auto-escape
是否在渲染模板時自動排查model的變量,默認為: false - spring.groovy.template.configuration.auto-indent
是否在渲染模板時自動縮進,默認為false - spring.groovy.template.configuration.auto-indent-string
如果自動縮進啟用的話,是使用SPACES還是TAB,默認為: SPACES - spring.groovy.template.configuration.auto-new-line
渲染模板時是否要輸出換行,默認為false - spring.groovy.template.configuration.base-template-class
指定template base class. - spring.groovy.template.configuration.cache-templates
是否要緩存模板,默認為true - spring.groovy.template.configuration.declaration-encoding
在寫入declaration header時使用的編碼 - spring.groovy.template.configuration.expand-empty-elements
是使用<br/>
這種形式,還是<br></br>
這種展開模式,默認為: false) - spring.groovy.template.configuration.locale
指定template locale. - spring.groovy.template.configuration.new-line-string
當啟用自動換行時,換行的輸出,默認為系統的line.separator屬性的值 - spring.groovy.template.configuration.resource-loader-path
指定groovy的模板路徑,默認為classpath:/templates/ - spring.groovy.template.configuration.use-double-quotes
指定屬性要使用雙引號還是單引號,默認為false - spring.groovy.template.content-type
指定Content-Type. - spring.groovy.template.enabled
是否開啟groovy模板的支持. - spring.groovy.template.expose-request-attributes
設定所有request的屬性在merge到模板的時候,是否要都添加到model中. - spring.groovy.template.expose-session-attributes
設定所有request的屬性在merge到模板的時候,是否要都添加到model中. - spring.groovy.template.expose-spring-macro-helpers
設定是否以springMacroRequestContext的形式暴露RequestContext給Spring’s macro library使用 - spring.groovy.template.prefix
指定模板的前綴. - spring.groovy.template.request-context-attribute
指定RequestContext屬性的名. - spring.groovy.template.resource-loader-path
指定模板的路徑,默認為: classpath:/templates/ - spring.groovy.template.suffix
指定模板的后綴 - spring.groovy.template.view-names
指定要使用模板的視圖名稱.