2017년 10월 12일 목요일

[JSTL] Introduction to JSTL

  • What is JSTL?

JSTL stands for 'JSP Standard Tag Library'. It is a way to simplify JSP source code. As you may know, JSP's readibility is not good. JSTL increases its readibility and write code much simply.

  • How to download .jar file of JSTL.

Goto: http://www.mvnrepository.com
Search: jstl
Chose any jstl link and download .jar file.

  • How to apply JSTL in Eclipse

  1. Create a project: File > New > Dynamic Web Project ( keep pressing 'next' and check 'Generate web.xml deployment descripter' )
  2. Copy & Paste into lib folder: WebContent > WEB-INF > lib
  3. Now, you are ready to use JSTL

  • How to use JSTL in JSP

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

MUST JSP JSTL
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int n1 = 100, n2=200;
%>
<c:set var="n1" value="<%=n1%>"/>
<c:set var="n2" value="<%=n2%>"/>

n1 : ${n1} <br>
n2 : ${n2} <br>
n1 + n2 : ${n1+n2}<br>
n1 > n2 : ${n1>n2}<br>
n1 == n2 : ${n1 == n2}<br>
n1>0 && n2>0 : ${n1>0 && n2>0}<br>
n1>0 and n2>0 : ${n1>0 and n2>0}<br>
------------------------------------<hr>
<%List<String> words = new ArrayList<>();%>
<c:set var="words" value="<%=words%>"/>

is word empty? >> ${empty words}<br>
<% words.add("apple"); %>
add a word into 'words' List> ${words.get(0)}<br>
</body>
</html>


댓글 없음:

댓글 쓰기