如何创建一个父级SpringBoot工程
链接:https://blog.csdn.net/weixin_45528987/article/details/105302869
如何在window下安装SpringCloud alibaba的nacos中心
链接:https://blog.csdn.net/weixin_45528987/article/details/105303100
项目如何注册到nacos中心
链接:https://blog.csdn.net/weixin_45528987/article/details/105303245
第一步:创建一个Maven项目
第二步:添加需要的pom依赖
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 前端模板 thymeleaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<!--自定义工具包-->
<dependency>
<groupId>com.zxf</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
第三步:创建静态目录和首页html目录,并添加文件
第四步:新建application.yml配置文件,编写配置信息
server:
port: 9000
spring:
application:
name: home
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
thymeleaf:
prefix: classpath:templates/
suffix: .html
mode: HTML
encoding: UTF-8
servlet:
content-type: text/html
cache: false
第五步:创建SpringBoot启动类,首页访问Controller类
package com.zxf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class HomeApplication {
public static void main(String[] args) {
SpringApplication.run(HomeApplication.class, args);
}
}
@Controller
public class IndexController {
@GetMapping("index")
public String index(){
return "index";
}
}
第六步:MVC静态资源路径映射配置
第一种:配置文件配置
mvc:
static-path-pattern: /static/**
resources:
static-locations: classpath:/static/
第二种:配置类配置
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 静态资源加载配置 没有此配置静态资源将会被视为请求向后端发送GET请求
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}
访问成功