springboot中已经不推荐使用jsp,而是推荐使用模板,如freemarker,thymeleaf等,本文记录在sprigboot中使用模板。

创建一个maven的springboot工程,

freemarker,要使用freemarker模板需引入所需要的jar,pom.xml如下:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.allen.springboot.learn</groupId>
<artifactId>springboot_freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent> <dependencies>
<!-- web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- Freemarker 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies> </project>

在resources目录下创建application.properties文件,添加freemarker配置信息,代码如下:

 ##FREEMARKER
spring.freemarker.template-loader-path=classpath:/view/
spring.freemarker.suffix=.ftl
#spring.freemarker.prefix=
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false

这些配置信息中,比较重要的是前两行,分别指定文件所在路径和文件名的后缀。

至此freemarker的配置已经完成,接下来创建controller,实体类和模板,使用freemarker模板

 /**
*
*/
package com.allen.springboot.learn.controller; import java.util.ArrayList;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.allen.springboot.learn.entity.UserInfo; /**
* @author admin
*
*/
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController { @RequestMapping("/hello")
public String hello(Model model){
UserInfo u1 = new UserInfo();
u1.setId(1);
u1.setEmail("11@qq.com");
u1.setAge(12);
u1.setPassword("111111");
u1.setSex("男");
u1.setUsername("allen");
model.addAttribute("u1", u1); List<UserInfo> userList = new ArrayList<UserInfo>();
userList.add(u1);
UserInfo u2 = new UserInfo();
u2.setId(2);
u2.setEmail("22@qq.com");
u2.setAge(22);
u2.setPassword("222222");
u2.setSex("F");
u2.setUsername("kobe");
userList.add(u2);
UserInfo u3 = new UserInfo();
u3.setId(3);
u3.setEmail("33@qq.com");
u3.setAge(33);
u3.setPassword("333");
u3.setSex("M");
u3.setUsername("kg");
userList.add(u3);
model.addAttribute("userList", userList);
return "fmk";
} }
 /**
*
*/
package com.allen.springboot.learn.entity; /**
* @author admin
*
*/
public class UserInfo { private Integer id;
private String username;
private String password;
private String sex;
private String email;
private int age; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
 <!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
用户信息<br/>
id:${u1.id}<br/>
username:${u1.username}<br/>
password:${u1.password}<br/>
sex:${u1.sex}<br/>
email:${u1.email}<br/>
age:${u1.age}
<hr/>
列表信息<br/>
<table>
<#if (userList ? size > 0 )>
<#list userList as user>
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.sex}</td>
<td>${user.email}</td>
<td>${user.age}</td>
</tr>
</#list>
<#else>
没有数据
</#if>
</table> </body>
</html>

现在就可以启动工程,访问controller了,信息正常信息

至此在springboot中使用freemarker已完成。

接下来看看使用thymeleaf模板,thymeleaf模板是springboot默认使用的模板,模板文件默认路径是在src/main/resources/templates下,和上文一样,只需要在pom.xml文件中引入对应的jar。

创建模板:

 <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
hello <span style="color:red" th:text="${name}"></span>
</body>
</html>

创建controller:

 /**
*
*/
package com.allen.springboot.learn.controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; /**
* @author admin
*
*/
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController { @RequestMapping("/hello")
public String helloThymeleaf(Model model){
model.addAttribute("name", "Thymeleaf");
return "tmf";
} }

接下来启动项目,访问controller中的地址,浏览器端显示如下内容

springboot入门_模板的更多相关文章

  1. SpringBoot集成beetl模板快速入门

    SpringBoot集成beetl模板快速入门 首次探索 beetl官方网址:http://ibeetl.com/ 创建SpringBoot工程(idea) 新建工程 选择创建Spring工程 书写包 ...

  2. Spring_MVC_教程_快速入门_深入分析

    Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门  资源下载: Spring_MVC_教程_快速入门_深入分析V1.1.pdf Spring ...

  3. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  4. SpringBoot入门基础

    目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...

  5. Spring全家桶系列–[SpringBoot入门到跑路]

    //本文作者:cuifuan Spring全家桶————[SpringBoot入门到跑路] 对于之前的Spring框架的使用,各种配置文件XML.properties一旦出错之后错误难寻,这也是为什么 ...

  6. springBoot入门教程(图文+源码+sql)

    springBoot入门 1   springBoot 1.1 SpringBoot简介 Spring Boot让我们的Spring应用变的更轻量化.比如:你可以仅仅依靠一个Java类来运行一个Spr ...

  7. Springboot入门:

    Springboot入门: 1.springboot是基于spring的全新框架,设计目的:简化spring应用配置和开发过程. 该框架遵循“约定大于配置”原则,采用特定的方式进行配置,从而事开发者无 ...

  8. SpringBoot入门系列(十一)统一异常处理的实现

    前面介绍了Spring Boot 如何整合定时任务已经Spring Boot 如何创建异步任务和定时任务.不清楚的朋友可以看看之前的文章:<Spring Boot 入门系列文章> 接下来主 ...

  9. SpringBoot入门详细教程

    一.SpringBoot入门 1.SpringBoot简介 SpringBoot是整个Spring技术栈的整合,来简化Spring应用开发,约定大于配置,去繁从简,just run 就能创建一 个独立 ...

随机推荐

  1. Google免费GPU使用教程

    今天突然看到一篇推文,里面讲解了如何薅资本主义羊毛,即如何免费使用Google免费提供的GPU使用权. 可以免费使用的方式就是通过Google Colab,全名Colaboratory.我们可以用它来 ...

  2. Java并发编程:Thread类的使用(转载)

    一:线程的状态: 在正式学习Thread类中的具体方法之前,我们先来了解一下线程有哪些状态,这个将会有助于后面对Thread类中的方法的理解. 线程从创建到最终的消亡,要经历若干个状态.一般来说,线程 ...

  3. Mysql Index extends优化

    Innodb通过自动把主键列添加到每个二级索引来扩展它们: CREATE TABLE t1 ( i1 , i2 , d DATE DEFAULT NULL, PRIMARY KEY (i1, i2), ...

  4. JAVA异步加回调的例子

    package com.sunchao.callback; /** * callback interface * @author Administrator * */ public interface ...

  5. TF-卷积函数 tf.nn.conv2d 介绍

    转自 http://www.cnblogs.com/welhzh/p/6607581.html 下面是这位博主自己的翻译加上测试心得 tf.nn.conv2d是TensorFlow里面实现卷积的函数, ...

  6. mysql 查看索引使用情况

    show status like 'Handler_read%'; Handler_read_key  代表着一个行被索引值读取的次数,值很低表明索引不经常用到,增加索引对性能改善不高. Handle ...

  7. java面向对象的三大特性——多态

    多态 所谓多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象,该引用变量发出的方法调用到底 ...

  8. 输入和输出--java的NIO

    Java的NIO 实际开发中NIO使用到的并不多,我并不是说NIO使用情景不多,是说我自己接触的并不是很多,前面我在博客园和CSDN上转载了2篇别人写的文章,这里来大致总结下Java的NIO,大概了解 ...

  9. mysql关于char和varchar的查询效率问题

    看了好多资料都说 varchar(size) 可变长度的字符值,节省空间,查询效率低 char(size) 固定长度的字符值,浪费空间,查询效率高 但是实际测试  char(100)   varcha ...

  10. jsp中 scope="application" 表示

    jsp中 <jsp:useBean id="countbean" scope="application" class="count.counte ...