Springboot入门 搭建(Springboot+tomcat+html)项目

一、Springboot项目创建

1、点击 File > New > Project

在这里插入图片描述

2、选择Spring lnitializr

选择maven,选择jdk,根据自己需求选择打包方式,然后点击next在这里插入图片描述

3、选择需要得jre包

SpringbootDevtools、SpringWeb
![在这里插入图片描述](https://img-blog.csdnimg.cn/1ee2fea98ff149d7bcf6f0d9a44003cb.png

4、修改pom.xml文件

<!-- thymeleaf -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

二、配置yml文件

1、可以右键把properties修改成yml,或者直接删除添加yml文件

在这里插入图片描述

2、配置 thymeleaf、静态文件

spring:
  #配置视图文件
  thymeleaf:
    mode: HTML
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    encoding: UTF-8
    servlet:
      content-type: text/html
  #配置静态文件
  mvc:
    static-path-pattern: /static/**

三、添加路径访问工具类

1、添加一个Controller文件夹,创建一个RoutingPage文件


package com.example.demo.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 配置路由
 */
@Controller
public class RoutingPage {
    /**
     * 首页访问
     * @return
     */
    @RequestMapping(value = {"/","/index"})
    public ModelAndView main(){
        ModelAndView index = new ModelAndView("index");
        return index;
    }

    /**
     * 单路径
     * @param name 文件名
     * @return
     */
    @RequestMapping( value = "/{name}")
    public ModelAndView router(@PathVariable String name){
        return new ModelAndView(name);
    }

    /**
     * 双路径
     * @param fist 文件夹
     * @param second 文件名
     * @param m
     * @return
     */
    @RequestMapping(value = "/{fist}/{second}")
    public ModelAndView route2(@PathVariable String fist, @PathVariable String second, @RequestParam Map m){
        final String view = fist+"/"+second;
        return new ModelAndView(view);
    }
}

四、添加tomcat

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、在templates文件夹中添加一个index.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>这是Index页面</h1>
</body>
</html>

六、启动项目是遇到得错误。

在这里插入图片描述

按我自己得理解应该是springboot版本不兼容,所以报错了。
最后我是把springboot版本改成了2.7.5再启动就可以了
在这里插入图片描述
今天知识就分享到这了,希望能帮助到您!!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值