springboot 中不建议使用jsp作为页面展示, 怎么使用可以看: http://412887952-qq-com.iteye.com/blog/2292471

关于什么是thymeleaf, 可以参照:http://www.cnblogs.com/vinphy/p/4674247.html

springboot 默认是使用的thymeleaf模板引擎的,

使用thymeleaf

1, 在pom.xml中加入依赖即可:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>

2, 关闭thymeleaf缓存:

  1. ########################################################
  2. ###THYMELEAF (ThymeleafAutoConfiguration)
  3. ########################################################
  4. #spring.thymeleaf.prefix=classpath:/templates/
  5. #spring.thymeleaf.suffix=.html
  6. #spring.thymeleaf.mode=HTML5
  7. #spring.thymeleaf.encoding=UTF-
  8. # ;charset=<encoding> is added
  9. #spring.thymeleaf.content-type=text/html
  10. # set to false for hot refresh
  11. spring.thymeleaf.cache=false

3, 编写模板文件, resource下的template为默认文件位置

/resource/templates/helloHtml.html

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  3. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  4. <head>
  5. <title>Hello World!</title>
  6. </head>
  7. <body>
  8. <h1 th:inline="text">Hello.v.</h1>
  9. <p th:text="${hello}"></p>
  10. </body>
  11. </html>

4, 编写controller

  1. package com.iwhere.test.web;
  2.  
  3. import java.util.Map;
  4.  
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7.  
  8. /**
  9. * thelemeaf访问controller层
  10. * @author wenbronk
  11. * @time 2017年3月17日 下午2:06:32 2017
  12. */
  13. @Controller
  14. public class TemplateController {
  15.  
  16. @RequestMapping("/helloHtml")
  17. public String helloHtml(Map<String, Object> map) {
  18. map.put("hello", "from TemplateController.helloHtml");
  19. return "/helloHtml";
  20. }
  21.  
  22. }
thymeleaf 上的表达式:
  1. <html xmlns:th="http://www.thymeleaf.org"></>
    通过 xmlns命名空间引入thymeleaf
    需要动态处理的元素前 加 th:
  2. <link th:src="@{bootstrap/css/bootstrap.min.css}" rel = "stylesheet"/>
    通过 @{} 获取web静态资源
  3.  
  4. <span th:text="${model.name}"></>
    通过 ${} 获取model的属性
  5.  
  6. <div th:if="${not #lists.isEmpty(people)}">
    通过 ${not #lists.isEmpty(list)} 判断people是否为空
  7.  
  8. <li th:each="person:${people}">
    遍历元素 people
      <button th:onclick="'getName(\'' + $(persion.name} + '\'});'">
        动态给元素绑定值
  9.  
  10. <script th:inline="javascript">
      var single = [[${singlePerson}]];
      console.log(single.name + ": " + single.age)
    </>
    可在js中取值

使用freemarker

1, 加入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-freemarker</artifactId>
  4. </dependency>

2, applicaiton.properties

  1. ########################################################
  2. ###FREEMARKER (FreeMarkerAutoConfiguration)
  3. ########################################################
  4. spring.freemarker.allow-request-override=false
  5. spring.freemarker.cache=true
  6. spring.freemarker.check-template-location=true
  7. spring.freemarker.charset=UTF-
  8. spring.freemarker.content-type=text/html
  9. spring.freemarker.expose-request-attributes=false
  10. spring.freemarker.expose-session-attributes=false
  11. spring.freemarker.expose-spring-macro-helpers=false
  12. #spring.freemarker.prefix=
  13. #spring.freemarker.request-context-attribute=
  14. #spring.freemarker.settings.*=
  15. #spring.freemarker.suffix=.ftl
  16. #spring.freemarker.template-loader-path=classpath:/templates/#comma-separatedlist
  17. #spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved

3, controller

  1. /**
  2. * 返回html模板.
  3. */
  4. @RequestMapping("/helloFtl")
  5. public String helloFtl(Map<String,Object> map){
  6. map.put("hello","from TemplateController.helloFtl");
  7. return"/helloFtl";
  8. }

4, 模板文件

helloFtl.ftl

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  3. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  4. <head>
  5. <title>Hello World!</title>
  6. </head>
  7. <body>
  8. <h1>Hello.v.</h1>
  9. <p>${hello}</p>
  10. </body>
  11. </html>

freemarker 和 thymeleaf是可以共存的

jsp的支持

jsp在springboot中已经被弱化了,

1, 导入maven依赖

  1. <!-- servlet 依赖. -->
  2. <dependency>
  3. <groupId>javax.servlet</groupId>
  4. <artifactId>javax.servlet-api</artifactId>
  5. <scope>provided</scope>
  6. </dependency>
  7.  
  8. <!--
  9. JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。JSTL只能运行在支持JSP1.2和Servlet2.3规范的容器上,如tomcat .x。在JSP .0中也是作为标准支持的。
  10.  
  11. 不然报异常信息:
  12. javax.servlet.ServletException: Circular view path [/helloJsp]: would dispatch back to the current handler URL [/helloJsp] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
  13.  
  14. -->
  15. <dependency>
  16. <groupId>javax.servlet</groupId>
  17. <artifactId>jstl</artifactId>
  18. </dependency>
  19.  
  20. <!-- tomcat 的支持.-->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-tomcat</artifactId>
  24. <scope>provided</scope>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.apache.tomcat.embed</groupId>
  28. <artifactId>tomcat-embed-jasper</artifactId>
  29. <scope>provided</scope>
  30. </dependency>

jdk编译版本

  1. <build>
  2. <finalName>spring-boot-jsp</finalName>
  3. <plugins>
  4. <plugin>
  5. <artifactId>maven-compiler-plugin</artifactId>
  6. <configuration>
  7. <source>1.8</source>
  8. <target>1.8</target>
  9. </configuration>
  10. </plugin>
  11. </plugins>
  12. </build>

2, application.properties

  1. # 页面默认前缀目录
  2. spring.mvc.view.prefix=/WEB-INF/jsp/
  3. # 响应页面默认后缀
  4. spring.mvc.view.suffix=.jsp
  5. # 自定义属性,可以在Controller中读取
  6. application.hello=Hello Angel From application

3, controller

  1. package com.kfit.jsp.controller;
  2.  
  3. import java.util.Map;
  4.  
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8.  
  9. /**
  10. * 测试
  11. * @author Angel(QQ:412887952)
  12. * @version v.0.1
  13. */
  14. @Controller
  15. public class HelloController {
  16.  
  17. // 从 application.properties 中读取配置,如取不到默认值为Hello Shanhy
  18. @Value("${application.hello:Hello Angel}")
  19. private String hello;
  20.  
  21. @RequestMapping("/helloJsp")
  22. public String helloJsp(Map<String,Object> map){
  23. System.out.println("HelloController.helloJsp().hello="+hello);
  24. map.put("hello", hello);
  25. return "helloJsp";
  26. }
  27. }

4, jsp页面 , 放在  webapps/WEB-INF/ 下

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. helloJsp
  11. <hr>
  12. ${hello}
  13.  
  14. </body>
  15. </html>

原文地址: http://412887952-qq-com.iteye.com/blog/2292402

springboot-10-前端页面整合, thymeleaf, freemarker, jsp 模板使用的更多相关文章

  1. springboot同时使用thymeleaf和jsp模板

    语言:javaEE 框架:springboot+thymeleaf.jsp模板引擎 背景:学习springboot过程中想同时使用thymeleaf和jsp访问(官方不建议) 步骤: 1)  在pom ...

  2. [Java] Spring boot2 整合 Thymeleaf 后 去除模板缓存

    Spring boot2 整合 Thymeleaf 后 去除模板缓存 网上好多文章只是简单粗暴的说,在 application.properties  做如下配置即可: #Thymeleaf cach ...

  3. SpringBoot 获取前端页面参数的集中方式总结

    SpringBoot的一个好处就是通过注解可以轻松获取前端页面的参数,之后尅将参数经过一系列处理传送到后台数据库,前端时间正好用到.大致分为一下几种: 1.指定前端URL请求参数名称与方法名称一致,这 ...

  4. SpringBoot集成前端模版(thymeleaf)

    1.在application.properties配置文件中添加 thymeleaf 的配置信息 spring.datasource.driverClassName=com.mysql.jdbc.Dr ...

  5. 十一、springboot(六)整合Thymeleaf

    1.添加jar包依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...

  6. SpringBoot 同时整合thymeleaf html、vue html和jsp

    问题描述 SpringBoot如何同时访问html和jsp SpringBoot访问html页面可以,访问jsp页面报错 SpringBoot如何同时整合thymeleaf html.vue html ...

  7. SpringBoot页面展示Thymeleaf

    https://www.jianshu.com/p/a842e5b5012e 开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了.Spring ...

  8. SpringBoot第九集:整合JSP和模板引擎Freemarker/Thymeleaf(2020最新最易懂)

    SpringBoot第九集:整合JSP和模板引擎(2020最新最易懂) 当客户通过前端页面提交请求后,我们以前是怎么做的?后端接收请求数据,处理请求,把响应结果交给模板引擎JSP,最后将渲染后的JSP ...

  9. 07.整合jsp、整合freemarker、整合thymeleaf

    整合jsp pom.xml部分内容 <packaging>war</packaging> </dependencies> <dependency> &l ...

随机推荐

  1. 【spfa训练】HDU4725 (层级建图)

    HDU4725 题目大意:一些节点分布在不同的层上,已知相邻的层可以往来距离为c,在给你一些已知的边,问你点1-n的最短路 分析:越往后做,越觉得最短路的考点已经不是spfa算法还是dijkscar算 ...

  2. setTimeout问题

    function fn(argu1){ alert(argu1); } setTimeout(fn, 1000, 12); setTimeout从第三个参数开始,之后的参数都是fn的.fn不用加(), ...

  3. jquery ui导入两次的错误提示

    如果jquery ui plugin的js文件出现到两次的话,就会出现报错. 解决办法: 找出引用了jquery ui 的文件,将其中一个去掉就ok了. 在Firefox下面的报错提示: TypeEr ...

  4. Delphi Language Overview

    Delphi is a high-level, compiled, strongly typed language that supports structured and object-orient ...

  5. 转载:R语言rvest包使用

    R中有好几个包都可以抓取网页数据,但是rvest + CSS Selector最方便. 通过查看器立刻知道表格数据都在td:nth-child(1),td:nth-child(3)之类的节点中,直接代 ...

  6. C#写文本文件,如何换行(添加换行符)

    把文本写到文件中,如果是几段文字拼合起来输出到文件中,通常每段非结尾文字后需要添加换行符,不然几段文字都变成一段. 在 C# 中,文本换行有两种方法,一种在需要换行的文本后面添加换行符 \r\n 即可 ...

  7. numpy 数组相减

    a与b的每一列相减

  8. Ubuntu 16.04LTS安装Nginx

    Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...

  9. python里面如何拷贝一个对象?deepcopy 和 copy 有什么区别 ?

    深拷贝就是说原内容改变但是拷贝的性内容不会改变,copy.copy和deepcopy对一个不可变类型进行拷贝, name结果相同都是浅拷贝指向引用如果是可变的话, 即使元组在最外层, 那么deepco ...

  10. Android中获取系统内存信息以及进程信息-----ActivityManager的使用(一)

    本节内容主要是讲解ActivityManager的使用,通过ActivityManager我们可以获得系统里正在运行的activities,包括 进程(Process)等.应用程序/包.服务(Serv ...