프로그래밍/Spring

컨트롤러

kingroad 2018. 7. 22. 00:18

package pms.web.json;


import org.springframework.http.HttpStatus;

import org.springframework.web.bind.annotation.MatrixVariable;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseStatus;

import org.springframework.web.bind.annotation.RestController;


import pms.domain.Tour;

import pms.service.TourService;


@RestController

@RequestMapping("/tour")

public class TourController {

    

    TourService tourService;

    

    public TourController(TourService tourService) {

        this.tourService = tourService;

    }


    @RequestMapping("add")

    @ResponseStatus(HttpStatus.CREATED)

    public void add(Tour tour) throws Exception {

        tourService.add(tour);

    }

    

    @RequestMapping("delete")

    //@ResponseStatus(HttpStatus.OK) // 응답 상태 코드 값의 기본은 "200(OK)" 이다.

    public void delete(@RequestParam("lno") int no)/*@Request저장소로 부터 값을 꺼내서 값을 전달해 준다. 만약 @RequestParam("no") int no처럼 파라미터 명과 매개 변수명이 같으면 @RequestParam을 생략할 수 있다.*/ throws Exception {

        tourService.delete(no);

    }

    

    @RequestMapping("list{page}")

    public Object list(

            @MatrixVariable(defaultValue="1") int pageNo,

            @MatrixVariable(defaultValue="3") int pageSize) {        

        return tourService.list(pageNo, pageSize);

    }

    

    @RequestMapping("update")

    @ResponseStatus(HttpStatus.OK) // 기본 값이 OK 이다. 

    public void update(Tour tour) throws Exception {

        tourService.update(tour);

    }

    /tour/1

    @RequestMapping("{no}")

    public Tour view(@PathVariable int no) throws Exception {

        return tourService.get(no); //DAO에서 selectOne으로 선언해놨기 때문에 get은 selectOne으로 동작한다

    }


}


JSON 데이터를 출력하는 페이지 컨트롤러 생성

기본적으로 컨트롤러는 사용자의 요청을 받아 서비스를 호출하는 역할로 쓰인다. 

RestController는 텍스트 데이터를 반환한다.