JSTL详解

来源:互联网 发布:好源码 编辑:程序博客网 时间:2024/04/28 03:45


JSTL详解(一)
一般用途的标签

在JSTL中,一般用途的标签只要是指具有输出,设置变量,和错误处理等功能的标签,他们在jsp中使用很频繁,它们有:

Java代码 复制代码
  1. l         <c:out>   
  2. 2         <c:set>   
  3. 3         <c:remove>   
  4. 4         <c:catch>  

下面,我来讲述一下他们的作用:

<c:out>
它是一个输出的标签,负责把计算出的结果输出到jspwriter,就跟调用out.println()基本一样。没有BODY的时候,使用下面的语法:
Java代码 复制代码
  1. <c:out value=”value”  [escapeXml]=”{true|false}”  [default=”defaultValue”]/>  

有BODY的时候,使用下面的语法:
Java代码 复制代码
  1. <c:out value=”value”  [escapeXml]=”{true|false}” >   
  2.        这里是BODY,可以是任何合法的jsp或者是html代码。   
  3. </c:out>  

注意,escapeXml的作用是是否将代码交给xml解析器解释,true为交给xml解析器解释,false为交给浏览器解释,见红色部分代码
Java代码 复制代码
  1. 举例:c_out.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  4. <html>   
  5. <head>   
  6.   <title>JSTL: c:out的使用</title>   
  7. </head>   
  8. <body bgcolor="#FFFFFF">   
  9. <hr>   
  10. <c:set var="sessionAttr" value="sessionValue" scope="session"/>   
  11. <c:set var="pageAttr" value="pageValue" scope="page"/>   
  12. <c:set var="requestAttr" value="requestValue" scope="request"/>   
  13. <c:out value="以下输出的是前面设置的属性<br>" [color=red]escapeXml="false"[/color]/>    
  14. <c:out value="${sessionAttr}"/>   
  15. <c:out value="${pageAttr}"/>   
  16. <c:out value="${requestAttr}"/>   
  17. <c:out value="${test_nodef}" default="没有test_nodef这个变量"/>   
  18. </html>  


<c:set>
这个标签用于在某个范围(page,request,session,application)里面设置特定的值(默认为page),或者设置某个已经存在的javabean的属性。类似于<%request.setAttribute(“name”,value)%>
语法:
1、使用value属性设置一个特定范围里面的属性:
<c:set value=”value”  var=”varName” [scope= “{page|request|session|application}”]/ >
2、使用value属性设置一个特定范围里面的属性,带有BODY:
<c:set value=”value”  var=”varName” [scope= “{page|request|session|application}”] >
       Body content
</c:set>
3、设置某个特定对象的一个属性:
<c:set value=”value”  target=”target”  property=”propertyName”/ >
4、设置某个特定对象的一个属性, 带有BODY:
<c:set value=”value”  target=”target”  property=”propertyName” >
       Body content
</c:set>
Java代码 复制代码
  1. 举例:c_set.jsp    
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  4. <jsp:useBean id="user" class="com.j2ee14.ch12.User"/>   
  5. <html>   
  6. <head>   
  7.   <title>JSTL:的使用c:set</title>   
  8. </head>   
  9. <body bgcolor="#FFFFFF">   
  10. <hr>   
  11. 设置一个属性,然后输出它<br>   
  12. <c:set var="maxCount" value="100"/>   
  13. <c:out value="${maxCount}"/>   
  14. <hr>设置属性时,把它的值放在标签的body中。   
  15. <c:set var="password">   
  16. ksdjfxsdf234234   
  17. </c:set>   
  18. <c:out value="${password}"/>   
  19. <hr>设置javaBean的属性,然后输出这些属性值:   
  20. <c:set value="hellking" target="${user}" property="userName"/>   
  21. userName=<c:out value="${user.userName}"/>   
  22. <hr>设置属性,并且指定它们的范围,属性的默认范围是page。   
  23. <c:set value="20" var="maxIdelTime" scope="session"/>   
  24. <c:set value="next.jsp" var="nextPage" scope="page"/>   
  25. </body>   
  26. </html>  


<c:remove>
它的作用是删除某个变量或者属性。类似于<%session.removeAttribute(“name”)%>,它的语法是:
<c:remove var=”varName” [scope= “{page|request|session|application}”]/ >
Java代码 复制代码
  1. 举例:c_remove.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  4. <html>   
  5. <head>   
  6.   <title>JSTL:c:remove的使用</title>   
  7. </head>   
  8. <body bgcolor="#FFFFFF">   
  9. <c:set value="10000" var="maxUser" scope="application"/>   
  10. <c:set value="10" var="count" scope="session"/>   
  11. maxUser=<c:out value="${maxUser}"/><br>   
  12. count=<c:out value="${count}"/>   
  13. <hr>调用c:remove...   
  14. <c:remove var="maxUser" scope="application"/>   
  15. <c:remove var="count"/>   
  16. 调用了c:remove后,参数值为:   
  17. maxUser=<c:out value="${maxUser}"/>,   
  18. count=<c:out value="${count}"/>,   
  19. </body>   
  20. </html>  


JSTL详解(二)

<c:catch>
它的作用是捕捉由嵌套在它里面的标签所抛出来的异常。类似于<%try{}catch{}%>,它的语法是:
<c:catch [var=”varName”]>nested actions</c:catch>

Java代码 复制代码
  1. 举例:c_catch.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  4. <html>   
  5. <head>   
  6.   <title>JSTL:catch的使用</title>   
  7. </head>   
  8. <body bgcolor="#FFFFFF">   
  9. <c:catch var="error">   
  10. <%   
  11.   Integer.parseInt("sdkfj");   
  12.  %>   
  13.  </c:catch>   
  14.  <hr>异常:   
  15.  <c:out value="${error}"/>   
  16.  <hr>异常 exception.getMessage=   
  17.  <c:out value="${error.message}"/>   
  18. <hr> 异常exception.getCause=   
  19.  <c:out value="${error.cause}"/>   
  20. </body>   
  21. </html>   


JSTL详解(三)
条件标签包括以下几种:
l         <c:if>;
2         <c:choose>;
3         <c:when>;
4         <c:otherwise>;
下面,我来分别介绍:

l.<c:if>; 它用来做条件判断,功能类似jsp中的<%if(boolean){}%>
语法:
1、无body的情况
<c:if test=”testCondition” var=”varName” [scope=“{page|request|session|application}”]/>
2、有body的情况
<c:if test=”testCondition” var=”varName” [scope=“{page|request|session|application}”]>
   Body内容
</c:if>

Java代码 复制代码
  1. 举例:c_if.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  4. <html>   
  5. <head>   
  6.   <title>JSTL:c:if的使用</title>   
  7. </head>   
  8. <body bgcolor="#FFFFFF">   
  9. <c:set var="count" value="100"/>   
  10. <c:if test="${count>78}">   
  11.   count>78  
  12. </c:if>   
  13. </body>   
  14. </html>  


2.<c:choose>;  它是用于条件选择,和<c:when>,<c:otherwise>一起使用,它的条件选择是排斥性的,相当于jsp中的<%switch(i){case 0: … case 1:… default :}%>,也可以完成<%if{}…else if{}….else if{} else{} %>的功能
语法:<c:choose>
             Body内容(<c:when>,<c:otherwise>)
      </c:choose>

注意:它的body内容只能是由以下的元素组成:
1.空格;
2.0个或多个<c:when>子标签,<c:when>必须在<c:otherwise>子标签之前出现;
3.0个或多个<c: otherwise >子标签

<c:when> 它是<c:choose>的一个分支
语法:
<c:when test=”testCondition”>
   Body内容
</c:when>
注意:它有两个限制:
1.必须以<c:choose>作为它的父标签
2.必须在<c:otherwise>标签之前出现

<c:otherwise>他代表了<c:choose>的最后选择相当于jsp的<%switch(i){case 0: … case 1:… default :}%> 中的default语句。
语法:
<c:otherwise>
   Conditional block
</c:otherwise>
它的使用有两个限制:
1.必须以<c:choose>作为它的父标签;
2.必须以<c:choose>的最后分支方式出现;

<c:choose>,<c:otherwise>,<c:when>
Java代码 复制代码
  1. 举例:c_choose.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  4. <c:set var="count" value="100"/>   
  5. <html>   
  6. <head>   
  7.   <title>JSTL:c:choose的使用</title>   
  8. </head>   
  9. <body bgcolor="#FFFFFF">   
  10. <c:choose>   
  11.     <c:when test="${count <=0}">   
  12.       <font color="blue">   
  13.     </c:when>   
  14.     <c:when test="${count<=60&&count>0}">   
  15.       <font color="red">   
  16.     </c:when>   
  17.     <c:otherwise>   
  18.       <font color="green">    
  19.     </c:otherwise>          
  20.   </c:choose>   
  21. count的值是:<c:out value="${count}"/>   
  22. </font>   
  23. </body>   
  24. </html>  


JSTL详解(四)

迭代标签的使用
如果使用scriptlets,那么,我们经常使用Iterator或者Enumeration来进行迭代,如:
       Iterator it = collection.iterator();
       while(it.hasNext()){
              SomeBean someBean = (SomeBean)it.next();
              out.println(someBean.getXXX());
              …………..
}

在前面的一次中,我们开发过一个迭代标签,JSTL中也提供了对迭代进行支持的标签,并且,他的功能比上一章的强大的多。JSTL中的迭代标签有两个:
1.<c:forEach>;
2.<c:forTokens>;

1、<c:forTokens>专门处理TokenString的迭代,可以指定一个或者是多个分隔符。由于他们使用的很少,在这里就不介绍了。
2、<c:forEach>是最常用的,他几乎能够完成所有的迭代任务,就象jsp中的for(int i=j;i<k;i++)下面我来详细介绍。(注意,“[]”里面的是可选条件)

语法:a、在Collection中迭代:
       <c:forEach [var=”varName”] items=”collection” [varStatus=”varStatusName”] [begin=”begin”] [end=”end”] [step=”step”]>
       Body 内容
       </c:forEach>

      b、迭代固定的次数:
       <c:forEach [var=”varName”] [varStatus=”varStatusName”]  begin=”begin” end=”end” [step=”step”]>
       Body 内容
       </c:forEach>

Java代码 复制代码
  1. 举例:c_forEach1.jsp(在Collection中迭代)   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>    
  4. <%@ page import="java.util.*,com.j2ee14.ch12.User"%>    
  5. <%    
  6.    Collection users_c=new ArrayList();    
  7.    for(int i=0;i<3;i++)    
  8.    {    
  9.       User user=new User();    
  10.       user.setUserName("foo"+i);    
  11.       user.setPassword("foo"+i);          
  12.       users_c.add(user);    
  13.    }    
  14.    session.setAttribute("users",users_c);    
  15. %>    
  16. <html>    
  17. <head>    
  18.   <title>JSTL:c:forEach的使用之一</title>    
  19. </head>    
  20. <body bgcolor="#FFFFFF"><center>    
  21. <h4>迭代某个collection中的元素。</h4>    
  22. <table border=1>    
  23. <tr><td>用户名</td><td>密码</td></tr>    
  24. <c:forEach var="users" items="${users}">    
  25.   <tr>    
  26.   <td><c:out value="${users.userName}"/></td>    
  27.   <td><c:out value="${users.password}"/></td>    
  28.   </tr>    
  29. </c:forEach>    
  30. </table>      
  31. </center>   
  32. </body>    
  33. </html>  



Java代码 复制代码
  1. 举例:c_forEach2.jsp(迭代固定的次数)   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>    
  4. <html>    
  5. <head>    
  6.   <tcounttle>JSTL:c:forEach的使用之二</tcounttle>    
  7. </head>    
  8. <body bgcolor="#FFFFFF"><center>    
  9. <h4>第二种迭代:5060</h4>    
  10. <c:forEach var="count" begin="50" end="60">    
  11.   <c:out value="${count}"/> **    
  12. </c:forEach>    
  13.   
  14. <h4>第二种迭代:10 to 100,step=10</h4>    
  15. <c:forEach var="count" begin="10" end="100" step="10">    
  16.   <c:out value="${count}"/>**    
  17. </c:forEach>    
  18. </center>    
  19. </body>    
  20. </html>  



JSTL详解(五)

首先,感谢大家的关注,下面我来继续讲解:URL相关的标签,他们的作用主要是负责页面的导航、重定向、资源的获得以及参数的传递等等,他们有:
1.<c:import>
2.<c:redirect>
3.<c:url>
4.<c:param>

<c:import>
作用:导入一个url的资源,相当于jsp 中的<jsp:include page=”path”>标签,同样也可以把参数传递到被导入的页面。
语法:a、资源的内容使用string对象向外暴露
<c:import url=”url” [context=”context”] [var=”varName”] [scope=”{page|request|session|application}”] [charEncoding=”charEncoding”]>
Optional body content for <c:param> subtags
</c:import>

b、资源的内容使用redirect对象向外暴露
<c:import url=”url” [context=”context”] varReader=”varReaderName” [charEncoding=”charEncoding”]>
Body content where varReader is consumed by another action
</c:import>

Java代码 复制代码
  1. 举例:c_import.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  4. <html>   
  5. <head>   
  6.   <title>JSTL:c:import的使用</title>   
  7. </head>   
  8. <body bgcolor="#FFFFFF">   
  9. <h3>绝对路径 URL</h3>   
  10. <blockquote>   
  11. <ex:escapeHtml>   
  12.   <c:import url="http://127.0.0.1:8080/ch12/footer.jsp"/>   
  13. </ex:escapeHtml>   
  14. </blockquote>   
  15.   
  16. <h3>相对路径并且传递参数到指定的URL</h3>   
  17. <blockquote>   
  18.   <c:import url="footer.jsp" charEncoding="gb2312">   
  19.   <c:param name="userName" value="hellking"/>   
  20.  </c:import>     
  21. </blockquote>   
  22. </body>   
  23. </html>  


<c:redirect>
作用:把客户的请求发送到另一个资源,相当于jsp中的<% request.sendRedirect(“other.jsp”)%>或者servlet中的RequestDispatch.forward(“other.jsp”)的功能。
语法:a、没有body的情况
      <c:redirect url=”value” [context=”context”]/>
      b、有body,在body 中查询指定的参数
      <c:redirect url=”value” [context=”context”]>
         <c:param> subtags
      </c:redirect>
Java代码 复制代码
  1. 举例:c:redirect.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  4. <html>   
  5. <head>   
  6.   <title>JSTL:c:redirect的使用</title>   
  7. </head>   
  8. <body bgcolor="#cc99cc">   
  9. <c:url value="footer.jsp" var="nextpage">   
  10.    <c:param name="userName" value="hellking"/>   
  11. </c:url>   
  12. <c:redirect url="${nextpage}"/>   
  13. </body>   
  14. </html>  


<c:url>
作用:用于构造URL,主要的用途是URL的重写。
语法:a、没有body的情况
<c:url value=”value” [context=”context”] [var=”varName”] [scope=”{page|request|session|application}”]/>
      b、有body ,并在body 中有重写的参数
<c:url value=”value” [context=”context”] [var=”varName”] [scope=”{page|request|session|application}”]>
<c:param> subtags
</c:url>

Java代码 复制代码
  1. 举例:c_url.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  4. <html>   
  5. <head>   
  6.   <title>JSTL c:url 的使用</title>   
  7. </head>   
  8. <body bgcolor="#FFFFFF">   
  9. <c:url var="footer" value="footer.jsp" scope="page">   
  10.        <c:param name="id" value="hellking"/>   
  11. </c:url>   
  12.   
  13. <c:out value="${footer}"/>   
  14. <br>另一种没有参数的URL<br>   
  15. <c:url value="footer.jsp"/>   
  16. </body>   
  17. </html>  




<c:param>
作用:它是在<c:import>,<c:redirectt>,<c:url>中添加请求的参数。和一般的参数没什么区别。
语法:a、参数的值使用value属性指定
<c:param name=”name” value=”value”/>
     b、参数的值在body 中指定
<c:param name=”name”>
   参数值
</c:param>

Java代码 复制代码
  1. 举例:c_param.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>   
  3. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  4. <html>   
  5. <head>   
  6.   <title>JSTL c:param的使用</title>   
  7. </head>   
  8. <body bgcolor="#FFFFFF">   
  9. <c:redirect url="footer.jsp">   
  10.    <c:param name="userName">   
  11.    hellking   
  12.    </c:param>   
  13. </c:redirect>   
  14. </body>   
  15. </html>  


JSTL详解(六) 格式化标签
<fmt: formatNumber value =”12.3” pattern=”.000”/>
将输出 12.300. 应用样式 ”.000”, 将使格式化后的小数部分有 3 位。不足 3 位将以 0 补齐。
<fmt:formatDate value=”<%=new java.util.Date() %>” type=”date”  pattern="yyyy/M/d" />
格式化的结果是: 2007-5-27 .
<fmt:formatDate value=”<%=new java.util.Date() %>” type=”time” pattern="hh:mm:ss"/>
格式化的结果是: 9:25:11
<fmt:formatDate value=”<%=new java.util.Date() %>” type=”both” />
格式化的结果是: 2007-5-27 9:25:11
Java代码 复制代码
  1. 举例:详细的使用如下例   
  2. <c:set var="now" value="<%=new java.util.Date()%>" />   
  3. <table border="1" cellpadding="0" cellspacing="0"  
  4.     style="border-collapse: collapse" bordercolor="#111111" width="63%" id="AutoNumber2">   
  5.       <tr>   
  6.         <td width="100%" colspan="2" bgcolor="#0000FF">   
  7.           <p align="center">   
  8.             <b><font color="#FFFFFF" size="4">   
  9.         Formatting:<fmt:formatDate value="${now}" type="both" timeStyle="long" dateStyle="long" />   
  10.               </font>   
  11.             </b>   
  12.           </p>   
  13.         </td>   
  14.       </tr>   
  15.       <tr>   
  16.         <td width="51%">formatDate type="time"</td>   
  17.         <td width="49%">   
  18.           <fmt:formatDate type="time" value="${now}" />   
  19.         </td>   
  20.       </tr>   
  21.       <tr>   
  22.         <td width="51%">type="date"</td>   
  23.         <td width="49%"><fmt:formatDate type="date" value="${now}" /></td>   
  24.       </tr>   
  25.       <tr>   
  26.         <td width="51%">type="both"</td>   
  27.         <td width="49%"><fmt:formatDate type="both" value="${now}" /></td>   
  28.       </tr>   
  29.       <tr>   
  30.         <td width="51%">type="both" dateStyle="default" timeStyle="default"</td>   
  31.         <td width="49%">   
  32.           <fmt:formatDate type="both" dateStyle="default" timeStyle="default" value="${now}" />   
  33.         </td>   
  34.       </tr>   
  35.       <tr>   
  36.         <td width="51%">type="both" dateStyle="short" timeStyle="short"</td>   
  37.         <td width="49%">   
  38.           <fmt:formatDate type="both" dateStyle="short" timeStyle="short" value="${now}" />   
  39.         </td>   
  40.       </tr>   
  41.       <tr>   
  42.         <td width="51%">type="both" dateStyle="medium"  timeStyle="medium"</td>   
  43.         <td width="49%">   
  44.           <fmt:formatDate type="both" dateStyle="medium"  timeStyle="medium" value="${now}" />   
  45.         </td>   
  46.       </tr>   
  47.       <tr>   
  48.         <td width="51%">type="both" dateStyle="long"   timeStyle="long"</td>   
  49.         <td width="49%">   
  50.           <fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${now}" />   
  51.         </td>   
  52.       </tr>   
  53.       <tr>   
  54.         <td width="51%">type="both" dateStyle="full" timeStyle="full"</td>   
  55.         <td width="49%">   
  56.           <fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${now}" />   
  57.         </td>   
  58.       </tr>   
  59.       <tr>   
  60.         <td width="51%">pattern="yyyy-MM-dd"</td>   
  61.         <td width="49%"><fmt:formatDate pattern="yyyy-MM-dd" value="${now}" /></td>   
  62.       </tr>   
  63.     </table>  

 



补充知识点:
1. 替换 request.getParameter("test"):
<c:if test="${param.test!=null}" >
<c:out value="${param.test}" />
</c:if>

2. <c:redirect url="a.jsp">

3.<c:redirect url="/max.jsp" context="/ch16">
<c:param name="name1" value="665"/>
<c:param name="name3" value=" 斯蒂芬 "/>
</c:redirect>

4.<c:forTokens items="zhangsan:lisi:as" delims=":" var="name">
${name}
</c:forTokens>



JSTL详解(七) SQL相关的标签
今天,我来讲解下一些很重要的jstl标签,如果运用得当,可以大大的简化数据库的操作,减少代码量。首先sql标签可以实现包括查询、更新、事务处理和设置数据源等强大的功能。下面分别讲述。
<sql:setDataSource>
<sql:query>
<sql:update>
<sql:transaction>
<sql:param>


<sql:setDataSource>
语法:<sql:setDataSource>用来设置数据源,可以通过scope设置使用范围,如page、application、session等。有两种方式可以设置数据源,一种是直接使用在web中配置的数据源,只要指定jndi名就可以了(这个就不要我多讲了吧,呵呵),第二种是指定所有的连接属性。
     <sql:setDataSource
     {dataSource=”dataSourceName” |   url=”jdbcurl”
     [driver=”driverClassName”]
     [user=”userName”]
     [password=”password”]
     [var=”varName”]
     [scope=”{page|session|request|application}”]/>

Java代码 复制代码
  1. 举例:sql_datasource.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>   
  3. <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>   
  4. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  5. <html>   
  6. <head>   
  7.   <title>JSTL:<sql:setDataSource>的使用</title>   
  8. </head>   
  9. <body bgcolor="#FFFFFF">   
  10. 创建普通的数据源:<br>   
  11. <sql:setDataSource   
  12.   var="ds1"  
  13.   driver="com.mysql.jdbc.Driver"  
  14.   url="jdbc:mysql://localhost:3306/jstlTest"  
  15.   user="jstl"  
  16.   password="jstl"  
  17. />   
  18. 从jndi名称空间中获得一个数据源。<br>   
  19. <sql:setDataSource  var="ds2"  dataSource="jdbc/ds2"/>   
  20. </body>   
  21. </html>  



<sql:query>
作用:不用说都知道了,数据库操作中最频繁的查询。
语法:a、没有body
         <sql:query sql=”sqlQuery”
         Var=”varName” [scope=”{page|session|request|application}”]
         [dataSource=”dataSourceName”]
         [maxRows=”maxRows”]
         [startRow=”startRow”] />
      b、有一个body ,并在body 中指定了查询需要的参数
         <sql:query sql=”sqlQuery”
         Var=”varName” [scope=”{page|session|request|application}”]
         [dataSource=”dataSourceName”]
         [maxRows=”maxRows”]
         [startRow=”startRow”] >
         <sql:param> actions
         </sql:query>

      c、有body,并且可以指定可选的参数(< sql:param >在后面讲述)
         <sql:query sql=”sqlQuery”
         Var=”varName” [scope=”{page|session|request|application}”]
         [dataSource=”dataSourceName”]
         [maxRows=”maxRows”]
         [startRow=”startRow”] >
         query
         optional<sql:param> actions
         </sql:query>

Java代码 复制代码
  1. 举例:sql_query.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  3. <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>   
  4. <%@ page contentType="text/html; charset=gb2312" language="java" %>   
  5. <html>   
  6. <head>   
  7.   <title>JSTL:<sql:query>的使用</title>   
  8. </head>   
  9. <body bgcolor="#FFFFFF">   
  10. <sql:setDataSource  var="ds2"  dataSource="jdbc/ch12"/>   
  11. 第一种查询:<hr>   
  12. <sql:query var="query" dataSource="${ds2}">   
  13.     SELECT * FROM sql_test   
  14. </sql:query>   
  15. <table border="1">   
  16.   <c:forEach var="row" items="${query.rows}">   
  17.   <tr>   
  18.     <td>Name: <c:out value="${row.id}"/></td>   
  19.     <td>mobile: <c:out value="${row.power}"/></td>   
  20.   </tr>   
  21.   </c:forEach>   
  22. </table>   
  23.   
  24. <hr>   
  25. 2种查询:<hr>   
  26. <sql:query var="query2" sql="SELECT * FROM sql_test where id=?" dataSource="${ds2}">   
  27.    <sql:param value="01"/>   
  28. </sql:query>   
  29. <table border="1">   
  30.   <c:forEach var="row" items="${query2.rows}">   
  31.   <tr>   
  32.     <td>Name: <c:out value="${row.id}"/></td>   
  33.     <td>mobile: <c:out value="${row.power}"/></td>   
  34.   </tr>   
  35.   </c:forEach>   
  36. </table>   
  37. </body>   
  38. </html>  


JSTL详解 (八)SQL相关的标签(二)
<sql:update>
作用:主要是执行插入、更新和删除操作的标签。另外,还有一些没有返回结果集的sql操作也可以使用这个标签。比如
create table tableName(id int….);
drop table tableName
grant tableName   等等。。
语法:a、没有body的情况
<sql:update sql=”sqlUpdate”
[dataSource=”dataSource”]
[var=”varName”]
[scope=”{page|session|request|application}”]/>
      b、有body ,并且在body中指定参数
<sql:update sql=”sqlUpdate”
[dataSource=”dataSource”]
[var=”varName”]
[scope=”{page|session|request|application}”] >
<sql:param>  actions
</sql:update>
     c、有body,在body中指定sql语句和可选的参数
<sql:update sql=”sqlUpdate”
[dataSource=”dataSource”]
[var=”varName”]
[scope=”{page|session|request|application}”] >
Update statement
<sql:param>  actions
</sql:update>

Java代码 复制代码
  1. 举例:sql_update.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
  3. <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>    
  4. <%@ page contentType="text/html; charset=gb2312" language="java" %>    
  5. <html>    
  6. <head>    
  7.   <title>JSTL:的使用</title>    
  8. </head>    
  9. <sql:setDataSource   var="ds2"   dataSource="jdbc/ch12" />    
  10. 更新记录值1<hr>    
  11. <sql:update var="update1" dataSource="${ds2}">    
  12.     update sql_test set power='low' where id=01    
  13. </sql:update>    
  14.   
  15. 2种更新:创建表<hr>    
  16. <sql:update var="update4" sql="create table sql_temp_901(test varchar(20))" dataSource="${ds2}"/>      
  17.   
  18. 3种更新:增加记录    
  19. <sql:update var="update5" sql="insert into sql_temp_901 values('hellking')" dataSource="${ds2}"/>    
  20.   
  21. 4种更新:删除记录<hr>    
  22. <sql:update var="update6" sql="delete from sql_temp_901 where test='hellking'" dataSource="${ds2}"/>      
  23.   
  24. 5种更新:删除表<hr>    
  25. <sql:update var="update7" sql="drop table sql_temp_901" dataSource="${ds2}"/>    
  26. </body>    
  27. </html>  


<sql:transaction>
作用:用语事务处理,它为<sql:query>和<sql:update>建立事务处理上下文,并且提供相同的数据源。
语法:<sql:transation [dataSource=”dataSourceName”]>
      [isolation=”isolationLevel”]>
      <sql:query> and <sql:update> statement
      </sql:transation>
      isolationLevel ::=”read_committed”
      |”read_uncommitted”
      |”repeatable_read”
      |”serializable”

注意:DataSource属性和其他标签的DataSource属性一样,isolactionlevel属性指定事务隔离的级别,如果没有指定它的值,那么就使用自己配置的DataSource事务隔离级别。而且,嵌套在里面的<sql:query> 和 <sql:update>标签不能再指定其他的数据源。<sql:transaction>
是数据库封装的轻量级事务处理,如果你需要复杂的事务处理或者分布试处理,它就不适合。
Java代码 复制代码
  1. 举例:sql_transaction.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
  3. <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>    
  4. <%@ page contentType="text/html; charset=gb2312" language="java" %>    
  5. <html>    
  6. <head>    
  7.   <title>JSTL:sql:transaction的使用</title>    
  8. </head>    
  9. <sql:setDataSource   var="ds2"  dataSource="jdbc/ch12" />    
  10. <h2>使用事务处理方式创建一个表:</h2>    
  11. <sql:transaction dataSource="${ds2}">    
  12.   <sql:update var="test_temp902">    
  13.     create table test_temp902 (    
  14.       id int primary key,    
  15.       name varchar(80)    
  16.     )    
  17.   </sql:update>    
  18.   <sql:update sql="insert into test_temp902 values(01,'wyy')"/>    
  19.   <sql:update sql="update sql_test set power='wyy' where id=01"/>    
  20. </sql:transaction>    
  21. </body>    
  22. </html>  


<sql:param>
作用:设置sql语句中“?”表示的占位符号的值。
语法:a、没有body,使用value指定。
         <sql:param value=”value”/>
      b、在body中指定
         <sql:param>
         参数值
         </sql:param>

Java代码 复制代码
  1. 举例:sql_param.jsp   
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
  3. <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>    
  4. <%@ page contentType="text/html; charset=gb2312" language="java" %>    
  5. <html>    
  6. <head>    
  7.   <title>JSTL:sql:param的使用</title>    
  8. </head>    
  9. <sql:setDataSource    
  10.   var="example"    
  11.   dataSource="jdbc/ch12"    
  12. />    
  13. 执行更新操作:<hr>    
  14. <sql:update var="update2" sql="update sql_test set power=? where id=?" dataSource="${example}">    
  15.    <sql:param value="high"/>    
  16.    <sql:param value="01"/>    
  17. </sql:update>    
  18. </body>    
  19. </html>  


JSTL详解(九) fn标签使用说明

    在jsp页面上经常遇到得到集合长度、字符长度、字符切取等应用需,在2.0以前这种需是许多程序员对JSTL及为不满意的地方之一。为此在2.0 中添加了functions标签,其提供对以上需求的支持。
使用方法     引用<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>


函数说明:    
函数描述fn:contains(string, substring)如果参数string中包含参数substring,返回truefn:containsIgnoreCase(string, substring)如果参数string中包含参数substring(忽略大小写),返回true fn:endsWith(string, suffix)如果参数 string 以参数suffix结尾,返回true fn:escapeXml(string)将有特殊意义的XML (和HTML)转换为对应的XML character entity code,并返回 fn:indexOf(string, substring)返回参数substring在参数string中第一次出现的位置 fn:join(array, separator)将一个给定的数组array用给定的间隔符separator串在一起,组成一个新的字符串并返回。 fn:length(item)返回参数item中包含元素的数量。参数Item类型是数组、collection或者String。如果是String类型,返回值是String中的字符数。fn:replace(string, before, after)返回一个String对象。用参数after字符串替换参数string中所有出现参数before字符串的地方,并返回替换后的结果 fn:split(string, separator)返回一个数组,以参数separator 为分割符分割参数string,分割后的每一部分就是数组的一个元素 fn:startsWith(string, prefix)如果参数string以参数prefix开头,返回true fn:substring(string, begin, end) 返回参数string部分字符串, 从参数begin开始到参数end位置,包括end位置的字符 fn:substringAfter(string, substring)返回参数substring在参数string中后面的那一部分字符串 fn:substringBefore(string, substring)返回参数substring在参数string中前面的那一部分字符串 fn:toLowerCase(string) 将参数string所有的字符变为小写,并将其返回 fn:toUpperCase(string) 将参数string所有的字符变为大写,并将其返回 fn:trim(string)去除参数string 首尾的空格,并将其返回  


例:邮箱省略显示 如smile2008@163.com --->显示成sm******@163.com

${fn:substring(userinfo.email,0,2)}******${fn:substring(userinfo.email,fn:indexOf(userinfo.email, '@'),fn:length(userinfo.email))}
原创粉丝点击