어제의 달력은 컨트롤러에 서비스가 붙어있는 점이 보기 안좋아 아예 static class를 별도의
클래스로 분리하고 @Component로 빈으로 등록했다.
package com.semiproject.semiproject.service;
import ch.qos.logback.core.util.COWArrayList;
import com.semiproject.semiproject.controller.Index;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Component
public class UserCalendar {
public LocalDate date = LocalDate.now();
public UserCalendar() {
}
public UserCalendar(LocalDate date) {
this.date = date;
}
public int getDate() {
return date.getDayOfMonth();
}
public int getMonth(){
return date.getMonthValue();
}
public int getYear(){
return date.getYear();
}
public static List<List< UserCalendar>> createDate(LocalDate date) {
List< UserCalendar> dateList = new ArrayList<>();
List<List< UserCalendar>> free = new ArrayList<>();
LocalDate first = date.with(TemporalAdjusters.firstDayOfMonth());
LocalDate last = date.with(TemporalAdjusters.lastDayOfMonth());
log.info("달력의 날 first last = {} {}", first,last);
// 첫번째주의 List 값만들기
List< UserCalendar> firstWeek = new ArrayList<>();
List< UserCalendar> secondWeek = new ArrayList<>();
List< UserCalendar> thirdWeek = new ArrayList<>();
List< UserCalendar> fourthWeek = new ArrayList<>();
List< UserCalendar> fifthWeek = new ArrayList<>();
final int imp =first.getDayOfWeek().getValue()%7;
final LocalDate ass= LocalDate.of(9999,1,1);
for (int i=0;i<imp;++i)
firstWeek.add(new UserCalendar(LocalDate.of(999,1,1)));
for(int i=0;i<last.getDayOfMonth();i++){
if(i<7-imp)
firstWeek.add(new UserCalendar(first.plusDays(i)));
if(i>=7-imp && i<14-imp)
secondWeek.add(new UserCalendar(first.plusDays(i)));
if(i>=14-imp && i<21-imp)
thirdWeek.add(new UserCalendar(first.plusDays(i)));
if(i>=21-imp && i<28-imp)
fourthWeek.add(new UserCalendar(first.plusDays(i)));
if(i>=28-imp && i< last.getDayOfMonth())
fifthWeek.add(new UserCalendar(first.plusDays(i)));
}
List<List< UserCalendar>> result = List.of(firstWeek,secondWeek,thirdWeek,
fourthWeek,fifthWeek);
log.info("날짜 데이터 1 = {}", result.get(0).toString());
log.info("날짜 데이터 2 = {}", result.get(1).toString());
log.info("날짜 데이터 3 = {}", result.get(2).toString());
log.info("날짜 데이터 4 = {}", result.get(3).toString());
log.info("날짜 데이터 5 = {}", result.get(4).toString());
log.info("리스트 {}",result.toString());
return result;
}
// public List<List< UserCalendar>> plusMonth(){
// date = date.plusMonths(1);
// return createDate();
// }
//
// public List<List< UserCalendar>> minusMonth(){
// date = date.minusMonths(1);
// return createDate();
// }
@Override
public String toString() {
return " UserCalendar{" +
"date=" + date +
'}';
}
}
package com.semiproject.semiproject.controller;
import com.semiproject.semiproject.service.UserCalendar;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalField;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Controller
public class Index {
// 이 달력은 이중리스트로 해결을 해야할것 같다
@Autowired
UserCalendar userCalendar ;
static LocalDate today = LocalDate.now();
@GetMapping({"/", ""})
public String index(Model model) {
// List<List< UserCalendar>> dates = createDate(today);
today = LocalDate.now();
List<List<UserCalendar>> dates = UserCalendar.createDate(today);
model.addAttribute("dates",dates);
return "index";
}
@ResponseBody
@PostMapping("/add")
public List<List<UserCalendar>> dateAdd(Model model) {
today = today.plus(1, ChronoUnit.MONTHS);
List<List<UserCalendar>> dates = userCalendar.createDate(today);
log.info("한달 더해진 날짜 = {}",dates);
return dates;
}
@ResponseBody
@PostMapping("/minus")
public List<List<UserCalendar>> dateMinus() {
today = today.minus(1, ChronoUnit.MONTHS);
List<List<UserCalendar>> dates = userCalendar.createDate(today);
log.info("한달 빠진 날짜 = {}",dates);
return dates;
}
컨트롤러 부분이 훨씬 깔끔해지고 , @Autowired로 생성자 자동주입 방식을 썻다.
여기서 겪은 어려운 점은 맨 처음에는 today 변수를 UserCalendar에 집어넣어 static 변수를 줘 오류를 찾긴 힘들었다는 점이다.
자꾸 log를 보는데 12월 31일의 날짜만 31개의 값이 등록이 된것이다.
아무리 생각해봐도 미스터리 였는데....
아무리 객체를 new로 새로 만들어도 static이면 공통 변수를 보고 있는 것이나 마찬가지므로 마지막 31일자 값이 today에 저장되어 나머지 객체들도 모두 12월 31일 값을 보고 있는 것이었다....
진짜 static을 쓰면 어려운 문제도 쉽게 해결할 수 있다고 생각했는데 이번 기회로 경각심을 얻었다....
다음부터는 조심해야할 것 같다....
하지만 이것으로 다른 컨트롤러에서도 날짜 객체를 얻을 수 있으니 좋은 경험을 했다고 생각한다...
아까운 내 40분....