spring-boot配置html模板引擎freemarker、thymeleaf
2017-09-04 13:09
726 查看
好了,今天接着说spring-boot,主要讲解的是如何使用spring-boot进行网页端开发。
我们说到网页端开发,首先一定会想到html跳转,那么我们来进行html跳转呢?
我本来以为不需要配置任何文件可以直接跳转html,so,我写了一个html跳转,但是IT is wrong!我查了一些资料,了解到我们需要配合html模板引擎来使用,比如freemarker、thymeleaf。
注:好多的文章说,这两个模板引擎是可以兼容的,但是我尝试了一下,却没有成功。如果朋友们发现了怎么让这两个模板引擎兼容,请告诉我,谢谢!邮箱:18703642982@163.com
我们直接上pom代码:
<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>demo-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo-boot</name>
<description>Demo project for Spring Boot</description>
<!-- 引入父项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<!-- 参数设置 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mysql-connector.version>5.1.21</mysql-connector.version>
</properties>
<!-- jar包依赖 -->
<dependencies>
<!-- spring-boot web引入,支持 Web 应用开发,包含 Tomcat 和 spring-mvc。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf 模板引擎引入 -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> -->
<!-- freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- redis 引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<!-- 添加适用于生产环境的功能,如性能指标和监测等功能 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring-boot 配置处理器引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!-- apache tomcat jdbc 引入 -->
<!-- <dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency> -->
<!-- dbcp 连接池引入 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.4</version>
</dependency>
<!-- mysql 驱动包引入 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.21</version>
<!-- <version>${mysql-connector.version}</version> -->
</dependency>
<!-- mybatis 引入 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我们在pom中添加了两个模板引擎,哦,忘记了,应该先看项目结构的,sorry:
there is no explain!不解释,今天有点懒,直接上application配置和java代码
##server.port: 8082
#server.tomcat.uri-encoding=UTF-8
##默认数据源配置
spring.datasource.url=jdbc:mysql://119.23.78.228:3306/shares_manager?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##数据库链接池参数配置
#指定使用哪个连接池,默认使用tomcate-jdbc连接池
spring.datasource.type=org.apache.commons.dbcp.BasicDataSource
#连接池使用的数据库账号
spring.datasource.dbcp.username=root
#连接池使用的数据库密码
spring.datasource.dbcp.password=123456
#连接池使用的数据库驱动类
spring.datasource.dbcp.driver-class-name=com.mysql.jdbc.Driver
#初始化链接数
spring.datasource.dbcp.initial-size=1
#最大链接数
spring.datasource.dbcp.max-active=20
#最小空闲数量
spring.datasource.dbcp.min-idle=1
#最大空闲数量
spring.datasource.dbcp.max-idle=20
#最大等待时间 - 60秒
spring.datasource.dbcp.max-wait=6000
##为实体对象所在的包,跟数据库表一一对应
#mybatis.typeAliasesPackage=com.example.model
##mapper文件的位置
#mybatis.mapperLocations=classpath:com/example/mapper/*.xml
##log设置(两种方式,1.log参数配置,2.logback.xml配置)
##freemarker 配置
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
#spring.freemarker.prefix=classpath:/templates/
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates
#spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/tm/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html
#spring.thymeleaf.cache=true
##注:thymeleaf和freemarker不可以共存
##自定义参数
userName=Alex
bookTitle=Spring Boot入门
package com.example.controller;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller //这里有个坑就是@RestController,绝对不能用@RestController
@RequestMapping("/tm")
public class ThymeleafController {
private static Logger logger = Logger.getLogger(ThymeleafController.class);
/**
* 返回html模板.
*/
@RequestMapping("/thymeleaf")
public String helloHtml(Map<String,Object> map){
logger.info("hello from TemplateController.helloHtml");
map.put("hello","from TemplateController.helloHtml");
return"/helloHtml";
}
@RequestMapping
public String helloHtml1(Map<String,Object> map){
logger.info("hello from TemplateController.helloHtml");
map.put("hello","from TemplateController.helloHtml");
return"/helloHtml";
}
}
package com.example.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller //这里有个坑就是@RestController,绝对不能用@RestController
@RequestMapping("fm")
public class FreeMarkerController {
@RequestMapping("freemarker")
public String testModel(Map<String, Object> map){
map.put("name", "[Angel -- 守护天使]");
return "/fm/welcome";
}
@RequestMapping
public String testModel1(Map<String, Object> map){
map.put("name", "[Angel -- 守护天使]");
return "/fm/welcome";
}
}
由于不可以两个引擎不可以兼容,所以我们需要一个一个来测试,故先注释掉thymeleaf的参数配置,如果需要用到请自己打开(并注释freemarker),启动服务即可访问了。
It is over!!下一章节,讲解spring-boot配置redis,并使用它,谢谢大家。
相关文章推荐
- Spring Boot与FreeMarker集成后配置全局模板转义html/xml
- Spring Boot 系列 - web开发 Thymeleaf、FreeMarker模板引擎
- Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎
- SpringBoot-07引擎模板thymeleaf和freemarker的使用
- (18)使用模板(thymeleaf-freemarker)【从零开始学Spring Boot】
- SpringBoot学习(三)从yml文件中获取数据和Thymeleaf 模板引擎
- 【java】intellij idea SpringMVC 配置FreeMarker模板引擎
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- 详解SpringBoot+Thymeleaf 基于HTML5的现代模板引擎
- (9) spring boot使用freemarker模板引擎
- spring boot 使用velocity、freeMarker模板创建html页面返回给前端
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- SpringBoot添加freemarker模板引擎
- springboot模板方案thymeleaf和freemarker之比较
- SpringBoot不使用模板引擎直接返回html
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- 【java】intellij idea SpringMVC 配置FreeMarker模板引擎