在项目中引用静态资源文件或者进行ajax请求时我们有时候会使用 ${basePath} ,其实这就是一种获取绝对路径的方式:

那么在springboot项目中要怎么配置才能使用 basePaht呢?

第一步:自定义拦截器(实现 HandlerInterceptor )

代码:

package com.slm.tools.project.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author: create by slm
* @version: v1.0
* @description: com.slm.tools.project.config
* @date:2019/4/9
*/
public class PathInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
String path = httpServletRequest.getContextPath();
String scheme = httpServletRequest.getScheme();
String serverName = httpServletRequest.getServerName();
int port = httpServletRequest.getServerPort();
String basePath = scheme + "://" + serverName + ":" + port + path;
httpServletRequest.setAttribute("basePath", basePath);
return true;
}

@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

}

@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
第二步:注册第一步中自定义的拦截器

代码:

package com.slm.tools.project.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
* @author: create by slm
* @version: v1.0
* @description: com.slm.tools.project.config
* @date:2019/4/9
*/
@Configuration
public class PathInterceptorConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new PathInterceptor()).addPathPatterns("/**");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
到这配置就算完了,接下来到前端测试使用一下

第三步:测试
我这里使用ajax进行测试:

function encrypt() {
var txt =$("#txt").val();
$.ajax({
url:"${basePath}/md5/encrypt",
dataType:"json",
data:{
txt:txt
},
success:function (data) {
$("#16MD5").html(data.MD516);
$("#16MD5Upper").html(data.MD516Upper);
$("#32MD5").html(data.MD532);
$("#32MD5Upper").html(data.MD532Upper);
}
}
)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
打开页面按F12键观察:

可以看到我们已经通过${basePaht}获取到了绝对路径。
结束

springboot整合freemarker模板引擎后在页面获取basePath绝对路径的更多相关文章

  1. Spring Boot 2.0 整合 FreeMarker 模板引擎

    本篇博文将和大家一起使用Spring Boot 2.0 和FreeMarker 模板引擎整合实战. 1. 创建新的项目 2. 填写项目配置信息 3. 勾选web 模块 4. 勾选freemarker模 ...

  2. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  3. SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

    在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...

  4. 「快学springboot」SpringBoot整合freeMark模板引擎

    前言 虽然现在流行前后端分离开发和部署,但是有时候还是需要用到服务端渲染页面的.比如:需要考虑到SEO优化等问题的时候,FreeMark其实还是很有作用的.本人的博客本来是用React开发的,但是后来 ...

  5. Spring MVC 学习总结(七)——FreeMarker模板引擎与动态页面静态化

    模板引擎可以让程序实现界面与数据分离,业务代码与逻辑代码的分离,这就提升了开发效率,良好的设计也使得代码复用变得更加容易.一般的模板引擎都包含一个模板解析器和一套标记语言,好的模板引擎有简洁的语法规则 ...

  6. springboot整合thymleaf模板引擎

    thymeleaf作为springboot官方推荐使用的模板引擎,简单易上手,功能强大,thymeleaf的功能和jsp有许多相似之处,两者都属于服务器端渲染技术,但thymeleaf比jsp的功能更 ...

  7. springboot整合Thymeleaf模板引擎

    引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...

  8. SpringBoot整合freemarker模板

    一.目录展示 二.导入依赖 三.application.properties配置文件 四.在src/main/resource/templates文件夹中创建HelloFreeMarker.ftl文件 ...

  9. freemarker模板引擎的使用

    freemarker是一套前端模板引擎,在使用时,要先在web项目中添加freemarker.jar的依赖. 我在这里主要演示spring-mvc整合freemarker模板引擎.项目案例的文件包结构 ...

随机推荐

  1. 【Leetcode_easy】985. Sum of Even Numbers After Queries

    problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...

  2. 前端研究CSS之文字与特殊符号元素结合的浏览器兼容性总结

    页面布局里总是会有类似 “文字 | 文字” 的设计样式,不同的浏览器存在严重偏差. 有兼容问题就要解决,下面总结了3种解决方案,分享给大家: 一.系统默认的样式 1.元素换行的段落 <div c ...

  3. java四大名著及idea常用插件

    四大名著 <Java编程思想><Think in Java> https://github.com/quanke/think-in-java https://github.co ...

  4. 14、vue-pdf的使用

    安装 npm install --save vue-pdf vue-pdf默认只显示第一页,可以写按钮翻页,也可以v-for多页显示 项目结构 实例一 按钮分页 <template> &l ...

  5. Docker快速安装

    目前装Docker得最简单方式就是脚本安装了,方法如下: curl -fsSL https://get.docker.com -o get-docker.sh sh get-docker.sh 安装后 ...

  6. ffmpeg.编译(20191129)

    1.一步步实现windows版ijkplayer系列文章之一——Windows10平台编译ffmpeg 4.0.2,生成ffplay - HarlanC - 博客园.html(https://www. ...

  7. [转载]SQL Server提权系列

    本文原文地址:https://www.cnblogs.com/wintrysec/p/10875232.html 一.利用xp_cmdshell提权 xp_cmdshell默认是关闭的,可以通过下面的 ...

  8. [.Net] 一句话Linq(递归查询)

    功能查询起止日期范围内连续的月份列表. /* Period */ cbxPeriod.DataSource = Enumerable.Range(, ).Select(t => DateTime ...

  9. CodeForces-1159B-Expansion coefficient of the array

    B. Expansion coefficient of the array time limit per test:1 second memory limit per test:256 megabyt ...

  10. Spring AOP日志实现(三)--获取访问者用户名

    通过Security获取访问者用户名: 也可以通过session来获取: 整体思路: