SpringMVC中Controller中如何获取当前访问地址的url信息呢?

Java-框架王 SpringMVC 发布时间:2021-07-12 10:19:32 阅读数:2282 1
实现思路:
       只需借助request对象的相关信息,即可返回相应的地址信息

例:
 

public class UserController {

	@Autowired
	private HttpServletRequest request;

	/**
	 * 用户返回用户注册
	 *
	 */
	@RequestMapping(value = "/userReg", method = RequestMethod.GET)
	public String userReg() {

		String url = "";
        url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + request.getContextPath() + request.getServletPath();

        if (request.getQueryString() != null) {
            url += "?" + request.getQueryString();
        } 
		System.out.println(url);
		return "userReg";
	}

}
在URL运行相应的地址信息:
      http://localhost:8080/com.java265.helloworld/userReg
在控制台中会打印出相应的信息:
   http://localhost:8080/com.java265.helloworld/userReg
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringMVC/202107/471.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者