복붙노트

[SPRING] spring-mvc에서 페이지를 리디렉션하는 매개 변수를 전달하는 방법

SPRING

spring-mvc에서 페이지를 리디렉션하는 매개 변수를 전달하는 방법

나는 다음과 같은 컨트롤러를 썼다.

@RequestMapping(value="/logOut", method = RequestMethod.GET )
    public String logOut(Model model, RedirectAttributes redirectAttributes)  {
        redirectAttributes.addFlashAttribute("message", "success logout");
        System.out.println("/logOut");
        return "redirect:home.jsp";
    }

home.jsp 페이지에서이 코드를 변경하는 방법 $ {message}를 작성하고 "success logout"을 볼 수 있습니다.

해결법

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

    1.반환 값에 redirect : 접두사가 포함되면 viewResolver는 이것을 리디렉션이 필요하다는 특별한 표시로 인식합니다. 나머지보기 이름은 리디렉션 URL로 처리됩니다. 클라이언트는이 리디렉션 URL에 새 요청을 보냅니다. 따라서 리디렉션 요청을 처리하려면 처리기 메서드를이 URL에 매핑해야합니다.

    반환 값에 redirect : 접두사가 포함되면 viewResolver는 이것을 리디렉션이 필요하다는 특별한 표시로 인식합니다. 나머지보기 이름은 리디렉션 URL로 처리됩니다. 클라이언트는이 리디렉션 URL에 새 요청을 보냅니다. 따라서 리디렉션 요청을 처리하려면 처리기 메서드를이 URL에 매핑해야합니다.

    리디렉션 요청을 처리하기 위해 이와 같은 핸들러 메소드를 작성할 수 있습니다.

    @RequestMapping(value="/home", method = RequestMethod.GET )
    public String showHomePage()  {
        return "home";
    }
    

    그리고 다음과 같이 logOut 핸들러 메소드를 다시 작성할 수 있습니다.

    @RequestMapping(value="/logOut", method = RequestMethod.POST )
    public String logOut(Model model, RedirectAttributes redirectAttributes)  {
        redirectAttributes.addFlashAttribute("message", "success logout");
        System.out.println("/logOut");
        return "redirect:/home";
    }
    

    편집하다:

    응용 프로그램 구성 파일에이 항목을 사용하여 showHomePage 메소드를 피할 수 있습니다.

    <beans xmlns:mvc="http://www.springframework.org/schema/mvc"
     .....
     xsi:schemaLocation="...
     http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
     ....>
    
    <mvc:view-controller path="/home" view-name="home" />
     ....
    </beans>
    

    / home에 대한 요청을 home이라는보기로 전달합니다. 뷰가 응답을 생성하기 전에 실행할 Java 컨트롤러 로직이없는 경우이 방법이 적합합니다.

  2. from https://stackoverflow.com/questions/19249049/how-to-pass-parameters-to-redirect-page-in-spring-mvc by cc-by-sa and MIT license