SpringBoot 它Spring里面所提供的一个框架,或者一些maven的集合,(提供很多starter)
我们通过它,可以快速的构建项目 编译和部署,监控;
我们正常跑一个springweb程序值需要添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@SpringBootApplication
public class APP {
public static void main(String[] args) {
SpringApplication.run(APP.class);
}
}
@RestController
public class AppController {
@RequestMapping("/hello")
public String index(){
return "dada";
}
}
网页显示dada
springboot跳转jsp需要引入springboot对jsp的支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- servlet 依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat 的支持. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
需要在resources配置application.properties
剩下的写法和上面一样
jsp的路径就是spring.mvc.view.prefix=/WEB-INF/add/这个
写完后运行会404这里就需要在idea里面配置一下
对于freemaker支持
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
需要配置application.properties下面的内容
# FreeeMarker 模板引擎配置
# 设定ftl文件路径
spring.freemarker.tempalte-loader-path=classpath:/templates
# 关闭缓存,及时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
对于json的支持
@SpringBootApplication
public class FreemarkerAPP {
public static void main(String[] args) {
SpringApplication.run(FreemarkerAPP.class);
}
}
@Controller
public class FreemarkerController {
@RequestMapping("/index")
public String index(Model model){
System.out.println("==========================");
model.addAttribute("name","来了老弟");
return "index";
}
}
在resources中写一个xxxx.ftl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello , ${name}</h1>
</body>
</html>