spring-boot配置html模板引擎freemarker、thymeleaf

  好了,今天接着说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&amp;amp;characterEncoding=utf-8&amp;amp;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 < StringObject >  map){
          map. put( "name""[Angel -- 使]");
         return  "/fm/welcome";
    }
    
    @ RequestMapping
     public  String  testModel1( Map < StringObject >  map){
          map. put( "name""[Angel -- 使]");
         return  "/fm/welcome";
    }
}

由于不可以两个引擎不可以兼容,所以我们需要一个一个来测试,故先注释掉thymeleaf的参数配置,如果需要用到请自己打开(并注释freemarker),启动服务即可访问了。

It is over!!下一章节,讲解spring-boot配置redis,并使用它,谢谢大家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值