關(guān)鍵字: jstl api
EL表達式
注:在一個jsp中一定要注意
<%@ page isELIgnored="true|false" %>
1,可以訪問一個簡單的參數(shù)
userName是一個參數(shù)
${userName}
2, 訪問一個嵌套的參數(shù)
${userBean.userName}
3, 可以是一個表達式
${userBean.age>0}
${userBean.age>20 && userBean.age<10}
4, 隱含對象
1) pageContext jsp頁面的上下文,它提供了訪問以下對象的方法
a, Servlet Context,Servlet的上下文信息
b, Session 客戶端的session對象
c, request
d, response
2) param 把請求中的參數(shù)名和單個值進行映射
3) paramValues 把請求中的參數(shù)名和一個array值進行映射
4) cookie 把請求中的cookie名和單個值進行映射
表達式編程舉例:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset:gb2312" language="java" %>
<jsp:useBean id="user" class="com.iss.ch1.TestBean" scope="request">
<jsp:setProperty name="user" property="*" />
</jsp:useBean>
<html>
<head><title>表達式語言舉例</title>
</head>
<body>
姓名:${user.userName}<br>
密碼:${user.password}<br>
年齡:${user.age}<br>
<hr>
姓名:${param.userName}<br>
密碼:${param.password}<br>
年齡:${param.age}<br>
<hr>
姓名:${param['userName']}<br>
密碼:${param['password']}<br>
年齡:${param['age']}<br>
<hr>
標準標簽庫(JSTL)
JSTL包含了和以下操作相關(guān)的標簽
常用標簽 <c:out> ,<c:set>
條件標簽 <c:if>, <c:choose> <c:when> <c:otherwise>
url標簽 <c;import>
xml標簽 <xml:out>
國際化輸出標簽 <fmt:timeZone>
SQL標簽 : <sql:query>
1, 一般標簽;
<c:out> <c:set> <c:remove> <c:catch>
1) 把計算的結(jié)果輸出
a <c:out value="value" [escapeXml="{true|false}"] [default="defaultValue"]/>
b <c:out value="value" [escapeXml="{true|false}"]> body </c:out>
<c:out value="test"/> //輸出"test"
<c:out value="test2'>itese </c:out> //中的body中的內(nèi)容不會發(fā)送到客戶端
<c:out value="${test}"/>
<c:out value="${notex}" default="如果notex變量不存在,則將顯示此信息"/>
2) 用來將某范圍(request,session,application等)中設(shè)置某個值
a, <c:set value="value" var="varName" [scope="{page|request|session|application}"]>
b, <c:set var="varname" [scope="{page....}"]> body </c:set>
c, <c:set value="value" target="target" property="propertyname"/>
d, <c:set target="target" property="propertyname"> body </c:set>
如:<c:set value="admin" var="username"/>
<c:out value="${username}"/>
<c:set var="password">
pass
</c:set>
<c:set value="100" var="maxUser" scope="application"/>
<jsp:useBean id="user" scope="request" class="com.iss.ch1.test"/>
<c:set value="admin" target="${user}" property="userName"/>
<c:set target="${user}" property="password">
test
</c:set>
3) 用于刪除某個變量或者屬性
<c:remove var="varName" [scope="{page|request|session|application}"]/>
如:
<c:set value="20" var="max" scope="application"/>
<c:remove var="max" scope="application"/>
4) 捕獲由嵌套在它里面的標簽拋出的異常
<c:catch [var="varName"]> test </c:catch>
例:
<c:catch var="mytest">
<% int i=0;
int j=10/i;
%>
</c:catch>
<c:out value="${mytest}"/>
<c:out value="${mytest.message}"/>
<c:out value="${mytest.cause}"/>
2 條件標簽
1) 用于進行條件判斷,如果test屬性為true,則就計算它的body
a, <c:if test="test1" var="varName" [scope="{page|request|....}"] />
b, <c:if test="test1" var="varName" [scope="{page|request|....}"] > body </c:if>
test為表達式的條件
例: <jsp:useBean id="user" class="com.iss.ch1.test"/>
<c:set value="16" target="${user}" property="age"/>
<c:if test="${user.age<18}">
對不起,你的年齡過小
</c:if>
2) <c:choose> 用于條件選擇,它和<c:when>及<c:otherwise>一起使用
注: 不能單獨使用
就象是開關(guān)語句 swith
<c:choose> </c:choose>
<c:when test="條件"> 也就是<c:choose>的分支
此語句一定要在<c:choose>的里面,并且一定要在<c:otherwise>之前
</c:when>
在<c:choose>中可以有0個或者多個<c:when>或<c:otherwise>
<c:otherwise> 也就是最后的分支語句
test 與開關(guān)語句中的最后選擇
</c:otherwise>
如:
<c:choose>
<c:when test="${user.age<=18}">
<font color="blue">
</c:when>
<c:when test="${user.age<30 && user.age>18}">
<font color="red">
</c:when>
<c:otherwise>
<font color="green">
</c:otherwise>
</c:choose>
你的年齡:<c:out value="${user.age}"/>
3 迭代標簽
我們一般使用 Iterator 或Enumeration來進行迭代
<c:forEach>
<c:forTokens>
語法1 在Collection中迭代
<c:forEach [var="varName"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="stet"]>
body
</c:forEach>
語法2 迭代固定的次數(shù)
<c:forEach [var="varName"] [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="stet"]>
body
</c:forEach>
例:
<% Collection users=new ArrayList();
for(int i=0;i<5;i++)
{
TestBean user=new TestBean();
user.setUser("user");
user.setAge("age");
users.add(user);
}
session.setAttribute("usert",users);
%>
<c:forEach var="use" items="${usert}">
<tr>
<td><c:out value="${use.user}" /></td>
<td><c:out value="${use.age}" /></td>
</tr>
</c:forEach>
<c:forTokens>
主要用于處理TokenString 的迭代,可以指定一個或者多個分隔符
<c:forTokens items="stringOfTokens" delims="delimiters" [var="varName"] [begin="begin"] [end="end"] [step="step"]>
body
</c:forTokens>
使用"|" 作為分隔符
<c:forTokens var="tok" items="blue,red,green|yellow|pink,black|white" delims="|">
<c:out value="${tok}"/>©
</c:forTokens>
使用"|," 作為分隔符
<c:forTokens var="tok" items="blue,red,green|yellow|pink,black|white" delims="|,">
<c:out value="${tok}"/>©
</c:forTokens>
URL 相關(guān)的標簽
就是頁面導(dǎo)向,重定向,資源獲得,參數(shù)傳遞等相關(guān)標簽
<c:import url="url" var="varname" scope=" " charEnvoding=" ">
tstee <c:param >
</c:import >
與<jsp:include file=""/>一樣的功能
<c:redirect url=" " > 重定向到另一個資源
<c:param> teewe
</c:redirect>
<c:redirect url="test.jsp">
<c:param name="username" value="admin"/>
</c:redirect> 重定向到test.jsp中同時帶上相關(guān)參數(shù)
注:在以上所有標簽,在jsp中要定義標簽:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
xml標簽
<x:parse>
<x:out>
<x:set>
1) <x:parse>用于解釋xml文檔
a, 解釋由String 或reader對象產(chǎn)生的xml文檔
<x:parse xml="xmldocument" {var="varName" [scope="scope"]|varDom="var" [scopeDom="scope"]} [systemid="systemid"] [filter="filter"/>
b, 解釋在Body中指定的xml文檔
<x:parse {var="varName" [scope="scope"]|varDom="var" [scopeDom="scope"]} [systemid="systemid"] [filter="filter">
body
</x:parse>
例如:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<%@ page contentType="text/html;charset:gb2312" language="java" %>
<html><head><title>test</title></head>
<body>
<c:set var="xmltest">
<a><b><c>test1</c></b><d>test2</d></a>
</c:set>
<x:parse var="myxml" xml="${xmltest}" />
<x:out select="$myxml/a/b/c" />
<x:out select="$myxml//d" />
<x:parse var="mybook">
<books>
<book id="1">
<name>java書</name>
<price>89</price>
</book>
</books>
</x:parse>
<x:out select="$mybook/books//name"/>
<x:out select="$mybook//name"/>
<x:out select="mybook/books/book/name"/>
<x:out select ="mybook/books/book/price"/>
<x:set var="test">
<books>
<book id="01">
<name>jsp書</name>
<price>23</price>
<authors>
<author>
<name>teee</name>
<adder>ddddd</adder>
</author>
</authors>
</book>
</books>
</x:set>
<x:parse var="txml" xml="${test}"/>
<x:out select="txml/books/name"/>
xml流程控制
<x:if>
<x:choose>
<x:when>
<x:otherwise>
<x:forEach>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>實例</title>
</head>
<body bgcolor="#FFFFFF">
<h3>Parse / ForEach</h3>
<x:parse var="document">
<GetAllBooks>
<book id="1234">
<name>JSP應(yīng)用</name>
<publisher>出版社</publisher>
<price>xxx</price>
<category>計算機</category>
<description>JSP書</description>
<authors>
<author id="1">
<name>asiapower</name>
<address>address:xxxx1</address>
</author>
<author id="2">
<name>hellking</name>
<address>address:xxxx2</address>
</author>
</authors>
</book>
</GetAllBooks>
</x:parse>
<x:forEach select="$document/GetAllBooks">
-> <x:out select="."/>
<br>
</x:forEach>
<hr/>
<x:forEach select="$document//book">
->
<x:if select=".//author">
<x:out select=".//author/name"/>
</x:if>
<br/>
</x:forEach>
<hr>
<x:forEach select="$document//book">
->
<x:choose>
<x:when select='$document//author[@id="2"]'>
author id=2,<x:out select='$document//author[@id="2"]'/>
</x:when>
<x:otherwise>
不是 id=2
</x:otherwise>
</x:choose>
<br/>
</x:forEach>
</body>
</html>
SQL相關(guān)標簽
<sql:setDataSource> 用于設(shè)定數(shù)據(jù)源 ,還可以指其范圍
<sql:stDataSource [datasource="datasource"] url="jdbcurl" [driver="driverClassName"] user="username" password="password" var="varname" scope="">
例:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>sql datasource</title>
</head>
<body bgcolor="#FFFFFF">
創(chuàng)建普通的數(shù)據(jù)源:<br>
<sql:setDataSource
var="example1"
driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev"
user="bn"
password="bn"
/>
創(chuàng)建普通的數(shù)據(jù)源,把用戶名和密碼寫在url中:<br>
<sql:setDataSource
var="example2"
driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev;user=bn;password=bn"
/>
獲得一個數(shù)據(jù)源。<br>
<sql:setDataSource
var="example3"
dataSource="jdbc/bn"
/>
<hr>
使用第一個數(shù)據(jù)源:<hr>
<sql:query var="query1" dataSource="${example1}">
SELECT * FROM contact
</sql:query>
<table border="1">
<c:forEach var="row" items="${query1.rows}">
<tr>
<td>Name: <c:out value="${row.userName}"/></td>
<td>Value: <c:out value="${row.mobile}"/></td>
</tr>
</c:forEach>
</table>
使用第二個數(shù)據(jù)源:<hr>
<sql:query var="query2" dataSource="${example2}">
SELECT * FROM contact
</sql:query>
<table border="1">
<c:forEach var="row" items="${query2.rows}">
<tr>
<td>Name: <c:out value="${row.userName}"/></td>
<td>Value: <c:out value="${row.mobile}"/></td>
</tr>
</c:forEach>
</table>
使用第三個數(shù)據(jù)源:<hr>
<sql:query var="query3" dataSource="${example3}">
SELECT * FROM contact
</sql:query>
<table border="1">
<c:forEach var="row" items="${query3.rows}">
<tr>
<td>Name: <c:out value="${row.userName}"/></td>
<td>Value: <c:out value="${row.mobile}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
<sql:query>
語法:
<sql:query var ="varname" scope=" " datasource="" maxRows=" startRow=""/>
<sql:query var ="varname" scope=" " datasource="" maxRows=" startRow="">
<sql:param
</sql:query>
例:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>標準標簽查詢</title>
</head>
<body bgcolor="#FFFFFF">
創(chuàng)建普通的數(shù)據(jù)源:<br>
<sql:setDataSource
var="example"
driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev"
user="bn"
password="bn"
scope="session"
/>
第一種查詢:<hr>
<sql:query var="query" dataSource="${example}">
SELECT * FROM contact
</sql:query>
<table border="1">
<c:forEach var="row" items="${query.rows}">
<tr>
<td>Name: <c:out value="${row.userName}"/></td>
<td>mobile: <c:out value="${row.mobile}"/></td>
</tr>
</c:forEach>
</table>
<hr>
第2種查詢:<hr>
<sql:query var="query2" sql="SELECT * FROM contact where userName=?" dataSource="${example}">
<sql:param value="asiapower"/>
</sql:query>
<table border="1">
<c:forEach var="row" items="${query2.rows}">
<tr>
<td>Name: <c:out value="${row.userName}"/></td>
<td>mobile: <c:out value="${row.mobile}"/></td>
</tr>
</c:forEach>
</table>
<hr>
第3種查詢:<hr>
<sql:query var="query3" dataSource="${example}">
SELECT * FROM contact where userName=?
<sql:param value="hellking"/>
</sql:query>
<table border="1">
<c:forEach var="row" items="${query3.rows}">
<tr>
<td>Name: <c:out value="${row.userName}"/></td>
<td>mobile: <c:out value="${row.mobile}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
<sql:update>數(shù)據(jù)庫的更新
語法:
<sql:update sql="" datasource=" " var="varname" scope=""/>
<sql:update sql="" datasource=" " var="varname" scope="">
<sql:param>
</sql:update>
例:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>標準標簽的使用</title>
</head>
第1種更新:更新記錄值1<hr>
<sql:update var="update1" dataSource="${example}">
update contact set mobile='13688888' where userName='asiapower'
</sql:update>
<hr>
第2種更新:更新記錄值2<hr>
<sql:update var="update2" sql="update contact set mobile=? where userName=?" dataSource="${example}">
<sql:param value="13999999"/>
<sql:param value="hellking"/>
</sql:update>
<hr>
第3種更新:更新記錄值3<hr>
<sql:update var="update3" dataSource="${example}">
update contact set mobile=? where userName=?
<sql:param value="1399888"/>
<sql:param value="chenzhanjun"/>
</sql:update>
第4種更新:創(chuàng)建表<hr>
<sql:update var="update4" sql="create table test_temp(test varchar(20))" dataSource="${example}"/>
第5種更新:增加記錄
<sql:update var="update5" sql="insert into test_temp values('hellking')" dataSource="${example}"/>
第6種更新:刪除記錄<hr>
<sql:update var="update6" sql="delete from test_temp where test='hellking'" dataSource="${example}"/>
第7種更新:刪除表<hr>
<sql:update var="update7" sql="drop table test_temp" dataSource="${example}"/>
</body>
</html>
<sql:param>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>param的使用</title>
</head>
<sql:setDataSource
var="example"
dataSource="jdbc/bn"
/>
執(zhí)行數(shù)據(jù)添加操作:<hr>
<sql:update var="update" dataSource="${example}">
insert into contact (userName,mobile,phone,mail)values(?,?,?,?)
<sql:param>wyy</sql:param>
<sql:param>13634234</sql:param>
<sql:param>010213423434</sql:param>
<sql:param>wyy@xtom.com</sql:param>
</sql:update>
執(zhí)行更新操作:<hr>
<sql:update var="update2" sql="update contact set mobile=? where userName=?" dataSource="${example}">
<sql:param value="13999999"/>
<sql:param value="hellking"/>
</sql:update>
</body>
</html>