springBoot (5): web开发-模板引擎FreeMarker与thymeleaf

  

春季启动的网络应用开发,是基于spring mvc。


弹簧引导在弹簧默认基础上,自动配置添加了以下特性:

, 1包含了ContentNegotiatingViewResolver和BeanNameViewResolver bean。

, 2,对静态资源的支持,包括对WebJars的支持。

, 3,自动注册转换器,GenericConverter,格式化器bean。

, 4,对HttpMessageConverters的支持。

, 5、自动注册MessageCodeResolver。

, 6,对静态index . html的支持。

, 7,对自定义图标的支持。

, 8日主动使用ConfigurableWebBindingInitializer bean


FreeMarker Thymeleaf

速度(1.4版本之后弃用,spring框架4.3版本之后弃用)Groovy

胡子

, 1, jsp只能打包为:战争格式,不支持jar格式,只能在标准的容器里面跑(tomcat、jetty都可以)

, 2,内嵌的jetty目前不支持jsp

, 3,暗潮不支持jsp

, 4, jsp自定义错误页面不能覆盖弹簧引导默认的错误页面

新建一个工程,勾选FreeMarker, DevTools(开发方便)

 springBoot (5): web开发-模板引擎FreeMarker与Thymeleaf

会自动在砰的一声。xml中加入Freemarker的配置:

& lt; dependency>   ,,& lt; groupId> org.springframework.boot   ,,& lt; artifactId> spring-boot-starter-freemarker   & lt;/dependency>


package  com.example.demo.controller;      import  org.slf4j.Logger;   import  org.slf4j.LoggerFactory;   import  org.springframework.stereotype.Controller;   import  org.springframework.ui.Model;   import  org.springframework.web.bind.annotation.RequestMapping;/* *   ,* Created  by  DELL 提醒2017/6/13。   ,*/@ controller   @RequestMapping (“/web")   public  class  WebController  {   ,,,private  static  final  Logger  Logger =, LoggerFactory.getLogger (WebController.class);      ,,,@RequestMapping (“/index")   ,,,public  String 指数(Model 模型){   ,,,,,,,logger.info(“这是一个controller");   ,,,,,,,model.addAttribute (“title",“我是一个例子“);   ,,,,,,,return “index",,,//,注意,不要再最前面加上/,linux下面会出的错   ,,,}   }

& lt; ! DOCTYPE  html>   & lt; html>   & lt; head  lang=癳n"祝辞   ,,& lt; title> Spring  Boot  Demo 作用;FreeMarker   ,,,& lt; https://www.yisu.com/zixun/link  href="/css/index.css”rel="样式表"/>   头   <身体>   <中心>      

${标题}

     <脚本>   $(函数(){   $(" #标题”).click(函数(){   alert('点击了');   });   })>   


说明:

, 1,视图默认访问模板下面,如“index",则:在模板下面找index.ftl

, 2, css, js, img等静态资源则在静态下面找,如& lt; https://www.yisu.com/zixun/链接的href="/css/index.css”rel="样式表"/>,则是找静态下面的css下面的指数。css文件

Thymeleaf是一个优秀的面向java的XML/XHTML/HTML5页面模板,并且有丰富的标签语言和函数。使用Springboot框架进行界面设计,一般都会选择Thymeleaf模板。

引入依赖:

& lt; dependency>   ,,& lt; groupId> org.springframework.boot   ,,& lt; artifactId> spring-boot-starter-thymeleaf   & lt;/dependency>


# # # # # # # # # # # # # # # # # # # # # thymeleaf开始# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #   #关闭缓存   spring.thymeleaf.cache=false   #后缀   spring.thymeleaf.suffix=. html   #编码   spring.thymeleaf.encoding=utf - 8   # # # # # # # # # # # # # # # # # # # # # thymeleaf结束# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

@ controller   public  class  IndexController  {   ,,,@RequestMapping (“/index")   ,,,public  String 显示(Model 模型),throws  Exception  {   ,,,,,,,List, users =, new  ArrayList ();   ,,,,,,,users.add (new 用户(1 l,“zhangsan"));   ,,,,,,,users.add (new 用户(2 l,“lisi"));   ,,,,,,,users.add (new 用户(3 l,“wangwu"));   ,,,,,,,model.addAttribute (“hello",“我只是一个例子“);   ,,,,,,,model.addAttribute (“users",,用户);   ,,,,,,,model.addAttribute (“addtime" new 日期());   ,,,,,,,return"/helloHtml";   ,,,}   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

springBoot (5): web开发-模板引擎FreeMarker与thymeleaf