JSP

JSTL(JSP Standard Tag Library) - 2. if 사용

에어팟맥스 2022. 8. 31. 23:20

실행파일 - 두개의 문자열을 입력받아서 같은지 다른지 검사하기

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%
	String ctxPath = request.getContextPath();
	// /JSPServletBegin
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>두개의 문자열을 입력받아서 같은지 다른지 검사하기</title>

<script type="text/javascript" src="<%= ctxPath%>\js\jquery-3.6.0.min.js"></script>
<script type="text/javascript">

	$(document).ready(function(){
		
	//	$("form[name='myFrm']").bind("submit", function(){ });
		$("form[name='myFrm']").submit( function(){ 
			
			const first_val = $("input:text[name='first']").val().trim();
			const second_val = $("input:text[name='second']").val().trim();
			
			if(first_val == "" || second_val == "") {
				alert("문자를 입력하세요.");
				return false; // submit 을 하지 않음
			}
			
		}); // end of $("form[name='myFrm']").submit() --------------
		
	}); // end of $(document).ready() -----------------------

</script>

</head>
<body>

	<form name="myFrm" action="02_if_result_02.jsp">
		첫번째 입력란 : <input type="text" name="first" />
		두번째 입력란 : <input type="text" name="second" />
		세번째 입력란 : <input type="text" name="third" /> 
		<br>
		<input type="submit" value="확인" />
		<input type="reset" value="취소" /> 
	</form>

</body>
</html>

 

결과 파일 - 전송되어 온 값을 if 를 사용하여 비교한 결과물 출력하기

 

${param.first} 는 getParameter 를 해온 것이다. 

eq: equal(같음 == )

ne: !equal(같지않음 != )

 

<c:if test="${param.first eq param.second}">

<c:if test="${param.first ne param.second}">

 

empty 는 null 과, null은 아니지만 아무것도 없는 것(사이즈가 0) 모두를 포함하기 때문에 empty 사용 권장

<c:if test="${empty param.third}">

<c:if test="${not empty param.third}">

<c:if test="${!empty param.third}">

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%-- ==== JSTL(JSP Standard Tag Library) 사용하기 ==== --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>전송되어 온 값을 if 를 사용하여 비교한 결과물 출력하기</title>
</head>
<body>

	<c:if test="${param.first eq param.second}">
		<!-- ${param.first} 는 getParameter 를 해온 것이다. eq: equal(같음 == )-->
		${param.first} 와 ${param.second} 은 <span style="color:blue;">같습니다.</span>
	</c:if>
	
	<c:if test="${param.first ne param.second}">
		<!-- ${param.first} 는 getParameter 를 해온 것이다. ne: !equal(같지않음 != )-->
		${param.first} 와 ${param.second} 은 <span style="color:red;">같지 같습니다.</span>
	</c:if>
	
	<hr style="border:solid 1px red; margin:20px 0;">
	
	<c:if test="${param.first == param.second}">
		<!-- ${param.first} 는 getParameter 를 해온 것이다. eq: equal(같음 == )-->
		${param.first} 와 ${param.second} 은 <span style="color:blue;">같습니다.</span>
	</c:if>
	
	<c:if test="${param.first != param.second}">
		<!-- ${param.first} 는 getParameter 를 해온 것이다. ne: !equal(같지않음 != )-->
		${param.first} 와 ${param.second} 은 <span style="color:red;">같지 같습니다.</span>
	</c:if>
	
	<hr style="border:solid 1px blue; margin:20px 0;">
	
	<c:if test="${empty param.third}">
		세번째 입력란을 <span style="color: yellow; background-color: black;">입력하지 않으셨습니다.</span>
	</c:if>
	
	<c:if test="${not empty param.third}">
		세번째 입력란을 <span style="color: green;">입력하셨습니다.</span>
	</c:if>
	
	<c:if test="${!empty param.third}">
		세번째 입력란을 <span style="color: green;">입력하셨습니다.</span>
	</c:if>

</body>
</html>