jstl与EL表达式的比较

EL表达式
注:在一个jsp中一定要注意
<%@ page isELIgnored="true|false" %>
1,可以访问一个简单的参数
userName是一个参数
${userName}
2, 访问一个嵌套的参数
${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 把请求中的参数名和单个值进行映射
3) paramValues 把请求中的参数名和一个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包含了和以下操作相关的标签
常用标签 <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) 把计算的结果输出
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中的内容不会发送到客户端
<c:out value="${test}"/>
<c:out value="${notex}" default="如果notex变量不存在,则将显示此信息"/>
2) 用来将某范围(request,session,application等)中设置某个值
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>一起使用
注: 不能单独使用

就象是开关语句 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 与开关语句中的最后选择
</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 迭代固定的次数
<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 相关的标签
就是页面导向,重定向,资源获得,参数传递等相关标签
<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中同时带上相关参数

注:在以上所有标签,在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对象产生的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应用</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相关标签
<sql:setDataSource> 用于设定数据源 ,还可以指其范围
<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">
创建普通的数据源:<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"
/>
创建普通的数据源,把用户名和密码写在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"
/>
获得一个数据源。<br>
<sql:setDataSource
var="example3"
dataSource="jdbc/bn"
/>
<hr>
使用第一个数据源:<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>
使用第二个数据源:<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>

使用第三个数据源:<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">
创建普通的数据源:<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>数据库的更新
语法:
<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种更新:创建表<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"
/>
执行数据添加操作:<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>
执行更新操作:<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>
http://ericfang.iteye.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值