After springboot integration freemarker template engine basePath get the absolute path of the page

When referring to a static resource files in the project or be ajax requests we sometimes use $ {basePath}, in fact, this is the way to obtain an absolute path:


Then springboot how to configure the project to use basePaht it?

The first step: custom interceptor (achieve HandlerInterceptor)

Code:

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 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
Step: Step registered custom interceptor device

Code:

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: the Create by SLM
* @version: v1.0
* @description: com.slm.tools.project.config
* @date: 2019/4/9
* /
@Configuration
public class PathInterceptorConfig the extends WebMvcConfigurerAdapter {
@ override
public void addInterceptors (InterceptorRegistry Registry) {
registry.addInterceptor (new new PathInterceptor ()) addPathPatterns ( "/ **");.
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
to this configuration even finished, then use it to test the front end

Step Three: Testing
I tested here using 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键观察:

We can see that we have to get the absolute path by $ {basePaht}.
End

Guess you like

Origin www.cnblogs.com/exmyth/p/11324824.html