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

时间:2013-07-29 21:59:53

标签: java spring jsp spring-mvc

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

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

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

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

3 个答案:

答案 0 :(得分:95)

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

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

答案 1 :(得分:12)

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

例如;

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

答案 2 :(得分:-4)

获取Spring MVC Controller中的QueryString

这是Liferay门户特定的解决方案,它可以工作。

查询字符串示例:?reportTypeId=1&reportSeqNo=391

为了在Liferay Portal中获取reportSeqNo的值,我们需要获取原始Servlet请求。

String reportSeq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest)).getParameter("reportSeqNo");