Notice
Recent Posts
Recent Comments
«   2024/11   »
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
Archives
Today
Total
관리 메뉴

충분히 쌓여가는

컨트롤러 메서드의 반환타입(String, void, ModelAndView) 본문

Spring/연습

컨트롤러 메서드의 반환타입(String, void, ModelAndView)

빌드이너프 2023. 10. 2. 16:00

반환타입: String

YoilTeller.java

@Controller말고 @RestController 사용하면 yoil.html이 아닌 yoil이라는 문자만 출력됨(주의하기)

@RestControllerc는 view를 이용하는 것이 아닌 반환하는 값을 데이터 브라우저로 보내줌

package com.buildenough.ch2;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
import java.util.Calendar;

@Controller
public class YoilTeller {
    @RequestMapping("/getYoil")
    public String main(int year, int month, int day, Model model) throws IOException {

        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, month-1, day);

        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        char yoil = "일월화수목금토".charAt(dayOfWeek - 1);

        model.addAttribute("year", year);
        model.addAttribute("month", month);
        model.addAttribute("day", day);
        model.addAttribute("yoil", yoil);

        return "yoil";
    }
}

 

resource -> templates -> yoil.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>YoilTeller</title>
</head>
<body>
    <h1 th:text="|${year}년 ${month}월 ${day}일은 ${yoil}요일 입니다|"></h1>
</body>
</html>


반환타입: void

요청 URL을 뷰의 이름으로 사용한다

package com.buildenough.ch2;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
import java.util.Calendar;

@Controller
public class YoilTeller {
    @RequestMapping("/yoil")
    public void main(int year, int month, int day, Model model) throws IOException {

        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, month-1, day);

        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        char yoil = "일월화수목금토".charAt(dayOfWeek - 1);

        model.addAttribute("year", year);
        model.addAttribute("month", month);
        model.addAttribute("day", day);
        model.addAttribute("yoil", yoil);

    }
}


반환타입: ModelAndView

Model과 View를 하나로 묶어서 반환해준다

setViewName으로 view를 지정해줌

(예외처리할 때 주로 ModelAndView 사용함)

package com.buildenough.ch2;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
import java.util.Calendar;

@Controller
public class YoilTeller {
    @RequestMapping("/yoil")
    public ModelAndView main(int year, int month, int day) throws IOException {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("yoilError");

        if(!isValid(year, month, day)) {
            return mv;
        }

        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, month-1, day);

        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        char yoil = "일월화수목금토".charAt(dayOfWeek - 1);

        mv.addObject("year", year);
        mv.addObject("month", month);
        mv.addObject("day", day);
        mv.addObject("yoil", yoil);
        mv.setViewName("yoil");

        return mv;
    }

    private boolean isValid(int year, int month, int day) {
        return true;
    }
}