springboot整合freemarker模板引擎后在页面获取basePath绝对路径

在项目中引用静态资源文件或者进行ajax请求时我们有时候会使用 ${basePath} ,其实这就是一种获取绝对路径的方式:
springboot整合freemarker模板引擎后在页面获取basePath绝对路径_第1张图片
springboot整合freemarker模板引擎后在页面获取basePath绝对路径_第2张图片
那么在springboot项目中要怎么配置才能使用 basePaht呢?

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

springboot整合freemarker模板引擎后在页面获取basePath绝对路径_第3张图片
代码:

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 {

    }
}

第二步:注册第一步中自定义的拦截器

springboot整合freemarker模板引擎后在页面获取basePath绝对路径_第4张图片
代码:

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("/**");
    }
}

到这配置就算完了,接下来到前端测试使用一下

第三步:测试

我这里使用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);
                }
            }
        )
    }

打开页面按F12键观察:
springboot整合freemarker模板引擎后在页面获取basePath绝对路径_第5张图片
可以看到我们已经通过${basePaht}获取到了绝对路径。
结束

你可能感兴趣的:(java)