『 파일 다운로드(Writer/OutputStream사용)

​<소스코드>

​@Controller
public class DownloadController {
    // Writer를 사용하는 경우
    @RequestMapping(value="/downloadFile3", method = {RequestMethod.GET} )
    public void downloadFile3(Writer writer) throws IOException {
       
        writer.append("HelloWorld");
    }
}


 

 

 

 


 

 



『 파일 다운로드(ResponseEntity사용)

​<소스코드>

​@Controller
public class DownloadController {
   
    // ResponseEntity를 사용하는 경우
    @RequestMapping(value="/downloadFile2", method = {RequestMethod.GET} )
    public ResponseEntity<String> downloadFile2() throws IOException {
       
        // HTTP헤더 지정
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "octet-stream"));
        headers.set("Content-Disposition", String.format("filename=\"%s\"", "sample.txt"));
       
        String data = "HelloWorld!";
        return new ResponseEntity<String>(data, headers, HttpStatus.OK);
       
    }
}



 

 

 


 

 

 

 

 



『 파일 다운로드(HttpServletResponse사용)

<소스코드>

​@Controller
public class DownloadController {
   
    // HttpServletResponse를 사용하는 경우
    @RequestMapping(value="/downloadFile1", method = {RequestMethod.GET} )
    public void downloadFile1(HttpServletResponse response) throws IOException {
       
        // HTTP 헤더 지정
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", String.format("filename=\"%s\"", "sample.txt"));
       
        PrintWriter writer = response.getWriter();
        writer.append("HelloWorld");
    }
}

 

 

 

 

 


 


Form으로부터 값을 받아,

세션에 저장하는 경우 



<소스코드>


@Controller
public class SampleController {
 
    @RequestMapping(value="/sample", method={RequestMethod.POST})
    public ModelAndView sample(WebRequest request, @ModelAttribute LoginCommand command, BindingResult bindingResult) {
        // 바인드시 에러처리
        if(bindingResult.hasErrors()) {
            ・・・생략
        }
        // 세션에 데이터 저장
        request.setAttribute("loginUser", "hogehoge", RequestAttributes.SCOPE_SESSION);
       
        ModelAndView mav = new ModelAndView("/toView");
        return mav;
    }
}



 

 

 

 

 

 

 

 


『 Form으로부터 값을 받는 경우

​<소스코드>

​@Controller
public class SampleController {
 
    @RequestMapping(value="/sample", method={RequestMethod.POST})
    public ModelAndView sample(@ModelAttribute LoginCommand command, BindingResult bindingResult) {
       
        // 바인드시 에러처리
        if(bindingResult.hasErrors()) {
           ・・・생략
        }
        ModelAndView mav = new ModelAndView("/toView");
        return mav;
    }
}


 

 

 

 

 



 『 URL 리퀘스트로부터 값을 받는 경우

​<소스코드>

​@Controller
public class SampleController {
 
    @RequestMapping(value="/sample", method={RequestMethod.GET})
    public ModelAndView sample(@RequestParam(value="userCd", required=true) Integer id, @RequestParam String token) {
       
        // 바인드 시 에러처리
        if(bindingResult.hasErrors()) {
           ・・・생략
        }
       
        ModelAndView mav = new ModelAndView("/toView");
        return mav;
    }
}


 

 

 

 

 


『 다른 URL로 이동하는 경우

​<소스코드>

​@Controller
public class SampleController {
    // 포워드
    @RequestMapping("/sample1")
    public String forward() {
        return "forward:/hello.html";
    }
   
    // 리다이렉트
    @RequestMapping("/sample2")
    public String redirect() {
        return "redirect:/hello.html";
    }
}


 

 

 


 



『 단순 View(Jsp)로 이동하는 경우(모델 존재) 

​<소스코드>

​@Controller
public class SampleController {
 
    @RequestMapping("/sample")
    public ModelAndView sample() {
       
        // View 설정
        ModelAndView mav = new ModelAndView("/toView");
       
        // 모델을 취득해서, 메시지 설정
        mav.addObject("message1", "메시지1");
       
        return mav;
    }
}


 

 

 


+ Recent posts