在项目中引用静态资源文件或者进行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}获取到了绝对路径。
结束

最新文章

  1. C#/net 使用Protocol Buffers入门
  2. C语言中结构体的位域(bit-fields)
  3. Centos 下oracle 11g 安装部署及手动建库过程
  4. active mq 配置延时
  5. yii2.0 gii
  6. 【C#学习笔记】播放wav文件
  7. 01_C语言基础
  8. 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集
  9. OpenCV入门教程
  10. WordPress中函数钩子hook的作用及基本用法
  11. 01-vagrant安装centos7
  12. 高斯混合模型和EM算法
  13. 二分算法C实现
  14. Netflix网关zuul(1.x和2.x)全解析
  15. webpack安装
  16. python数据结构与算法第三天【时间复杂度计算方法】
  17. poj2836 状态压缩dp
  18. MT【61】含参数二次函数最大最小值
  19. C/C++中#pragma once的使用
  20. error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”

热门文章

  1. AI - H2O - 安装与运行
  2. Django:ORM中ForeignKey外键关系分析
  3. 微信小程序常用控件汇总
  4. 理解Hybrid接口的应用
  5. Appium移动自动化测试-----(一)Appium介绍
  6. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
  7. myeclipse 相关问题
  8. 2019.10.16&17小结
  9. Python中遍历整个列表及注意点(参考书籍Python编程从入门到实践)
  10. Python yield 使用浅析【转】