N스크린하이브리드앱과정/JSP:Model-2

[9주차][5일][3~6th] JSP커스텀 태그와 JSTL

광천스러움 2013. 9. 13. 12:31

★ jstl 실습

Model2 라는 프로젝트를 만들어 준다(jstl을 실습할 것이므로 jstl 폴더만 만들어준다).

 

 

1. eltest.jsp

바디

<%
session.setAttribute("test", "Session Test");
%>
<form action="eltest2.jsp" method="post">
이름:<input type="text" name="name" value="홍길동">
<input type="submit" value="전송">
</form>

 

 

2. eltest2.jsp

바디

<%
request.setCharacterEncoding("utf-8");
%>
<!-- 세션값 출력 가능 -->
<h3>${sessionScope.test }</h3>
<!-- 이름이 name인 input태그 파라미터값을 가져옴 -->
<h3>${param.name }</h3>
<h3>\${10%3 }= ${10%3 }</h3>
<h3>\${5!=7 }= ${5!=7 }</h3>

 

 

3. jstlcore.jsp

<!-- 태그 라이브러리 설치 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

바디

<c:set var="test" value="Hello jstl"/>
<h3>변수값 출력:<c:out value="${test }"/></h3>
<h3>삭제후 변수값 출력:<c:remove var="test"/></h3>

<c:catch var="err">
<%=10/0 %>
</c:catch>
<h3>오류:<c:out value="${err }"/></h3>

<c:if test="${5<10 }">
<h3>5는 10보다 작다</h3>
</c:if>

<c:choose>
<c:when test="${5+10==50 }">
<h3>5+10은 50이다</h3>
</c:when>
<c:otherwise>
<h3>5+10은 50이 아니다</h3>
</c:otherwise>
</c:choose>

 

4. jstlcore2.jsp

태그 라이브러리

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

 

바디

<c:forEach var="test" begin="1" end="10" step="2">
 <b>${test }</b>&nbsp;
</c:forEach>
<br>
<c:forTokens var="alphabet" items="a,b,c,d,e,f,g,h" delims=",">
 <b>${alphabet }</b>&nbsp;
</c:forTokens>
<br>
<c:set var="data" value="홍길동,이길동,김길동" />
<c:forTokens var="name" items="${data }" delims=",">
 <b>${name }</b>
</c:forTokens>

 

 

5. jstlfmt.jsp

태그 라이브러리

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

 

바디

<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="50000" type="currency"/><br>
<fmt:formatNumber value="0.15" type="percent"/><br>
<fmt:formatNumber value="500567300" pattern="###,###,###"/><br><br>

<jsp:useBean id="date" class="java.util.Date"/>
<fmt:formatDate value="${date }" type="date" /><br>
<fmt:formatDate value="${date }" type="time" /><br>
<fmt:formatDate value="${date }" type="both" /><br>
<fmt:formatDate value="${date }" type="both" timeStyle="short" dateStyle="short"/><br>
<fmt:formatDate value="${date }" type="both" timeStyle="long" dateStyle="long"/><br>

 

 

6. jstlsql.jsp

태그 라이브러리

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

 

바디

<sql:setDataSource var="con"
 driver="com.mysql.jdbc.Driver"
 url="jdbc:mysql://localhost:3306/jspbeginner"
 user="jspid"
 password="jsppass"/>
 
<sql:update dataSource="${con }">
 insert into test(num,name) values(1,'홍길동')
</sql:update>
<sql:update dataSource="${con }">
 insert into test(num,name) values(2,'김길동')
</sql:update>
<sql:update dataSource="${con }">
 insert into test(num,name) values(3,'홍길순')
</sql:update>
<sql:update dataSource="${con }">
 insert into test(num,name) values(4,'김길순')
</sql:update>

<sql:query var="rs" dataSource="${con }">
 select *from test where name=?
 <sql:param>홍길동</sql:param>
</sql:query>

<c:forEach var="data" items="${rs.rows }">
 <c:out value="${data['num'] }"/>
 <c:out value="${data['name'] }"/>
 <br>
</c:forEach>

 

 

7. jstlxml.jsp

태그 라이브러리

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

 

바디

<x:parse var="xmldata">
<students>
 <student>
 <name>김광천</name>
 <age>28</age>
 <gender>남</gender>
 <phone>010-111-1111</phone>
 </student>
 
 <student>
 <name>김길동</name>
 <age>19</age>
 <gender>남</gender>
 <phone>010-111-1111</phone>
 </student>
 
 <student>
 <name>김길순</name>
 <age>25</age>
 <gender>여</gender>
 <phone>없음</phone>
 </student>
 
 <student>
 <name>홍길순</name>
 <age>10</age>
 <gender>여</gender>
 <phone>없음</phone>
 </student>
</students>
</x:parse>

<x:forEach select="$xmldata//student">
 <x:if select="./name!='홍길순'">
  <x:out select="./name"/>
  <x:out select="./age"/>
  <x:out select="./gender"/>
  <x:choose>
   <x:when select="./phone!='없음'">
    [전화번호:<x:out select="./phone"/>]
   </x:when>
   <x:otherwise>
    [전화 없음]
   </x:otherwise>
  </x:choose>
  <br>
 </x:if>
</x:forEach>

 

★ 첨부

- jar 파일

jstl.jar

standard.jar

xalan.jar