springboot教程t2——thymeleaf模板

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dahaiaaaqe/article/details/81659955

1 springboot项目默认文件位置

  • /src/java/resources  Maven的资源文件目录
  • /src/java/resources/static
  • /src/java/resources/public
  • /src/java/resources/templates
  • /src/java/resources/META-INF/resources

springboot静态首页的支持

   默认情况下,springboot回到上面文件目录路径中找index.html(注意:controller注解不能使用@RestController,要用@Controller,因为@RestController =@Controller+@ResponeBody,会直接返回josn数据).

   即

classpath:/META-INF/resources/index.html  
classpath:/resources/index.html  
classpath:/static/index.html  
calsspath:/public/index.html  

  配置文件中修改映射时视图的前后缀(前缀prefix是”classpath:/templates/”,后缀suffix是”.html” )

spring.thymeleaf.prefix: classpath:/templates/  
spring.thymeleaf.suffix: .html 

 pom.xml引入的jar

 <!-- thymeleaf模板引擎 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

静态资源(css、js、图片等)默认放在resources/static下面。如果要修改默认存放目录,可以通过设置属性 spring.mvc.static-path-pattern来实现。thymeleaf模板的模板文件一般配置在static文件下,文件名后缀名为html。

3 代码示范

  SpringBootDemo71Application

package com.roncoo.education;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDemo71Application {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemo71Application.class, args);
	}
}

 WebController


package com.roncoo.education.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * spring-boot-demo-6-2
 * 
 * @author wujing
 */
@Controller
@RequestMapping(value = "/web")
public class WebController {

	@RequestMapping(value = "index")
	public String index(ModelMap map) {
		map.put("title", "thymeleaf hello word");
		return "index";
	}

}

   index.html

<!DOCTYPE html>
<html>
<head lang="en">
	<title>Spring Boot Demo - FreeMarker</title>
	
	<link href="/css/index.css" rel="stylesheet" />
	
</head>
<body>
	<center>
		<img src="/images/logo.png" />
		<h1 id="title" th:text="${title}"></h1>
	</center>
	
	<script type="text/javascript" src="/webjars/jquery/2.1.4/jquery.min.js"></script>
	
	<script>
		$(function(){
			$('#title').click(function(){
				alert('鐐瑰嚮浜�');
			});
		})
	</script>
</body>
</html>

  pom.xml

<?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.roncoo.education</groupId>
	<artifactId>spring-boot-demo-7-1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-demo-7-1</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.0.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

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

	<dependencies>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.webjars</groupId>
			<artifactId>jquery</artifactId>
			<version>2.1.4</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

猜你喜欢

转载自blog.csdn.net/dahaiaaaqe/article/details/81659955
T2
今日推荐