springboot入门_模板

时间:2022-07-20 04:49:19

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入门_模板

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

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

springboot入门_模板

创建模板:

 <!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入门_模板