JSP Standard Tag Library (JSTL)是一个标准的标签库用来控制jsp的行为、迭代和控制表现、国际化以及sql等。
JSTL是javaEE的一部分并被绝大部分servlet容易所包含,但当我们用到时需要下载jstl的jar才能在容器中使用。大部分时间,我们可以在事例程序下找到他们,你需要把他们放到你工程的WEB-INF/lib目录下就可以了。这些jar就会被容器所找到,例如在tomcat下你需要jstl.jar 和 standard.jar。并加入到build path下才可以。
JSTL包含以下五类:
1、Core Tags:核心标签提供包含迭代、逻辑判断、异常、url、以及跳转等。你只需要在你的jsp页面包含以下代码就可以:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
|
2、Formatting and Localization Tags:这个是用来提供格式化数字、日期i18n从本地。只需要加入下面代码
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
|
3、SQL Tags:这个是对关系型数据库的支持。例如mysql、oracle等等。之需要加入以下代码:
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
|
4、XML Tags: 主要用户XML文档。例如解析XML,转换xml数据和Xpath表达式等。需要加入如下代码:
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
|
5、JSTL Functions Tags:function 标签用于一些操作逻辑,多数用于String 操作,例如spli等:需要加入以下代码:
|
所有的标签都是以http://java.sun.com/jsp/jstl/开始的。
Tag | Description |
---|---|
To write something in JSP page, we can use EL also with this tag | |
Same as |
|
redirect request to another resource | |
To set the variable value in given scope. | |
To remove the variable from given scope | |
To catch the exception and wrap it into an object. | |
Simple conditional logic, used with EL and we can use it to process the exception from |
|
Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by |
|
Subtag of |
|
Subtag of |
|
for iteration over a collection | |
for iteration over tokens separated by a delimiter. | |
used with |
|
to create a URL with optional query string parameters |
来看事例:
Employee.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package
com.journaldev.model;
public
class
Employee {
private
int
id;
private
String name;
private
String role;
public
Employee() {
}
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getRole() {
return
role;
}
public
void
setRole(String role) {
this
.role = role;
}
}
HomeServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package
com.journaldev.servlet;
import
java.io.IOException;
import
java.util.ArrayList;
import
java.util.List;
import
javax.servlet.RequestDispatcher;
import
javax.servlet.ServletException;
import
javax.servlet.annotation.WebServlet;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
com.journaldev.model.Employee;
@WebServlet
(
"/HomeServlet"
)
public
class
HomeServlet
extends
HttpServlet {
private
static
final
long
serialVersionUID = 1L;
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
List
new
ArrayList
Employee emp1 =
new
Employee();
emp1.setId(
1
); emp1.setName(
"Pankaj"
);emp1.setRole(
"Developer"
);
Employee emp2 =
new
Employee();
emp2.setId(
2
); emp2.setName(
"Meghna"
);emp2.setRole(
"Manager"
);
empList.add(emp1);empList.add(emp2);
request.setAttribute(
"empList"
, empList);
request.setAttribute(
"htmlTagData"
,
"
creates a new line.");
request.setAttribute(
"url"
,
"http://www.journaldev.com"
);
RequestDispatcher rd = getServletContext().getRequestDispatcher(
"/home.jsp"
);
rd.forward(request, response);
}
}
home.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=US-ASCII"
>
<
title
>Home Page
title
>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<
style
>
table,th,td
{
border:1px solid black;
}
style
>
head
>
<
body
>
<%-- Using JSTL forEach and out to loop a list and display items in table --%>
<
table
>
<
tbody
>
<
tr
><
th
>ID
th
><
th
>Name
th
><
th
>Role
th
>
tr
>
<
c:forEach
items
=
"${requestScope.empList}"
var
=
"emp"
>
<
tr
><
td
><
c:out
value
=
"${emp.id}"
>
c:out
>
td
>
<
td
><
c:out
value
=
"${emp.name}"
>
c:out
>
td
>
<
td
><
c:out
value
=
"${emp.role}"
>
c:out
>
td
>
tr
>
c:forEach
>
tbody
>
table
>
<
br
><
br
>
<%-- simple c:if and c:out example with HTML escaping --%>
<
c:if
test
=
"${requestScope.htmlTagData ne null }"
>
<
c:out
value
=
"${requestScope.htmlTagData}"
escapeXml
=
"true"
>
c:out
>
c:if
>
<
br
><
br
>
<%-- c:set example to set variable value --%>
<
c:set
var
=
"id"
value
=
"5"
scope
=
"request"
>
c:set
>
<
c:out
value
=
"${requestScope.id }"
>
c:out
>
<
br
><
br
>
<%-- c:catch example --%>
<
c:catch
var
=
"exception"
>
<% int x = 5/0;%>
c:catch
>
<
c:if
test
=
"${exception ne null}"
>
<
p
>Exception is : ${exception} <
br
/>
Exception Message: ${exception.message}
p
>
c:if
>
<
br
><
br
>
<%-- c:url example --%>
<
a
href
=
"
c:url
>">JournalDev
a
>
body
>
html
>
当访问http://localhost:8080/JSTLExample/HomeServlet这个servlet的时候,我们会看到下面的结果