1. Cree un proyecto de springboot
Heredar spring-boot-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>1.5.19.RELEASE</version>
</parent>
Agregar componentes web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Escriba el archivo application.properties o application.yml en recursos
Configurar y escribir application.yml
server:
port: 8081 # 配置端口
context-path: /xiaodu #配置根路径
Escribir código java
Comenzar la clase
/**
* springboot 启动类
* 这个类最好放在最外层包下,其他的java类放到他的包下
* 启动会扫描本包和子包下的类
*/
@SpringBootApplication
public class Springboot_Web_StartApp_8081 {
public static void main(String[] args) {
SpringApplication.run(Springboot_Web_StartApp_8081.class,args);
}
}
Escribe el controlador
/**
* @RestController 是@Controller ,@ResponseBody一起使用的作用
*/
@RestController
public class MyHelloController {
/**
* getMaping == @RequestMapping(method = {RequestMethod.GET})
* @return
*/
@GetMapping("/hello")
public String hello() {
return "hello world";
}
}
Se puede acceder ejecutando directamente el método principal para comenzar;
Soporte de configuración jsp
Escriba application.yml
spring: mvc: view: # 配置支持jsp prefix: /WEB-INF/jsp/ suffix: .jsp
Cree un directorio de aplicaciones web para almacenar páginas jsp
Y configúrelo como un directorio de recursos web,
Haga clic en la configuración de su propio proyecto, seleccione + firmar, agregar web
Agregar jar de soporte para jsp
<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>
Finalmente configure el directorio de trabajo del principal del elemento de inicio
Seleccione editar configuración.
Escriba el controlador para acceder
@GetMapping("/hello")
public String hello() {
System.out.println("aaaaaaaaaaaaaaaaaaaaaa");
return "hello";
}
Conexión del método de configuración de eclipse: https://mp.csdn.net/postedit/89376652