SpringBoot简化Spring项目的创建、运行、调试、部署过程,它提供了Spring开发过程的依赖打包,并按照习惯大于约定解决了配置问题。默认使用tomcat服务器, 日志使用logback,构建支持maven与gradle,我们只需关注开发,而不用太关注xml配置。
下面我们一步一步开始搭建项目
1:File-new-Project创建springboot项目
2:创建webapp/WEB-INF/jsp/index.jsp 目录与文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>duck首页</h1>
${message}
<br><br>
取配置文件参数
${hello}
</body>
</html>
3:创建第一个Controller类
package com.duck.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
//会返回jsp界面
@Controller
//RestController是Controller 与 ResponseBody的合体,返回的是json字符串
//@RestController
public class DuckController {
private static final Logger logger = LoggerFactory.getLogger(DuckController.class);
//取配置文件配置的参数(如果配置文件没有配置appplication.hello 那就取冒号后的值)
@Value("${application.hello:配置是空取我}")
private String hello = "";
@RequestMapping("/index")
public String index(Map<String, Object> model){
logger.info("index方法");
model.put("hello",hello);
model.put("message","SpringBoot项目的首页");
//返回字符串,框架会默认去目录spring.mvc.view.prefix定义的文件夹下找(字符串+spring,mvc.view.suffix)的jsp文件
return "index";
}
}
4:设置application.properties访问资源路径
5:还需配置项目
file -> project structure
选择web
分别配置Deployment Descriptors和Web Resource Directories
6:pom设置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<!-- 父类,定义了所依赖的包,父类pom中用dependenceManagement定义的包,如果子工程想引用,必须用在子工程中定义,
可以不带版本号,用父类的dependence显示指定 -->
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.18.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.duck</groupId>
<artifactId>duck-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>duck-admin</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--test启动器,这是真正的启动类,@SpringBootApplication由此启动-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- servlet依赖包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- springboot支持jsp -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
7:启动类
package com.duck;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = {"com.duck"},exclude={})
public class DuckAdminApplication {
public static void main(String[] args) {
SpringApplication.run(DuckAdminApplication.class, args);
}
}
8:访问首页
SpringBoot启动三部曲:配置pom 配置启动类 添加Controller
SpringBoot访问jsp三部曲:添加pom依赖 Controller配置@Controller 配置application的jsp访问路径
SpringBoot默认支持freemarker,如果需要freemarker
(1)Controller添加方法
@RequestMapping("/freemarker")
public String freemarker(Map<String,Object> model){
logger.info("freemarker方法");
model.put("message","freemarker首页");
return "freemarker";
}
(2)application.properties去除jsp配置
#页面配置
#spring.mvc.view.prefix=/WEB-INF/jsp/
#spring.mvc.view.suffix=.jsp
#配置Controller参数
application.hello=Hello SpringBoot
(3)添加ftl模板 默认路径 /resources/templates/
(4)添加pom支持
<!-- 引入freeMarker的依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
(5)访问freemarker