Pages

Tuesday, January 15, 2013

JSTL Cơ bản [ P1 Core]

[updating...]
JSP là một công nghệ được xâu dựng dựa trên cú pháp của ngôn ngữ kịch bản - script language tương tự php , js.... do vậy việc ta lập trình các câu lệnh điều khiển, vòng lặp , đọc ghi dữ liệu.... đều phải khai báo, sử dụng thông qua các script tag như  :

JSP Syntax :
<%-- -->
<% code fragment %>
 or <jsp:scriptlet> code fragment </jsp:scriptlet>
<%= expression %>
or
<jsp:expression> expression </jsp:expression>
<%! declaration; [ declaration; ]+ ... %>
or
 <jsp:declaration> code fragment [ declaration; ]+ ... </jsp:declaration>
--> điều này khiển trang jsp trở nên phức tạm lên rất nhiều.
Examples
<% 
   String name = null; 
   if (request.getParameter("name") == null) { 
%> 
 
<%@ include file="error.html" %> 
 
<% 
   } else { 
   foo.setName(request.getParameter("name")); 
   if (foo.getName().equalsIgnoreCase("integra")) 
      name = "acura"; 
   if (name.equalsIgnoreCase( "acura" )) { 
%>
Như ta thấy ví dụ này chỉ kiểm tra giá trị của một param để làm công việc tương ứng mà cũng đã dối mắt rồi khó kiểm soát và việc bảo trì cũng trở lên khó khăn hơn.

Một giải pháp được ưa thích hơn đó là sử dụng JSTL Core tag libs.
JSTL core bao gồm các tag sau : 


JSTL Core
Sau đây ta sẽ đi lần lượt các tag thường xuyên được sử dụng trong ứng dụng.

<c:out /> 



<c:forEarch /> 

-------------------------------------------------------------------------
Muốn lấy số thứ tự hiện thời của vòng lặp khi thao tác với forEach tag trong JSTL -> sử dụng thuộc tính varStatus.

- [varStatus].index : bắt đầu từ 0  -  the 0-based index of the current round of the iteration
- [varStatus].count : bắt đầu từ 1 -   the 1-based count of the current round of the iteration


-------------------------------------------------------------------------

Link tham khảo :
http://jstl.java.net/
http://docs.oracle.com/javaee/6/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html
http://docs.oracle.com/javaee/5/tutorial/doc/bnakc.html
http://stackoverflow.com/tags/jstl/info

No comments:

Post a Comment