SpringBoot学习_crud引入资源

本文详细介绍了如何在SpringBoot项目中组织HTML、DAO、Entities及静态资源,并提供了两种方法将index设置为首页,同时讲解了如何通过Maven依赖引入webjars资源并在HTML中使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html放入到templates文件夹下
在这里插入图片描述
dao和entities
在这里插入图片描述
css,js等静态资源放到static文件夹下
在这里插入图片描述
把index定为首页的方法:
方法一:在Controller中添加方法定位到首页,{"/","/index.html"}代表多种请求

@RequestMapping({"/","/index.html"})
    public String index(){
        return "index";
    }

第二种方法:添加ViewController视图解析器
MyMvcConfig 继承 WebMvcConfigurer
重写addViewControllers方法

package com.nyh.springboot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author nyh
 * @create 2018-11-05  20:11
 **/
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }
}

引用webjars
先导入webjars
maven依赖到webjars官网去复制

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>4.1.3</version>
</dependency>

然后到html
用thymeleaf模板引擎的语法

<html lang="en" xmlns:th="http://www.thymeleaf.org">

然后通过th:href引进来
/代表当前项目下

<link th:href="@{/webjars/bootstrap/4.1.3/css/bootstrap.css}" rel="stylesheet">

里面的路径可以参照jar包中的文件夹路径
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值