一、创建项目:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-znnjb5ma-1662204782889)(new.png)]
或者参考下图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QeUkKIEw-1662204782891)(new2.png)]
给项目引入jar:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ikRtCd6w-1662204782892)(jar.png)]
二、编写自定义控制器:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BQj0Uwna-1662204782892)(controller.png)]
三、application.yml:
在项目的src/main/resources下新建application.yml,设置如下内容:
# 强制response和request对象采用UTF8编码
server:
servlet:
encoding:
charset: UTF-8
enabled: true
force: true
port: 80 #设置项目访问端口
# spring设置
spring:
mvc:
view:
prefix: /static/ # 响应给浏览器的网页的路径 #/WEB-INF/ #如果是static的话可以不用配置prefix和suffix
suffix: .html # 响应给浏览器的网页的后缀 # .jsp #如果是static的话可以不用配置prefix和suffix
static-path-pattern: /static/** #URL响应地址
web:
resources:
static-locations: classpath:/static/ #配置静态文件位置
devtools:
restart:
enabled: true #开启热部署
additional-paths: src/main/java #src/main/java下的代码修改后会自动重启
exclude: static/** # static下的内容修改后不需要重启
freemarker:
cache: false # 页面不加载缓存(不采用浏览器缓存),修改即使生效
四、测试访问:
http://localhost/zyq/getData
五、hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>static文件夹内的网页可以直接用浏览器访问,templates文件夹中的网页需要用Controller转发访问</div>
Hello Page By zhaoYQ
</body>
</html>
六、控制器添加访问网页的方法:
@RequestMapping("/toHello")
public String toHelloPage(){
return "hello";
}
七、项目添加热部署配置:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VKN46GTp-1662204782893)(hotdev.png)]
yml中配置:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oEnOwIr6-1662204782894)(hotdev_yml.png)]
八、访问网页:
测试地址: http://localhost/zyq/toHello
九、项目结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O3abG9ws-1662204782895)(structure.png)]
九、完整代码:
9.1.applicationContext.yaml
server:
servlet:
encoding:
charset: utf-8
enabled: true
force: true
port: 80
spring:
mvc:
view:
prefix: /static/ #如果是static的话可以不用配置mvc.view.prefix和mvc.view.suffix
suffix: .html #如果是static的话可以不用配置mvc.view.prefix和mvc.view.suffix
web:
resources:
static-locations: classpath:/static/ #配置静态文件位置
thymeleaf:
cache: false
devtools:
restart:
enabled: true #设置开启热部署
additional-paths: src/main/java #重启目录
exclude: WEB-INF/**
freemarker:
cache: false #页面不加载缓存,修改即时生效
9.2.pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tedu.springBootDemo</groupId>
<artifactId>day29springdemo02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>day29springdemo02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
9.3.启动类:
package com.tedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Day29springdemo02Application {
public static void main(String[] args) {
SpringApplication.run(Day29springdemo02Application.class, args);
}
}
9.4.自定义控制器:
package com.tedu.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//http://localhost/zyq/toHello
//@RestController//@RestController注解相当于@ResponseBody + @Controller合在一起的作用
@Controller
@RequestMapping("/zyq")
public class TestController {
@RequestMapping("/toHello")
public String execute(){
return "hello";
}
@GetMapping("/getData")
@ResponseBody
public String getString(){
return "hello";
}
}
//知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
// 1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,
// 配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
// 2) 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
// 如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
//springBoot项目设置热部署:
//https://blog.csdn.net/chachapaofan/article/details/88697452
//要让本项目在target下生成一个用于部署的jar包或war包的话,可以打开视图view--工具窗口tool view--终端窗口terminal:
//输入清空target目录的命令: mvn clear
//在target目录生成jar的命令: mvn install
//也可以在idea的maven窗口中双击打开当前项目,然后展开Lifecycle,用clean清除targer中的内容
//用install重新把项目在target中打成jar包或war包
9.5.网页hello.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello static HTMLPage
</body>
</html>
赵永齐 2022-09-03