티스토리 뷰
예제 1
ElTest.java
package el.test;
public class ElTest {
private String[] productList = { "test1", "test2", "test3", "test4", "test5" };
private int num1 = 30;
private int num2 = 50;
public String[] getProductList() {
return productList;
}
public void setProductList(String[] productList) {
this.productList = productList;
}
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
}
Eltest.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form name=from1 method="post" action="sel.jsp">
<jsp:useBean id="eltest" class="el.test.ElTest" scope="session"></jsp:useBean>
<select name="sel">
<%
for (String item : eltest.getProductList()) {
out.println("<option>" + item + "</option>");
}
%>
</select> <br> <input type="submit" value="선택" />
</form>
</body>
</html>
sel.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
선택한 상품 : ${ param.sel}
<br> num1 값 : ${eltest.num1 }
<br> num2 값 : ${eltest.num2 }
<br> num1 + num2 = ${eltest.num1 + eltest.num2 }
</body>
</html>
forEach로 변경
ElTest_forEach.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form name=from1 method="post" action="sel_forEach.jsp">
<jsp:useBean id="eltest_forEach" class="el.test.ElTest"
scope="session"></jsp:useBean>
<select name="sel">
<c:forEach var="item" items="${eltest_forEach.getProductList()}">
<option>${item }</option>
</c:forEach>
</select> <br> <input type="submit" value="선택" />
</form>
</body>
</html>
sel_forEach.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
선택한 상품 : ${ param.sel}
<br> num1 값 : ${eltest_forEach.num1 }
<br> num2 값 : ${eltest_forEach.num2 }
<br> num1 + num2 = ${eltest_forEach.num1 + eltest_forEach.num2 }
</body>
</html>
예제 2
<c:forTokens>를 <c:forEach>로 변경
forTokens
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3><c:forTokens></h3>
<c:forTokens items="123-456-789" delims="-" var="sel">
${sel }<br>
</c:forTokens>
</body>
</html>
forEach
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3><c:forEach></h3>
<c:set var="tel" value="123-456-789" />
<c:set var="result" value="${fn:split(tel,'-')}" />
<c:forEach var="telNum" items="${result}">
${telNum } <br>
</c:forEach>
</body>
</html>
'FRONT-END > JSP' 카테고리의 다른 글
[JSP] JSTL (0) | 2018.05.04 |
---|---|
[JSP] MVC PATTERN (0) | 2018.05.02 |
[JSP] JAVABEAN (0) | 2018.04.16 |
[JSP] ACTION TAG - 액션 태그 (0) | 2018.04.16 |
[JSP] 간단한 장바구니 (0) | 2018.04.13 |
댓글