一、SpringBoot 集成SpringMVC
项目结构
user-springmvc
--|java
--|com.mvc
--|config
--|controller
--|entity
--|service
--|MvcApplication
--|resources
--|static
--|templates
--|poml.xml
文件说明:
- 1、pom文件:用于maven配置
- 2、config文件夹:存放java的配置类
- 3、controller文件夹:用于存放Controller类
- 4、entity文件夹:用于存放java实体类
- 5、service文件夹:用于存放service类
- 6、statice文件夹:用于存放静态资源数据(js,css,图片文件)
- 7、templates文件夹:用于存放视图文件(html)
1、pom文件添加
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.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>
<modelVersion>4.0.0</modelVersion>
<artifactId>use-springmvc</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
2、创建启动类
package com.mvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author brusion
* @date 2018/9/26
*/
@SpringBootApplication
public class MvcApplication {
public static void main(String[] args) {
SpringApplication.run(MvcApplication.class, args);
}
}
说明:
- 注解SpringBootApplication:声明为一个SpringBoot启动类
3、Controller类
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author brusion
* @date 2018/9/26
*/
@Controller
public class MvcController {
@RequestMapping("/test")
public String say() {
return "/index";
}
@RequestMapping("/data")
@ResponseBody
public String getData() {
return getClass().getName().toString();
}
}
说明:
- Controller注解:声明当前类为一个Controller类
- RequestMapping注解:作用于方法上用于声明请求路径
- ResponseBody注解:用于声明返回的是String类型的数据不是视图
4、测试:
1、运行启动类