1. 程式人生 > >spring boot: thymeleaf模板引擎使用

spring boot: thymeleaf模板引擎使用

sage homepage 順序 object tomcat fig component art format

spring boot: thymeleaf模板引擎使用

在pom.xml加入thymeleaf模板依賴

<!-- 添加thymeleaf的依賴 -->  
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
    

  

在applicationContext.properties中增加thymeleaf配置

########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
spring.thymeleaf.content-type=text/html
# set to false for hot refresh 
spring.thymeleaf.cache=false

  

#<!-- 關閉thymeleaf緩存 開發時使用 否則沒有實時畫面-->
spring.thymeleaf.cache=false
## 檢查模板是否存在,然後再呈現
spring.thymeleaf.check-template-location=true
#Content-Type值
spring.thymeleaf.content-type=text/html
#啟用MVC Thymeleaf視圖分辨率
spring.thymeleaf.enabled=true
## 應該從解決方案中排除的視圖名稱的逗號分隔列表
##spring.thymeleaf.excluded-view-names=
#模板編碼
spring.thymeleaf.mode=LEGACYHTML5
# 在構建URL時預先查看名稱的前綴
spring.thymeleaf.prefix=classpath:/templates/
# 構建URL時附加查看名稱的後綴.
spring.thymeleaf.suffix=.html
# 鏈中模板解析器的順序
#spring.thymeleaf.template-resolver-order= o
# 可以解析的視圖名稱的逗號分隔列表
#spring.thymeleaf.view-names=
#thymeleaf end

  

來看下我的實例:

pom.xml參考

<parent>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-parent</artifactId>
  	<version>1.5.9.RELEASE</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
  
  
  <!-- spring mvc,aop,restful,fastjson -->
  <dependency>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  
  
  <!-- jstl -->
  <dependency>
  	<groupId>javax.servlet</groupId>
  	<artifactId>jstl</artifactId>
  </dependency>
  
  <!-- tomcat的支持 -->
  <!-- dependency>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-tomcat</artifactId>
  	<scope>provided</scope>
  </dependency>
  <dependency>
  	<groupId>org.apache.tomcat.embed</groupId>
  	<artifactId>tomcat-embed-jasper</artifactId>
  	<scope>provided</scope>
  </dependency-->
  
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>
  
  <!-- mysql支持 -->
  <dependency>
  	<groupId>mysql</groupId>
  	<artifactId>mysql-connector-java</artifactId>
  </dependency>
  
 <!-- 添加spring-data-jpa依賴 -->
 <!-- 引入了 spring-data-jpa就不需要引入spring-boot-starter-jdbc -->
 <dependency>
 	<groupId>org.springframework.boot</groupId>
 	<artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>
 
    
  <!-- 添加thymeleaf的依賴 -->  
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
    
    
    
    <!-- fastjson -->  
    <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.15</version>
		</dependency>
    
    <!-- 熱部署 -->
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-devtools</artifactId>
    	<optional>true</optional>
    	<scope>true</scope>
    </dependency>
    
  </dependencies>
  
  <build>
  	<plugins>
  	
  		<!-- 熱部署  -->
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  			<configuration>
  				<!--fork :  如果沒有該項配置,呢個devtools不會起作用,即應用不會restart -->
  				<fork>true</fork>
  			</configuration>
  		</plugin>
  		
  	</plugins>
  </build>
  

  

App.java參考

package com.muyang.boot22;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * Hello world!
 *
 */

@SpringBootApplication
public class App 
{
	
	//fastJson配置
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters()
	{
		
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		
		fastConverter.setFastJsonConfig(fastJsonConfig);
		
		HttpMessageConverter<?> converter = fastConverter;
		return new HttpMessageConverters(converter);
	
	}
	
    public static void main( String[] args )
    {
        //System.out.println( "Hello World!" );
    	SpringApplication.run(App.class, args);
    }
}

  

applicationContext.properties

#####################################
###default-view
#####################################
#spring.mvc.view.prefix=/WEB-INF/jsp/
#spring.mvc.view.suffix=.jsp


################################
### spring port
################################
server.port = 8081
server.context-path=/springboot


################################
### zh/cn
###############################
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8


########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
spring.thymeleaf.content-type=text/html
# set to false for hot refresh 
spring.thymeleaf.cache=false




#######################################
####mysql
#######################################

spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

  

HelloController.java

package com.muyang.boot22.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/")
public class HelloController 
{

	/*@RequestMapping("/hello")
	public String index(Map<String, Object>map)
	{
		map.put("name", "張三");
		return "hello";
		//int i = 1024/0;
	}*/
	
	
	@RequestMapping(value="/hello")
	public String thyhello(Map<String, Object> map)
	{
		map.put("name", "張三");
		map.put("age", "56");
		map.put("gender", "man");
		return "hello";
		
	}
	
	@RequestMapping(value = "/home")
     public String homePage(Map<String, Object> map)throws IOException{
		return "home";
     }
	
	//跳轉
	 @GetMapping(value = "/index")
     public void homePage(HttpServletResponse response)throws IOException{
         response.sendRedirect("home.html");
//         return "index";
     }
	
}

  

hello.html

<!DOCTYPE html>
<html>
<head>thymeleaf - views</head>
<body>

      <h1>
	 	Hello,thymeleaf
	 	<br />
	 	This is my first thymeleaf demo.
	 	
	 	
	 	<hr />
	 	 	name: <span th:text="${name}"></span>
	 	age: <span th:text="${age}"></span>
	 	gender: <span th:text="${gender}"></span>
	 	
	 	
	 	</h1>
	 	
	 	ssssssssssss
</body>
</html>

  

技術分享圖片

訪問地址:

http://localhost:8081/springboot/hello

http://localhost:8081/springboot/home.html

http://localhost:8081/springboot/index

spring boot: thymeleaf模板引擎使用