괴발개발/Spring Framework

<c:if></c:if> 로 페이지 예외처리, 조건에 따른 값을 보여주기, option값 select하기

moonday 2021. 7. 26. 23:34

c:if를 사용하려면 아래의 코드가 해당파일의 맨위에 꼭 있어야 됨!

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

기본생김새 : 

<c:if test = "조건식"></c:if>

*조건식이 들어가야하는게 포인트이지만, 변수나 객체,메소드사용이 자유로움

 

예를들어, 게시판 목록에서 게시글이 아예 없는 경우, 빈페이지나 테이블등을 보여주기보다는 게시글이 없다고 알려주는게 나을 때 사용 가능

 

<c:if test="객체이름.size() !=0"></c:if> 를 사용해서 글이 1개이상일때 보여줄 구간을 정함

<c:if test="객체이름.size() == 0"></c:if> 를 사용해서 글이0개일때 글이없다고 알려주는 텍스트를 노출함

	<div class="container">
	  <h2>게시판 목록</h2>
	  <p>자유롭게 글을 써재끼세요 Yayyy</p>
	  <a href="<%=request.getContextPath()%>/board/register"><button class="btn btn-outline-primary">글쓰기</button></a>            
	  <c:if test="${list.size() != 0}">
		  <table class="table table-hover">
		    <thead>
		      <tr>
		        <th>글번호</th>
		        <th>제목</th>
		        <th>작성자</th>
		        <th>조회수</th>
		        <th>등록일</th>
		      </tr>
		    </thead>
		    <tbody>
	      		<c:forEach items="${list}" var ="list">
		    	  <tr>
			        <td>${list.num}</td>
			        <td><a href="<%=request.getContextPath()%>/board/detail?num=${list.num}">${list.title}</a></td>
			        <td>${list.writer}</td>
			        <td>${list.views}</td>
			        <td>${list.getDateTime()}</td>
		      	  </tr>
	        	</c:forEach>
		    </tbody>
		  </table>
	  </c:if>
	  <c:if test="${list.size()==0}">
	  	<h2>등록된 게시글이 없습니다.</h2>
	  </c:if>
	</div>

 

 

페이지 뿐만아니라, 글자나 라인 또는 값 등등을 c:if로 처리할 수 있는데, 

   	    <div class="form-group">
	      <label>성별:</label>
	      <select type="select" class="form-control" name="gender">
	      	<option value="M" <c:if test="${user.gender == 'M'}">selected</c:if> >남</option>
	      	<option value="F" <c:if test="${user.gender == 'F'}">selected</c:if> >여</option>
	      </select>
	    </div>

다음과 같은 코드에서 test의 조건식 안에 'M'과 'F'와 같은 부분은 M과 F가 문자열값인지 다른 값을 의미하는 것인지 인식하기가 어려운 문제가 있기때문에 option의 부분이 jsp파일에서 에러가 계속 날 것.

gender라는 변수의 값을 VO파일에서 char 불가능, String 가능

**Private String gender; 로 변수선언했기를 바람 

 


c:if로 조건에 따른 값을 보여주기 

eq equals <c:if test="${변수명 eq '비교할문자열여기입력'}"></c:if>, 

eq = equals

ne not equals <c:if test="${변수명 ne '비교할문자열여기입력'}"></c:if>

ne = not equals


c:if로 select박스의 option값을 select하기

(DB에 저장된 값을 불러와서 jsp파일에서 저장된 값 기준으로 select해서 보여주기)

<select class="btn btn-outline-secondary rounded-start" name="board_category" id="inputGroupSelect02">
  <option value="0">카테고리</option>
  <option value="1"<c:if test="${board.board_category == '정보공유'}">selected</c:if>>정보공유</option>
  <option value="2"<c:if test="${board.board_category == '상담/질문'}">selected</c:if>>상담/질문</option>
  <option value="3"<c:if test="${board.board_category == '사담'}">selected</c:if>>사담</option>
  <option value="4"<c:if test="${board.board_category == '업데이트'}">selected</c:if>>업데이트</option>
  <option value="5"<c:if test="${board.board_category == '알림'}">selected</c:if>>알림</option>
  <option value="6"<c:if test="${board.board_category == '시험안내'}">selected</c:if>>시험안내</option>
</select>