SpringMVC中Controller中如何获取当前访问地址的url信息呢?
实现思路:
只需借助request对象的相关信息,即可返回相应的地址信息
例:
http://localhost:8080/com.java265.helloworld/userReg
在控制台中会打印出相应的信息:
http://localhost:8080/com.java265.helloworld/userReg
只需借助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
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


