zl程序教程

您现在的位置是:首页 >  后端

当前栏目

springboot建父工程,整合Thymeleaf模板引擎

SpringBoot模板引擎 整合 工程 thymeleaf
2023-06-13 09:13:24 时间

springboot整合Thymeleaf模板引擎

开发环境

  • jdk:jdk1.8.0_212
  • maven:apache-maven-3.6.2
  • springboot版本:2.2.0 之前配置了一个快速入门课程,后面准备用springboot集成其他demo,为了后面方便集成,所以使用一个父工程来配置统一的依赖环境。

父工程构建

快速入门课程的基础上做了一些修改,作为父工程

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
    		<!--核心模块,包括自动配置支持、日志和YAML-->
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
    		<!--测试模块,包括JUnit、Hamcrest、Mockito-->
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

引入Web模块,方便后面测试

<dependency>
			<groupId>org.springframework.boot</groupId>
			<!--web模块-->
			<artifactId>spring-boot-starter-web</artifactId>
</dependency>

创建controller类com.zjq.demo.controller.TestController,内容如下

@RestController
public class TestController {

    @RequestMapping(value = "/hello")
    public String hello(){
        return "HelloWorld";
    }
}

启动主程序,访问 http://localhost:8080/hello ,可以看到页面输出 HelloWorld

使用maven构建

1.使用idea,点击File,new >>project,老版idea可以直接用 SPRING INITIALIZR 新建项目,新版的idea需要添加

Spring Assistant插件,添加好后重启idea,点击File,new >>project,选择Spring Assistant,选择SDK版本,使用default https://start.spring.io/ 构建项目

4.可以看到pom.xml文件中自动引入了这些依赖

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

引入Web模块,方便后面测

创建controller类com.zjq.demo.controller.TestController,内容如下

@RestController
public class TestController {

    @RequestMapping(value = "/hello")
    public String hello(){
        return "HelloWorldByIdea";
    }
}

启动主程序,访问 http://localhost:8080/hello ,可以看到页面输出 HelloWorldByIdea