PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2021-05-25 17:26:31  修改时间:2024-11-20 05:20:12  分类:06.Java框架/系统  编辑

首先,借助工具类:【Spring Boot】获取Request/Response的工具类:RequestContextHolderUtil,获取到Request对象

package com.wenjianbao.util;

import javax.servlet.http.HttpServletRequest;

/**
 * 页面URL工具类
 */
public class PageURLUtil {
    /**
     * 获取当前页面URL
     */
    public static String getPageUrl() {
        HttpServletRequest request = RequestContextUtil.getRequest();

        return request.getScheme() + "://"
            + request.getServerName()
            + (request.getServerPort() == 80 ? "" : ":" + request.getServerPort())
            + request.getContextPath()
            + request.getRequestURI()
            + (request.getQueryString().equals("") ? "" : "?" + request.getQueryString());
    }
}

测试:

@RequestMapping("blog")
@Controller
public class BlogController {
    @GetMapping("list")
    @ResponseBody
    public String blogList() {
        String pageUrl = PageURLUtil.getPageUrl();
        System.out.println(pageUrl);
    }
}

输出:

http://localhost:8001/blog/list?pageNum=2&pageSize=3

 

附:Hutool】URL生成器-UrlBuilder