복붙노트

[SPRING] Spring MVC에서 동적 URL로 리디렉션

SPRING

Spring MVC에서 동적 URL로 리디렉션

내 Spring MVC 애플리케이션을 동적 URL (사용자가 제출)로 리디렉션하려고한다. 그래서 내가 이와 같은 코드를 가지고 있다면,

@RequestMapping("/redirectToSite")
protected ModelAndView redirect(
    @RequestParam("redir_url") String redirectUrl,
    HttpServletRequest request, 
    HttpServletResponse response) 
{
    // redirect to redirectUrl here
    return ?
}

제출 된 URL로 리디렉션하려면 어떻게해야합니까? 예를 들어 http : // mySpringMvcApp / redirectToSite? redir_url = http : //www.google.com은 Google로 리디렉션해야합니다.

해결법

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

    1.이 시도:

    이 시도:

    @RequestMapping("/redirectToSite")
    protected String redirect(@RequestParam("redir_url") String redirectUrl) 
    {
        return "redirect:" + redirectUrl;
    }
    

    이것은 Spring Reference Documentation의 16.5.3.2 Redirect : prefix에서 설명한다. 물론 수동으로이 작업을 수동으로 수행 할 수도 있습니다.

    response.sendRedirect(redirectUrl);
    
  2. ==============================

    2.

    @RequestMapping(value="/redirect",method=RequestMethod.GET)
    void homeController(HttpServletResponse http){
      try {
        http.sendRedirect("Your url here!");
      } catch (IOException ex) {
    
      }
    }
    
  3. from https://stackoverflow.com/questions/9311940/redirect-to-dynamic-url-in-spring-mvc by cc-by-sa and MIT license