开发IDE:spring tool suite
步骤:
1)new --->Spring Starter Project
2) 填写项目信息,见下图
3)下一步后出现下图:
因为在上次已经建立过一个基于Spring-boot的web工程,所以Frequently used中出现web、thymeleaf,可以根据实际项目需要选择相应模块,如security等
4)按照提示下一步后再下一步,工程就建立好了,如下图:
5)写一个controller
-
import org.springframework.stereotype.Controller; -
import org.springframework.web.bind.annotation.RequestMapping; -
import org.springframework.web.bind.annotation.RequestMethod; -
@Controller -
public class MainController { -
@RequestMapping(value="/index",method=RequestMethod.GET) -
public String get(){ -
return "index"; -
} -
}
6)src/main/java中写一个页面(thymeleaf为模板)index.html,页面代码如下:
-
<!DOCTYPE html> -
<html lang="en" xmlns:th="http://www.thymeleaf.org"> -
<head>this is testing</head> -
<body> -
<td>Hello,mifeng! this is for testing</td> -
</body> -
</html>
7)完成后项目结构如下图
8)启动项目,浏览器可以看到效果
下面是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.mifeng.test</groupId> -
<artifactId>web-practice</artifactId> -
<version>0.0.1-SNAPSHOT</version> -
<packaging>jar</packaging> -
<name>web-practice</name> -
<description>web-practice</description> -
<parent> -
<groupId>org.springframework.boot</groupId> -
<artifactId>spring-boot-starter-parent</artifactId> -
<version>1.5.1.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.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> -
</dependency> -
</dependencies> -
<build> -
<plugins> -
<plugin> -
<groupId>org.springframework.boot</groupId> -
<artifactId>spring-boot-maven-plugin</artifactId> -
</plugin> -
</plugins> -
</build> -
</project>