JSP jstl

미니멀리즘·2022년 11월 16일

https://www.javatpoint.com/jstl-function-tags

JSTL이란?
HTML 코드 내에 java 코드인 스크립틀릿 <%= student %>를 ${student}로, <%=if %>문을 <c:if>, <%=for%>문을 <c:forEach>로 대체하여 사용한다.

JSP - EL 표현식 문법과 사용 방법
EL(Expression Language)

foreach.jsp

<!-- jar파일 붙여 놓음 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<% 
	//배열을 만듬
	String[] cities = {"Mumbai", "Singapore", "Philadelphia"};
	//이페이에 있는 속성들을("이름으로", 이값들을 저장함)
	pageContext.setAttribute("myCities",cities);
%>

<html>
<body>
	
	<c:forEach var="tempCity" items = "${myCities}">
	
	${tempCity} <br/>
	
	</c:forEach>

</body>

</html>
  

foreach-student-test.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import = "com.jstlEx.jsp.tagdemo.Student" %>
<%@ page import = "java.util.*" %>

<%
	//배열을 만듬
	/* List<Student> student = new ArrayList<>();

	Student student2 = new Student("John","Doe",false);
	Student student3 = new Student("MaxWell","Johnson",false);
	
	student.add(student2);
	student.add(student3);
	student.add(new Student("Marry","Public",true)) */;
	
	Student[] student = new Student[3];
	student[0] = new Student("John","Doe",false);
	student[1] = new Student("MaxWell","Johnson",false);
	student[2] = new Student("Marry","Public",true);
	
	
	/* Student[] student = {(new Student("John","Doe",false)),
			(new Student("MaxWell","Johnson",false)),
			(new Student("Marry","Public",true))};  */
	
	//이페이에 있는 속성들을("이름으로", 이값들을 저장함)
	pageContext.setAttribute("mystudent",student);
	
%>

<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>


<c:forEach var="student2" items = "${mystudent}">
	${student2.firstName} ${student2.getLastName()} ${student2.isGoldCustomer()} </br>
</c:forEach>


	<table border = "2">
	<tr>
		<th>First Name</th>
		<th>Last Name</th>
		<th>Gold Customer</th>
		<th>jstl core : if ,choose</th>
		
	</tr>
	<c:forEach var="student" items = "${mystudent}">
		
		<tr>
			<td>${student.firstName} </td>
			<td>${student.getLastName()} </td>
			<td><c:if test="${student.isGoldCustomer()}">
					Special Discount
				</c:if>
				<c:if test="${!student.isGoldCustomer()}">
					No offer
				</c:if>
			</td>
			<td>
				<c:choose>
					<c:when test = "${'Marry' eq student.firstName}">
						First Name is Marry
					</c:when>
					
					<c:otherwise>
						NoNoNo
					</c:otherwise>
				</c:choose>
			</td>
		</tr>

	</c:forEach>
	</table>
 
</body>
</html>

function-test.jsp

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

<html>

<body>


<c:set var="data" value="Lovejava"/>


Length of the string <b>${data}</b> : ${fn:length(data)} 
<br/><br/>

Uppercase version of the string : <b>${data}</b> : ${fn:toUpperCase(data)}
<br/><br/>

Does the string <b>${data}</b> Start with <b>
${fn:substring(data,0,4)}</b>? : ${fn:startsWith(data,"Love"")}

</body>

</html>

split-join.jsp

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

<html>

<body>
<%-- Here is the jsp comment --%>
<!-- Here is the html comment -->

<c:set var="data" value="Singapore,Toyko,Munbai,London"/>

<h2>N0.1</h2>
<c:forEach var= "test" items = "${data}">
	${test}<br/>
</c:forEach>

<br/>

<h2>First Demo</h2>

<c:set var ="FirstDemo" value = "${fn:split(data,',')}"/>

<c:forEach var="i" items = "${FirstDemo}">
	${i} <br/>
</c:forEach>


<h2>Second Demo</h2>
Result of joining : ${fn:replace(data,",","*")}

 <br/> <br/>

<c:set var="j" value ="${fn:join(FirstDemo,'*')}"/>
Result of joining2 : ${j}

</body>
</html>

i18n-messages-test.jsp

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

	<!-- 변수선언 --> 
<c:set var="theLocale" 
value = "${not empty param.theLocale 
		? param.theLocale : pageContext.request.locale}"
scope="session"/>
	<%-- {2>1 ? true : false } --%>

<fmt:setLocale value = "${theLocale}"/>
<fmt:setBundle basename = "com.jstlEx.jsp.tagdemo.i18n.resources.mylabels"/>
<html>
<body>
${pageContext.request.locale}
<a href="i18n-messages-test.jsp?theLocale=en_US">English (US)</a>|
<a href="i18n-messages-test.jsp?theLocale=es_ES">Spanish (ES)</a>|
<a href="i18n-messages-test.jsp?theLocale=de_DE">German (DE)</a>

<hr>
	<fmt:message key="label.greeting"/> <br/><br/>
	<fmt:message key="label.firstname"/> <i>John</i> <br/>
	<fmt:message key="labal.lastname"/> <i>Doe</i> <br/><br/>
	<fmt:message key="label.welcome"/> <br/>
<hr>

Selected locale: ${theLocale}
</br>

</body>
</html>
profile
웹 개발자

0개의 댓글