JSP JSTL (if, choose)
JSP에서 값을 넘길때 여러가지 경우가 있을 수가 있는데 java와 마찬가지로 if문을 쓸 수가 있다.
위처럼 red, green, blue 중 하나를 보냈을 때 받는 쪽에서는 여러가지 값이 넘어올 수가 있다.
이것을 처리해주어야 하는데
Scriptlet과 java if문 , JSTL 사용
scriptlet을 이용해서 request.getParameter("")로 넘겨온 값을 받아주고
java의 if문처럼 사용할 수가 있다.
또는
JSTL을 이용해서 if문을 만들 수도 있다.
<c:set var="colorValue2" value="black"></c:set>
<c:if test="${param.color =='r'}">
<c:set var="colorValue2" value="red"></c:set>
</c:if>
<c:if test="${param.color =='b'}">
<c:set var="colorValue2" value="blue"></c:set>
</c:if>
<c:if test="${param.color =='g'}">
<c:set var="colorValue2" value="green"></c:set>
</c:if>
EL tag를 이용해서 넘어오는 값을 ${param.color}로 받은 다음 그 값이 어떤 것이냐에 따라 colorValue2의 값을 정해주는것이다.
JSTL Choose문 사용
<c:choose>
<c:when test="${param.color eq 'r' }">
<c:set var="colorValue" value="red"></c:set>
</c:when>
<c:when test="${param.color eq 'b' }">
<c:set var="colorValue" value="blue"></c:set>
</c:when>
<c:when test="${param.color eq 'g' }">
<c:set var="colorValue" value="green"></c:set>
</c:when>
<c:otherwise>
<c:set var="colorValue" value="black"></c:set>
</c:otherwise>
</c:choose>
자바의 스위치 문과 같이 choose 밑에 when으로 조건을 걸어주고 set으로 값을 정해주면 된다.