一.什么是spring boot
spring boot是一个全新框架,其作用是用来简化spring搭建和开发过程的
二.具体示例
创建maven项目,编辑pom.xml文件,添加以下内容:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
接下来创建java类进行测试:
@SpringBootApplication
@RestController
public class Test{
@RequestMapping("/")
public String testUrlOne(){
return "学习spring boot";
}
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
}
main方式运行,进去浏览器访问localhost:8080,可以看到学习spring boot
方式二:访问html、velocity 等模板
添加模板文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
修改java代码
添加一个java类,改为TestMain
将main方式代码,全部写到TestMain里面
@SpringBootApplication
public class TestMain{
public static void main(String[] args) {
SpringApplication.run(TestMain.class, args);
}
}
修改Test方式,重新写一个方法,便于区分
@Controller
public class Test{
@RequestMapping("/index")
public String test2(Model model){
model.addAttribute("name","boot");
return "index";
}
在resources下面建立文件templates,在里面创建index.html文件
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'名称: ' + ${name} + '!'"/>
</body>
</html>
启动main方法,访问localhost:8080/index 就可以访问到页面了