특정 컨트롤러의 로직을 처리할 때, 다른 경로를 호출하는 경우가 있다. 이때 redirect: 를 사용한다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * SampleContoller4 
 * Use RedirectAttribute for calling another path
 * Use 'redirect:'
 */
 
package org.zerock.web;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.zerock.domain.ProductVO;
 
@Controller
public class SampleController4 {
 
    private static final Logger logger =
        // check the log
        LoggerFactory.getLogger(SampleController4.class);
    
    @RequestMapping("/doE")
    public String doE(RedirectAttributes rttr){
        logger.info("################## doE ##################");
        logger.info("redirect to doF");
        
        // when we send temporary data, use 'addFlashAttribute'  
        rttr.addFlashAttribute("msg""redirected message!!!");
        
        // use 'redirect:'
        return "redirect:/doF";
    }
    
    @RequestMapping("/doF")
    public void doF(String msg) {
        
        logger.info("################## doF ##################");
        logger.info("doF : " + msg);
    }
}
 
cs


결과 : http://localhost:8080/doE 라고 입력을 하면..http://localhost:8080/doF 경로로 redirect 된 결과가 호출 된다. 

+ Recent posts