복붙노트

[SPRING] Spring MVC 컨트롤러에서 쿼리 문자열 값 가져 오기

SPRING

Spring MVC 컨트롤러에서 쿼리 문자열 값 가져 오기

다음과 같은 리퍼러 URL이 있습니다.

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

Spring Controller에서 "page"와 "gotoUrl"값을 얻으려면 어떻게해야합니까?

이 값을 변수로 저장하여 나중에 다시 사용할 수 있습니다.

해결법

  1. ==============================

    1.HttpServletRequest 인터페이스의 getParameter () 메소드를 사용할 수 있습니다.

    HttpServletRequest 인터페이스의 getParameter () 메소드를 사용할 수 있습니다.

    예를 들어;

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

    2.SpringMVC에서 @RequestParam 주석으로 메소드 문자열을 파싱하고 쿼리 문자열에서 값을 지정할 수 있습니다.

    SpringMVC에서 @RequestParam 주석으로 메소드 문자열을 파싱하고 쿼리 문자열에서 값을 지정할 수 있습니다.

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

    3.Spring MVC 컨트롤러에서 QueryString 가져 오기

    Spring MVC 컨트롤러에서 QueryString 가져 오기

    이것은 Liferay 포털 특정 솔루션이며 작동합니다.

    검색어 문자열 예 :? reportTypeId = 1 & reportSeqNo = 391

    Liferay Portal에서 reportSeqNo 값을 가져 오려면 원본 서블릿 요청을 받아야합니다.

    String reportSeq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest)).getParameter("reportSeqNo");
    
  4. from https://stackoverflow.com/questions/17934972/get-query-string-values-in-spring-mvc-controller by cc-by-sa and MIT license