24.03.03

서린·2024년 3월 3일

혼자개발

목록 보기
59/82

포트폴리오를 작성하면서 프로젝트를 둘러보다가 한가지 잘못된 곳을 발견했다
등산일지 작성하고 카드뷰로 뜰 때 설정해뒀던 날짜가 글 작성일이 아니라 오늘 현재의 날짜로 설정되어있었다..!

기존 diary.jsp

<%--userId, 날짜, 더보기 --%>
	<div>
		<div class="d-flex justify-content-between">
			<span class="font-weight-bold ml-3 mt-2">${card.user.loginId}</span>
						
			<%--더보기(내가 쓴 글일 때만 노출, 삭제 또는 수정) --%>
			<c:if test="${userId eq card.user.id}" >
				<a href="#" class="more-btn" data-toggle="modal" data-target="#modal" data-post-id="${card.post.id}">
					<img src="https://www.iconninja.com/files/860/824/939/more-icon.png" width="30">
				</a>
			</c:if>
		</div>
		<%--날짜 --%>
		<%
			Date date = new Date();
			SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy년 M월 d일");
			String strDate = simpleDate.format(date);
		%>
		<div class="ml-3">					
			<%=strDate %>
		</div>
	</div>	

프로젝트 초반에 <fmt:formatDate value="${post.createdAt}" type="date" pattern="yyyy년 M월 d일" /> 이 코드로 날짜를 구현하려고 했더니 인식이 안되어 다른 방법으로 시도했던것인데
다시 안되는 이유를 찾아 fmt를 사용해야겠다고 생각했다

다른 기능을 구현 할 때도 종종 el태그가 인식되지 않는 경우가 있었는데 연결이 안되는 경우가 대부분이었다 그러니까 post에서 createAt을 꺼내올 수 없는 상황 같았다

위쪽의 카드뷰 순환문을 보니 <c:forEach items="${cardList}" var="card">로 cardList를 뿌리고 있는것을 보고 card안에 post의 createAt이 포함되지 않았는지를 확인해봤다

CardView.java

@Data
public class CardView {

	//글 1개 정보
	private PostEntity post;
		
	//글쓴이 정보
	private UserEntity user;
	
	//댓글
	private List<CommentView> commentList;
	
	//좋아요 갯수
	private int likeCount;
	
	//좋아요 눌렀나 
	private boolean filledLike;
}

PostBO.java

@Service
public class DiaryBO {

	@Autowired
	private PostBO postBO;
	
	@Autowired
	private UserBO userBO;
	
	@Autowired
	private CommentBO commentBO;
	
	@Autowired
	private LikeBO likeBO;
	
	//다이어리 카드 구성
	//input : userId(비로그인/로그인 허용 null도 허용)
	//output: List<cardView>
	public List<CardView> generateCardViewList(Integer userId) { //화면용으로 가공할 때는 보통 generate 사용
		List<CardView> cardViewList = new ArrayList<>(); // [] 비어있는 리스트
		
		//글 목록을 가져오기
		List<PostEntity> postList = postBO.getPostList();
		
		//글목록 반복문 순회      
		for (PostEntity post : postList) { // 글이 3개일 때 : 0 1 2 
			
			//post 하나에 대응되는 하나의 카드를 만든다
			CardView cardView = new CardView();
			
			//글 1개
			cardView.setPost(post);
			
			//글쓴이 정보세팅 userbo에게 id로 조회하겟다고 함
			UserEntity user = userBO.getUserEntityById(post.getUserId());
			cardView.setUser(user);
	
			//댓글
			List<CommentView> commentList = commentBO.generateCommentViewListByPostId(post.getId());
			cardView.setCommentList(commentList);
			
			//좋아요 갯수
			int likeCount = likeBO.getLikeCountByPostId(post.getId());
			cardView.setLikeCount(likeCount);
			
			//내가 좋아요 눌렀는지
			boolean filledLike = likeBO.filledLike(post.getId(), userId);
			cardView.setFilledLike(filledLike);
			
			cardViewList.add(cardView);
		}
		return cardViewList;
	}
	
}

cardView와 postBO에 날짜에 관한 코드가 없는 것을 확인 하고
각각 코드를 추가해줬다

//글 작성 날짜 
	private Date createdAt;
//글 작성 날짜 
	cardView.setCreatedAt(post.getCreatedAt());
    

그리고 diary.jsp의 날짜 부분을 수정해주니

<%--날짜 --%>
  <div class="ml-3">		
  	<fmt:formatDate value="${card.post.createdAt}" type="date" pattern="yyyy년 M월 d일" />					
  </div>

날짜가 잘 나타났다 !

0개의 댓글