1.新建springBoot项目
(1)
(2)
(3)
(4)
(5)
(6)
2.配置项目JDK(前面已经配置的可以不用配置)
(1)
(2)
3.把项目配置成maven项目
(1)
(2)
(3)
(4)
(5)
(6)(有maven项目这里结构有test)
4.访问json
配置数据库在默认生成的application.properties文件里配置(我启动报错,加上?serverTimezone=UTC就不报错了)
spring.datasource.url=jdbc:mysql://localhost:3306/linsa?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
HelloController.java @RestController相当于SSM框架里@Controller加上@ResponseBody(注意返回类型是不是页面)
package com.example.demo.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//@RequestMapping("/")
@RestController
public class HelloController {
@RequestMapping("/index")
public String index() {
return "hello";
}
}
访问结果如下图
5.配置html
在pom.xml里加jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在application.properties文件里配置加html配置
# 定位模板的目录
spring.mvc.view.prefix=/
# 给返回的页面添加后缀名
spring.mvc.view.suffix=.html
配置路径为static文件下的html(static下加test.html)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<h1>测试默认html</h1>
</body>
</html>
TestController.java(注意返回页面用@Controller注解,不能用@RestController)
package com.example.demo.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("test")
public String test(){
return "test";
}
}
显示页面如下
注意:在application.properties文件里配置加html配置一般如下
# 定位模板的目录
spring.mvc.view.prefix=classpath:/templates/
# 给返回的页面添加后缀名
spring.mvc.view.suffix=.html
配置路径为templates文件下的html(templates下加test.html)
test.css
body{
color:red;
}
test.html
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="/css/test.css"/>
<title>Test</title>
</head>
<body>
<h1>测试SpringBoot的后台返回html</h1>
</body>
</html>
结果如下图所示:
若得不到想要的结果,刷新maven并重新编译项目
maven刷新
重新编译项目
6.配置jsp
在pom.xml里加jar包
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
在application.properties文件里配置加jsp配置
# 定位模板的目录
#spring.mvc.view.prefix=classpath:/templates/
# 给返回的页面添加后缀名
#spring.mvc.view.suffix=.html
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
配置路径为WEB-INF/jsp文件下的jsp(jsp下加test.jsp)
<%--
Created by IntelliJ IDEA.
User: gaomin
Date: 2018/11/14
Time: 16:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>测试SpringBoot的后台返回jsp</h1>
</body>
</html>
TestController.java(注意返回页面用@Controller注解,不能用@RestController)
package com.example.demo.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("test")
public String test(){
return "test";
}
}
结果如下图所示:
若得不到想要的结果,刷新maven并重新编译项目