springboot html引入js_Spring Boot整合 : SpringMVC端口和静态资源

SpringBoot是内置tomcat的,所以整合SpringMVC我们就看一下修改tomcat端口和静态资源的过程,首先tomcat咱们电脑上默认访问的端口号是8080,就尝试把端口号修改为80。

目标

可以修改tomcat的端口和访问项目中的静态资源

修改tomcat端口

#tomcat端口
server:
port: 80

查询**Properties,设置配置项(前缀+类变量名)到application配置文件中

访问静态页面

==springboot官方指出:不支持jsp。==

那么用上面作为模板渲染,官方提供了两种:freemaker和Thymeleaf

1:引入freemaker依赖的pom.xml

org.springframework.bootspring-boot-starter-freemarker

2:添加配置

# 激活开发环境profile
spring:
freemarker:
suffix: .html # 修改模板的后缀,默认后缀是:.ftl
template-loader-path: classpath:/templates/ # 指定页面存放的目录,注意此目录下面的页面不能直接访问,必须通过springmvc的去跳转和转发

3:定义login.html页面





content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

Document


登录页面




4:定义LoginController.java

package com.itheima.web;

import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class LoginController {


//http://localhost:8091/login
@GetMapping("/login")
public String login(){
return "login";
}

}

5:访问: http://localhost:8091/login 进入到登录页面。

完整结构:

77327ae23ed190e41a0b4bcf76dfdeb1.png

访问静态资源(图片 img,样式 css和脚本 js)

a:在resources目录下新建static目录, 然后分别 新建css / img/ js目录存放对应的文件: 如下:

73bfedbf26bcb05adc32ca02e8f31862.png

静态资源放置的位置;放置静态资源并访问这些资源在spring boot项目中静态资源可以放置在如下目录:

注意它们的优先级是:resources > static > public 逐步递减的。

并在在访问和加载资源文件的时候不需要在资源目录的名字。

3:在页面中去引入相关的静态资源文件:





content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

Document



登录页面








c5bf1184295d4428e6fdc7b75860a31c.png

4:浏览器访问http://localhost:8081/css/main.css 看效果即可。

5:当然如果你不想把静态资源文件放在static下,你可以自定义,那么就需要覆盖默认的配置,如下:

yml和properties处理静态资源的区别

a:如果是application.properties文件类型配置方式,静态资源处理如下:

# 静态资源的访问路径,默认就是/**
spring.mvc.static-path-pattern=/page/**
#如果你修改静态资源(css/js/images/font)的位置的时候,修改下面代码
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}

默认情况下,是不需要修改因为默认值就是上面的配置,暴露出来的目的:方便你去修改和调整。

b:如果是application.yml处理静态资源如下:

问题:

spring:
profiles:
active: dev
freemarker:
suffix: .html
template-loader-path: classpath:/templates/
mvc:
static-path-pattern: /**
resources:
static-locations: classpath:/resources/,classpath:/static/

0462cf11dd8751959cb88af834a5e0bf.png

如果都配置了 优先级分别为:1 > 2 > 3  

当然一般不建议大家去修改和调整,因为springboto本身就是默认大于配置。保持默认即可。

5539373607bca79c1a480109d8fbe93c.png

回复关键词

 JUC    分布式限流   消息队列     alibaba     JVM性能调优       Docker  

看更多精彩教程

9e300015b1b8cfd5080319263b194d9a.png

无需摇摆不定

快来点在看吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值