zoukankan      html  css  js  c++  java
  • springboot整合freemarker模板引擎后在页面获取basePath绝对路径

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

  • 相关阅读:
    ipvsadm命令介绍
    转载-lvs-dr模式+keepalived双机
    Codeforces Round #426 (Div. 2)
    Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
    Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
    Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
    Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
    Codeforces Round #425 (Div. 2)
    Codeforces Round #425 (Div. 2)
    Codeforces Round #425 (Div. 2)
  • 原文地址:https://www.cnblogs.com/exmyth/p/11324824.html
Copyright © 2011-2022 走看看