在Spring MVC Controller中获取查询字符串值

Jak*_*ake 23 java spring jsp spring-mvc

我有一个像这样的推荐人网址:

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

如何在Spring Controller中获取"page"和"gotoUrl"的值?

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

Aff*_*ffe 96

在SpringMVC中,您可以使用@RequestParam注释指定查询字符串中的值,并将其作为方法参数传入.

public ModelAndView getPage(
    @RequestParam(value="page", required=false) String page, 
    @RequestParam(value="gotoUrl", required = false) String gotoUrl) {
}
Run Code Online (Sandbox Code Playgroud)


Rau*_*wal 12

您可以使用HttpServletRequest接口中的getParameter()方法.

例如;

  public void getMeThoseParams(HttpServletRequest request){
    String page = request.getParameter("page");
    String goToURL = request.getParameter("gotoUrl");
}
Run Code Online (Sandbox Code Playgroud)

  • 虽然这个答案在技术上是正确的,但不是推荐使用Spring的方法,@ Affe的答案更重要. (12认同)