Build springboot through Maven project

Programmer light 2022-02-13 07:52:40 阅读数:692

build springboot maven project

One 、 Choose maven, Create project

Two 、 add to POM File parent dependency

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

  3、 ... and 、 Support SpringMVC

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

Four 、 Create startup class 、rest Interface

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class MyApplication {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

5、 ... and 、 Executable jar Package configuration

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

6、 ... and 、 add to thymeleaf Templates

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

7、 ... and 、 Add the configuration

establish templates Folder

# apply name
spring.application.name=thymeleaf
# Application service WEB Access port
server.port=8080
# THYMELEAF (ThymeleafAutoConfiguration)
# Enable template cache ( The default value is : true )
spring.thymeleaf.cache=false
# Check if the template exists , And then present
spring.thymeleaf.check-template=true
# Check whether the formwork position is correct ( The default value is :true )
spring.thymeleaf.check-template-location=true
#Content-Type Value ( The default value is : text/html )
spring.thymeleaf.content-type=text/html
# Turn on MVC Thymeleaf View resolution ( The default value is : true )
spring.thymeleaf.enabled=true
# Template encoding
spring.thymeleaf.encoding=UTF-8
# List of view names to be excluded from resolution ,⽤ Comma separated
spring.thymeleaf.excluded-view-names=
# To be lucky ⽤ Template mode above template . another ⻅ StandardTemplate-ModeHandlers( The default value is : HTML5)
spring.thymeleaf.mode=HTML5
# In the build URL Prefix before view name ( The default value is : classpath:/templates/ )
spring.thymeleaf.prefix=classpath:/templates/
# In the build URL The suffix after the view name ( The default value is : .html )
spring.thymeleaf.suffix=.html

8、 ... and 、 add to controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
@RequestMapping("/index")
public ModelAndView index(){
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
return mv;
}
}

Nine 、 add to html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
index
</body>
</html>

Ten 、 visit

http://localhost:8080/index

copyright:author[Programmer light],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130752394057.html