공부/Spring Boot
2. Spring Boot Controller 작성
오피스엑소더스
2019. 7. 12. 15:11
1. 기초
> 클라이언트의 요청은 디스패처 서블릿이 받아서 처리한다.
> 디스패처 서블릿은 URI를 미리 갖고있다.
> URI의 내용은 컨트롤러에서 정한다.
> view resolver를 활용한다
> @Autowired 활용
- Autowired는 type으로 DI를 가능케 한다
- 서비스 변수에 Autowired 어노테이션을 적용
- Singleton 처럼 쓰인다.
- 컨테이너에 있는 빈 객체 중 타입이 일치하는 빈 객체를 주입시키는 방식
- 타입 우선 매칭
2. 코드
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 | import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class Welcome { @Autowired private CommonService commonService; @RequestMapping("/welcome")//1.클라이언트가 이 문자열을 요청했을 때 public String welcome() { //2.이부분의 내용을 수행하고 commonService.saveLog("나다"); return "welcome";//3.이 문자열을 반환한다. 문자열은 주소가 된다. } @RequestMapping("/freeboard") public String freeboard(@RequestParam("id") String id, Model model) { if("123".equals(id) == false) { //받은 id가 123과 같지 않으면 welcome 페이지로 이동. return "welcome"; } return "freeboard"; } @RequestMapping("/saveLogAsTxtFile") public void saveLogAsTxtFile() { } } | cs |
>