当前位置: 首页 > news >正文

springboot访问静态页面,并加载css、js样式

1、添加thyme leaf模板依赖

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

2、将静态资源分别放入spring boot相应目录下,一般static放资源,templates放html页面

3、添加controller,使页面可以在服务启动后通过地址栏访问,

        这里注意:下面任意一项不对都会运行异常

                1、一定要用@Controller注解,不能用@Restcontroller;

                2、因为是通过地址栏访问,所以使用@GetMapping注解,注解后的内容可随意写,访问时的地址就是:“http://localhost:8080/自己写的GetMapping后的内容”

                3、return“ ”里面填的字符串一定要是自己放入templates里的html页面的名字。


@Controller
public class LoginController {

//  跳转登录页面
    @GetMapping("/index")
    public String login ()  {
//        response.sendRedirect("index");
//        response.sendRedirect("index.html");
//        return "../resources/templates/index.html";

          return "index";
    }

4、添加配置类,加载css和js样式,其他方法的css,js引入可参考

package com.example.springboot.config;


import org.aopalliance.intercept.Interceptor;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class WebConfig extends WebMvcAutoConfiguration implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry){
//        registry.addInterceptor( new LoginInterceptor()).addPathPatterns("/adimin/**");


    }

    //加载静态页面的css和js文件
    //参考链接:https://blog.51cto.com/u_15236724/5369362
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }

}

 5、在html页面修改引入的静态资源的相对路径:

虽然href或src内的内容idea会标识不认识,但地址是没有问题。

在idea里

src="/js/jquery.min.js"        也就是地址        src="/static/js/jquery.min.js"

如果加了/static反而无法加载资源。

需要更多的样式加载方式可参考

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册登录界面Ⅱ</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/css/login.css">
    <!-- login1.css 渐变纯色背景
    <link rel="stlesheet" href="./css/login1.css"> -->
    <!-- prefixfree 省去浏览器适应前缀 -->

<script src="/js/prefixfree.min.js"></script>
</head>
<body>
<div class="container">
  <div class="welcome">
    <div class="pinkbox">
      <!-- 注册 -->
      <div class="signup nodisplay">
        <h1>Register</h1>
        <form id="regi" autocomplete="off">
          <input type="text" placeholder="用户名" v-model="username">
          <input type="email" placeholder="手机号" v-model="phoneNum">
          <input type="email" placeholder="邮箱" v-model="email">
          <input type="password" placeholder="密码"  v-model="password">
          <input type="password" placeholder="确认密码" v-model="confirmPassword">
          <button type="submit" class="button register" @click="signup()">注册</button>
        </form>
      </div>

      <!-- 登录 -->
      <div class="signin">
        <h1>Sign In</h1>
        <form id="login_inf" class="more-padding" autocomplete="off">
          <input type="text" placeholder="手机号或邮箱"  v-model="phoneNum" id="phoneNum">
          <input type="password" placeholder="密码"  v-model="password" id="password">
          
          <div class="identity">
            <input type="text" placeholder="验证码" v-model="identityNum" id="identityNum">
            <button type="button" class=" button">sent</button>
          </div>
          
          <div class="checkbox">
            <input type="checkbox" id="remember" /><label for="remember">记住密码</label>
          </div>
          <button type="submit" class="buttom sumbit" @click="login()">登录</button>
        </form>
      </div>
    </div>

    <div class="leftbox">
      <h2 class="title"><span>BLOOM</span>&<br>BOUQUET</h2>
      <p class="desc">Pick your perfect <span>bouquet</span></p>
      <img class="flower smaller" src="/img/login1.png" alt="picture" />
      <p class="account">Have an account?</p>
      <button class="button" id="signin">Login</button>
    </div>

    <div class="rightbox">
      <h2 class="title"><span>BLOOM</span>&<br>BOUQUET</h2>
      <p class="desc">Pick your perfect <span>bouquet</span></p>
      <img class="flower" src="/img/login2.png"  alt="picture"/>
      <p class="account">Don't have an account?</p>
      <button class="button" id="signup">Sign Up</button>
    </div>
  </div>
</div>   

<!-- partial -->
  <script src="/js/vue.js" type="text/javascript"></script>
  <script src="/js/jquery.min.js" type="text/javascript"></script>
  <script src="/js/axios.min.js" type="text/javascript"></script>
  <script src="/js/login-script.js" type="text/javascript"></script>  <!--样式动态js引入-->

</body>
</html>

6、启动服务,浏览器访问:http://localhost:8080/index

相关文章:

  • 35【源码】数据可视化:基于 Echarts + Python 动态实时大屏 - 门店销售业绩数据中心
  • 如何给《羊了个羊》配置一套智能客服系统?
  • CSS的背景——颜色,图片,背景图片位置,
  • 内网渗透之CFS三层靶机搭建
  • Mediacodec 如何硬件解码到纹理的
  • PDF格式分析(七十五)——线型注释(Line)
  • 医学视VQA——综述
  • Lambda表达式实现方式、标准格式、练习、省略模式、注意事项及和匿名内部类的区别
  • java毕业生设计医患辅助系统计算机源码+系统+mysql+调试部署+lw
  • CAD如何绘制六连环图案?CAD使用圆,椭圆,直线综合练习
  • 二叉树及其应用(增删改查)
  • 分库分表二:ShardingJDBC进阶实战案例上
  • ClickHouse(06)ClickHouse的数据表创建语句详细解析
  • 银纳米团簇-荧光Ag25团簇以及衍生团簇(直径1-2nm)
  • Jmeter-Windows环境配置
  • 分享一款快速APP功能测试工具
  • C# 免费离线人脸识别 2.0 Demo
  • JWT究竟是什么呢?
  • Objective-C 中关联引用的概念
  • Rancher如何对接Ceph-RBD块存储
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 记一次用 NodeJs 实现模拟登录的思路
  • 简单实现一个textarea自适应高度
  • 面试遇到的一些题
  • 你真的知道 == 和 equals 的区别吗?
  • 前端相关框架总和
  • 我是如何设计 Upload 上传组件的
  • Java总结 - String - 这篇请使劲喷我
  • ​Linux·i2c驱动架构​
  • (12)Hive调优——count distinct去重优化
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (转)es进行聚合操作时提示Fielddata is disabled on text fields by default
  • (转)关于pipe()的详细解析
  • .net 4.0发布后不能正常显示图片问题
  • .NET C# 使用 SetWindowsHookEx 监听鼠标或键盘消息以及此方法的坑
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .NET Framework .NET Core与 .NET 的区别
  • .net on S60 ---- Net60 1.1发布 支持VS2008以及新的特性
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • .NET 发展历程
  • .NET4.0并行计算技术基础(1)
  • .net分布式压力测试工具(Beetle.DT)
  • .NET基础篇——反射的奥妙
  • .one4-V-XXXXXXXX勒索病毒数据怎么处理|数据解密恢复
  • @RequestBody详解:用于获取请求体中的Json格式参数
  • []T 还是 []*T, 这是一个问题
  • [52PJ] Java面向对象笔记(转自52 1510988116)
  • [EFI]ASUS EX-B365M-V5 Gold G5400 CPU电脑 Hackintosh 黑苹果引导文件
  • [Hibernate] - Fetching strategies
  • [Hive] INSERT OVERWRITE DIRECTORY要注意的问题