1. 程式人生 > >web應用開發-模板引擎JSP

web應用開發-模板引擎JSP

  • spring boot的web應用開發,是基於spring mvc
  • Spring boot 在spring預設基礎上,自動配置添加了以下特性:
  1. 包含了ContentNegotiatingViewResolver和BeanNameViewResolver beans。
  2. 對靜態資源的支援,包括對WebJars的支援。
  3. 自動註冊Converter,GenericConverter,Formatter beans。
  4. 對HttpMessageConverters的支援。
  5. 自動註冊MessageCodeResolver。
  6. 對靜態index.html的支援。
  7. 對自定義Favicon的支援。
  8. 主動使用ConfigurableWebBindingInitializer bean
  • 模板引擎的選擇

FreeMarker

Thymeleaf

Velocity (1.4版本之後棄用,Spring Framework 4.3版本之後棄用)

Groovy

Mustache

注:jsp應該儘量避免使用,原因如下:

  1. jsp只能打包為:war格式,不支援jar格式,只能在標準的容器裡面跑(tomcat,jetty都可以)
  2. 內嵌的Jetty目前不支援JSPs
  3. Undertow不支援jsps
  4. jsp自定義錯誤頁面不能覆蓋spring boot 預設的錯誤頁面
  • Jsp Demo

         <dependency>

              <groupId>org.apache.tomcat.embed</groupId>

              <artifactId>tomcat-embed-jasper</artifactId>

              <scope>provided</scope>

         </dependency>

         <dependency>

              <groupId>javax.servlet</groupId>

              <artifactId>jstl</artifactId>

         </dependency>

新增配置引數:

spring.mvc.view.prefix: /WEB-INF/templates/

spring.mvc.view.suffix: .jsp