C標籤如何使用

 1.JSP標準標籤庫

       首先下載jstl-1.2.jar包如下圖所示

解壓文件夾下的METN-INF下的tld文件如下所示:

 

將這幾個文件報錯在WEB項目中WEB-INF下面,

 

在demo1.jsp頁面輸入如下所示

  1. <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>   
  2. <body>  
  3.     <h3> <c:out value="Hellofff Mary!"></c:out></h3> 
  4.   </body> 

啓動服務器即可進行輸出Hellofff Mary!

若不想導入

  1. <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%> 

則可在web.xml中設置,代碼如下:

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="3.0"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
  7.   <display-name></display-name>  
  8.    
  9.   <jsp-config> 
  10.     <taglib> 
  11.         <taglib-uri>http://www.mldn.cn/jstl/core</taglib-uri> 
  12.         <taglib-location>/WEB-INF/c.tld</taglib-location> 
  13.     </taglib> 
  14.   </jsp-config> 
  15.    
  16.   <welcome-file-list> 
  17.     <welcome-file>index.jsp</welcome-file> 
  18.   </welcome-file-list> 
  19.    
  20. </web-app> 

此時即可在導入jsp或html頁面中添加

  1. <%@ taglib uri="http://www.mldn.cn/jstl/core" prefix="c"%> 

記住哦!

2.核心標籤庫中主要標籤

 

<c:out>標籤如何使用

 

  1. <body>  
  2.  <% pageContext.setAttribute("info","馬開元的信息"); 
  3.   %> 
  4.   <h3>屬性存在:<c:out value="${info}"/></h3> 
  5.   <h3>屬性不存在:<c:out value="${info1}" default="沒有此內容"/></h3> 
  6.  </body> 

<c:set>標籤:

 

  1. <c:set var="info3" value="2號" scope="request"></c:set> 
  2.     屬性內容:${info3} 
  3.      
  4.      

則此輸出2號

例2:在項目中src下建立bean包,建立類Simple如下:

 

  1. package bean; 
  2.  
  3. public class Simple { 
  4.     private String id; 
  5.  
  6.     public String getId() { 
  7.         return id; 
  8.     } 
  9.  
  10.     public void setId(String id) { 
  11.         this.id = id; 
  12.     } 

在建立一個demo2.jsp頁面如下

 

  1. <body> 
  2.   <% Simple simple=new Simple(); 
  3.     request.setAttribute("simple",simple); 
  4.    %> 
  5.    <c:set value="3號" target="${simple}" property="id"/> 
  6.    <h3>id值爲,你猜猜是多少??黑黑${simple.id}</h3> 
  7. </body> 

將輸出3號,爲Simple類的id屬性賦值,這在web開發中,一般用type=“hidden”文本進行隱形傳值,用C標籤可以簡化步驟,也可爲其賦值

<c:remove>標籤如下:

 

  1. <c:set var="info3" value="2號" scope="request"></c:set> 
  2.     屬性內容:${info3} 
  3.     <c:remove var="info3" scope="request"/> 
  4.     屬性內容:${info3} 

第二個結果中沒有任何信息

<c:catch>標籤如下:

 

  1. <c:catch var="error"> 
  2.         <%  
  3.         int result=10/0; 
  4.          ;o 
  5.         %> 
  6.     </c:catch> 
  7.      
  8.     <h3>異常信息${error}</h3> 

<c:forEach>標籤使用:

 

  1. <body> 
  2.    <%  
  3.     List all=new ArrayList(); 
  4.     all.add("你好"); 
  5.     all.add("我好"); 
  6.     all.add("他好"); 
  7.     pageContext.setAttribute("pp",all); 
  8.    %> 
  9.    <h3> 
  10.     輸出全部: 
  11.     <c:forEach items="${pp}" var="mem"> 
  12.     ${mem} 
  13.     </c:forEach> 
  14.     </h3> 
  15.  </body> 

循環輸出list中的值,$爲EL表達式,輸出時不需導入任何包,很方便使用,<c:forEach>標籤在新聞網站中,若news中有多條,可以進行循環輸出,代碼方便容易懂

<c:choose>標籤使用:

 

  1. <body> 
  2.   <% pageContext.setAttribute("number",10); %> 
  3.   <c:choose> 
  4.     <c:when test="${num==10}"> 
  5.     number的值是10 
  6.     </c:when> 
  7.      
  8.     <c:when test="${num==20}"> 
  9.     number的值是20 
  10.     </c:when> 
  11.     <c:otherwise> 
  12.         沒一個條件滿足 
  13.     </c:otherwise> 
  14.   </c:choose> 
  15. </body> 

在實際操作中代碼示例如下:

 

  1. <%@ page language="java" import="java.util.*,com.mky.dao.*,com.mky.factory.*,com.mky.bean.*,com.mky.util.*" pageEncoding="gb2312"%> 
  2. <%@page import="java.sql.*" %> 
  3. <%@ taglib prefix="c" uri="/WEB-INF/c.tld"%> 
  4. <
  5.     String path = request.getContextPath(); 
  6.     String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  7. %> 
  8.  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  10. <html> 
  11.   <head> 
  12.     <base href="<%=basePath%>"> 
  13.      
  14.     <title>My JSP 'newsList.jsp' starting page</title> 
  15.      
  16.     <meta http-equiv="pragma" content="no-cache"> 
  17.     <meta http-equiv="cache-control" content="no-cache"> 
  18.     <meta http-equiv="expires" content="0">     
  19.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  20.     <meta http-equiv="description" content="This is my page"> 
  21.     <!-- 
  22.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  23.     --> 
  24.     <script type="text/javascript"> 
  25.     function del() 
  26.     { 
  27.         if(confirm("你真的想刪除該記錄麼?")) 
  28.         { 
  29.             return true; 
  30.         } 
  31.         return false; 
  32.     } 
  33.     </script> 
  34.   </head> 
  35.    
  36.   <body> 
  37.    <% request.setCharacterEncoding("gb2312");//設置字符編碼%> 
  38.   <%     
  39.         Connection conn=DBconnection.getConnection(); 
  40.         Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); 
  41.         ResultSet rs = null ; 
  42.         String sql = ""
  43.         int i = 0 ; 
  44.    %> 
  45.    
  46.       <div class="search_top" align="center"> 
  47.         <form action="AdminNewsManageList" method="get" name="searchNews"> 
  48.             <input id="searchContent" name="searchContent" type="text" maxlength="10"/> 
  49.             <select id="searchScope" name="searchScope"> 
  50.                 <option value="newsTitle">標題</option> 
  51.                 <option value="newsTime">時間</option> 
  52.                 <option value="newsWriter">作者</option> 
  53.             </select> 
  54.              
  55.              <
  56.             sql = "select * from t_newsCategory limit 0,3" ; 
  57.             rs = stmt.executeQuery(sql) ; 
  58.             i = 0 ; 
  59.             %> 
  60.             <select id="category" name="category"> 
  61.             <%  
  62.                 while(rs.next() && i<3) { 
  63.                     i ++ ; 
  64.             %> 
  65.              
  66.             <option value="<%=rs.getString("id")%>"><%=rs.getString("categoryName") %></option> 
  67.              
  68.                 <
  69.                     } 
  70.                 %> 
  71.             </select> 
  72.              
  73.             <input name="searchNews" type="submit" value="查詢" /> 
  74.         </form> 
  75.       </div> 
  76.    <table border=1 align=center> 
  77.       <tr> 
  78.          <td width=100>序號</td> 
  79.          <td width=200>新聞標題</td> 
  80.          <td width=100>新聞作者</td> 
  81.          <td width=100>發佈時間</td> 
  82.           <td>編輯</td> 
  83.           <td>刪除</td> 
  84.       </tr> 
  85.          <c:forEach items="${requestScope.newsList}" var="news" varStatus="status"> 
  86.           <tr> 
  87.           <%-- 此處爲編碼自動增加編號,灰常方便,謹記 --%> 
  88.             <td><c:out value="${status.index+1}"> </c:out></td> 
  89.             <td>${news.newsTitle}</a></td> 
  90.             <td>${news.newsWriter} </td> 
  91.             <td>${news.newsTime}</td> 
  92.             <td align=center>  
  93.                  <a href="AdminUpdateNews1?newsID=${news.newsID}" style="text-decoration: none;"> 
  94.                   <img alt="" src="admin/img/edit.gif" border="0"></a>  
  95.             </td> 
  96.             <td align=center> 
  97.                 <a  
  98.                 href="AdminDeleteNews?newsID=${news.newsID}" target="_self" style="text-decoration: none;" onclick="return del();"> 
  99.                  <img alt="" src="admin/img/del.gif" border="0"></a>  
  100.                <%--  onclick="if (confirm('確定要刪除嗎?')) return true; else return false;" --%> 
  101.             </td> 
  102.           </tr> 
  103.         </c:forEach> 
  104.     </table> 
  105.     <div align="center"> 
  106.      
  107.     <c:choose> 
  108.         <c:when test="${page.hasPrePage}"> 
  109.             <a href="AdminNewsManageList?currentPage=1&category=${newsList[0].category}">首頁</a> |  
  110.     <a href="AdminNewsManageList?currentPage=${page.currentPage -1 }&category=${newsList[0].category}">上一頁</a> 
  111.         </c:when> 
  112.         <c:otherwise> 
  113.             首頁 | 上一頁 
  114.         </c:otherwise> 
  115.     </c:choose> 
  116.     <c:choose> 
  117.         <c:when test="${page.hasNextPage}"> 
  118.             <a href="AdminNewsManageList?currentPage=${page.currentPage + 1 }&category=${newsList[0].category}">下一頁</a> |  
  119.     <a href="AdminNewsManageList?currentPage=${page.totalPage }&category=${newsList[0].category}">尾頁</a> 
  120.         </c:when> 
  121.         <c:otherwise> 
  122.             下一頁 | 尾頁 
  123.         </c:otherwise> 
  124.     </c:choose> 
  125.     當前爲第${page.currentPage}頁,共${page.totalPage}頁 
  126.      
  127.      
  128.   </div> 
  129.   </body> 
  130. </html> 

界面顯示如下:

着重看下<c:forEach>標籤,和<c:choose>標籤,很是使用!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章