在弹簧 MVC 控制器中获取查询字符串值

2022-09-01 16:20:04

我有一个这样的引荐来源网址:

http://myUrl.com?page=thisPage&gotoUrl=https://yahoo.com?gotoPage

如何在我的弹簧控制器中获取“page”和“gotoUrl”的值?

我想将这些值存储为变量,以便以后可以重用它们。


答案 1

在SpringMVC中,您可以指定查询字符串中的值,并将其作为方法参数与@RequestParam注释一起传递。

public ModelAndView getPage(
    @RequestParam(value="page", required=false) String page, 
    @RequestParam(value="gotoUrl", required = false) String gotoUrl) {
}

答案 2

您可以从 HttpServletRequest 接口使用 getParameter() 方法。

例如;

  public void getMeThoseParams(HttpServletRequest request){
    String page = request.getParameter("page");
    String goToURL = request.getParameter("gotoUrl");
}