项目目录
一、创建项目
(1)连个都勾选
(2)项目打包成web,因为要应用jsp
(3) 项目报错,修正:原因,因为创建了web工程,缺少文件。手动添加,如图:
二、添加项目依赖 pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot 核心组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 部署tomcat,排除冲突-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
三’、添加application.properties配置文件
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
四、创建controller类
@Controller
public class HelloController {
@RequestMapping("/hello")
public String index(Model m) {
m.addAttribute("now",DateFormat.getDateTimeInstance().format(new Date()));//将数据存放到model中
return "hello";//视图重定向hello.jsp
}
}
关键代码说明:
@Controller:指定只是一个controller类,请求前端连接处理的类标识。
五、创建启动类
package com.springboot.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@ComponentScan("com.springboot.controller")
//@EnableAutoConfiguration
@SpringBootApplication(scanBasePackages = "com.springboot")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
关键代码说明:
@SpringBootApplication(scanBasePackages = "com.springboot")
springboot项目启动类,@SpringBootApplication
是一个复合注解,包括@ComponentScan
,和@SpringBootConfiguration
,@EnableAutoConfiguration
。