1. 搭建springboot 项目
继承spring-boot-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>1.5.19.RELEASE</version>
</parent>
添加web组件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在resources 下编写application.properties 或者application.yml 文件
配置 编写application.yml
server:
port: 8081 # 配置端口
context-path: /xiaodu #配置根路径
编写java代码
启动类
/**
* springboot 启动类
* 这个类最好放在最外层包下,其他的java类放到他的包下
* 启动会扫描本包和子包下的类
*/
@SpringBootApplication
public class Springboot_Web_StartApp_8081 {
public static void main(String[] args) {
SpringApplication.run(Springboot_Web_StartApp_8081.class,args);
}
}
编写controller
/**
* @RestController 是@Controller ,@ResponseBody一起使用的作用
*/
@RestController
public class MyHelloController {
/**
* getMaping == @RequestMapping(method = {RequestMethod.GET})
* @return
*/
@GetMapping("/hello")
public String hello() {
return "hello world";
}
}
直接运行main 方法启动 就可以访问;
配置支持jsp
编写application.yml
spring: mvc: view: # 配置支持jsp prefix: /WEB-INF/jsp/ suffix: .jsp
创建存放jsp页面的webapp目录
并把它设置为web资源目录,
点开自己的项目配置 选择+ 号, 添加web
添加 对jsp的支持jar
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- servlet 依赖包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSTL (JSP standard Tag Library) JSP 标准标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
最后配置启动项的main 的工作目录
选择edit configruation..
编写controller 进行访问
@GetMapping("/hello")
public String hello() {
System.out.println("aaaaaaaaaaaaaaaaaaaaaa");
return "hello";
}
eclipse的配置方法连接:https://mp.csdn.net/postedit/89376652