1.JSP標準標籤庫
首先下載jstl-1.2.jar包如下圖所示
解壓文件夾下的METN-INF下的tld文件如下所示:
將這幾個文件報錯在WEB項目中WEB-INF下面,
在demo1.jsp頁面輸入如下所示
- <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
- <body>
- <h3> <c:out value="Hellofff Mary!"></c:out></h3>
- </body>
啓動服務器即可進行輸出Hellofff Mary!
若不想導入
- <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
則可在web.xml中設置,代碼如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- <display-name></display-name>
- <jsp-config>
- <taglib>
- <taglib-uri>http://www.mldn.cn/jstl/core</taglib-uri>
- <taglib-location>/WEB-INF/c.tld</taglib-location>
- </taglib>
- </jsp-config>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
此時即可在導入jsp或html頁面中添加
- <%@ taglib uri="http://www.mldn.cn/jstl/core" prefix="c"%>
記住哦!
2.核心標籤庫中主要標籤
<c:out>標籤如何使用
- <body>
- <% pageContext.setAttribute("info","馬開元的信息");
- %>
- <h3>屬性存在:<c:out value="${info}"/></h3>
- <h3>屬性不存在:<c:out value="${info1}" default="沒有此內容"/></h3>
- </body>
<c:set>標籤:
- <c:set var="info3" value="2號" scope="request"></c:set>
- 屬性內容:${info3}
則此輸出2號
例2:在項目中src下建立bean包,建立類Simple如下:
- package bean;
- public class Simple {
- private String id;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- }
在建立一個demo2.jsp頁面如下
- <body>
- <% Simple simple=new Simple();
- request.setAttribute("simple",simple);
- %>
- <c:set value="3號" target="${simple}" property="id"/>
- <h3>id值爲,你猜猜是多少??黑黑${simple.id}</h3>
- </body>
將輸出3號,爲Simple類的id屬性賦值,這在web開發中,一般用type=“hidden”文本進行隱形傳值,用C標籤可以簡化步驟,也可爲其賦值
<c:remove>標籤如下:
- <c:set var="info3" value="2號" scope="request"></c:set>
- 屬性內容:${info3}
- <c:remove var="info3" scope="request"/>
- 屬性內容:${info3}
第二個結果中沒有任何信息
<c:catch>標籤如下:
- <c:catch var="error">
- <%
- int result=10/0;
- ;o
- %>
- </c:catch>
- <h3>異常信息${error}</h3>
<c:forEach>標籤使用:
- <body>
- <%
- List all=new ArrayList();
- all.add("你好");
- all.add("我好");
- all.add("他好");
- pageContext.setAttribute("pp",all);
- %>
- <h3>
- 輸出全部:
- <c:forEach items="${pp}" var="mem">
- ${mem}
- </c:forEach>
- </h3>
- </body>
循環輸出list中的值,$爲EL表達式,輸出時不需導入任何包,很方便使用,<c:forEach>標籤在新聞網站中,若news中有多條,可以進行循環輸出,代碼方便容易懂
<c:choose>標籤使用:
- <body>
- <% pageContext.setAttribute("number",10); %>
- <c:choose>
- <c:when test="${num==10}">
- number的值是10
- </c:when>
- <c:when test="${num==20}">
- number的值是20
- </c:when>
- <c:otherwise>
- 沒一個條件滿足
- </c:otherwise>
- </c:choose>
- </body>
在實際操作中代碼示例如下:
- <%@ page language="java" import="java.util.*,com.mky.dao.*,com.mky.factory.*,com.mky.bean.*,com.mky.util.*" pageEncoding="gb2312"%>
- <%@page import="java.sql.*" %>
- <%@ taglib prefix="c" uri="/WEB-INF/c.tld"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'newsList.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- <script type="text/javascript">
- function del()
- {
- if(confirm("你真的想刪除該記錄麼?"))
- {
- return true;
- }
- return false;
- }
- </script>
- </head>
- <body>
- <% request.setCharacterEncoding("gb2312");//設置字符編碼%>
- <%
- Connection conn=DBconnection.getConnection();
- Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
- ResultSet rs = null ;
- String sql = "";
- int i = 0 ;
- %>
- <div class="search_top" align="center">
- <form action="AdminNewsManageList" method="get" name="searchNews">
- <input id="searchContent" name="searchContent" type="text" maxlength="10"/>
- <select id="searchScope" name="searchScope">
- <option value="newsTitle">標題</option>
- <option value="newsTime">時間</option>
- <option value="newsWriter">作者</option>
- </select>
- <%
- sql = "select * from t_newsCategory limit 0,3" ;
- rs = stmt.executeQuery(sql) ;
- i = 0 ;
- %>
- <select id="category" name="category">
- <%
- while(rs.next() && i<3) {
- i ++ ;
- %>
- <option value="<%=rs.getString("id")%>"><%=rs.getString("categoryName") %></option>
- <%
- }
- %>
- </select>
- <input name="searchNews" type="submit" value="查詢" />
- </form>
- </div>
- <table border=1 align=center>
- <tr>
- <td width=100>序號</td>
- <td width=200>新聞標題</td>
- <td width=100>新聞作者</td>
- <td width=100>發佈時間</td>
- <td>編輯</td>
- <td>刪除</td>
- </tr>
- <c:forEach items="${requestScope.newsList}" var="news" varStatus="status">
- <tr>
- <%-- 此處爲編碼自動增加編號,灰常方便,謹記 --%>
- <td><c:out value="${status.index+1}"> </c:out></td>
- <td>${news.newsTitle}</a></td>
- <td>${news.newsWriter} </td>
- <td>${news.newsTime}</td>
- <td align=center>
- <a href="AdminUpdateNews1?newsID=${news.newsID}" style="text-decoration: none;">
- <img alt="" src="admin/img/edit.gif" border="0"></a>
- </td>
- <td align=center>
- <a
- href="AdminDeleteNews?newsID=${news.newsID}" target="_self" style="text-decoration: none;" onclick="return del();">
- <img alt="" src="admin/img/del.gif" border="0"></a>
- <%-- onclick="if (confirm('確定要刪除嗎?')) return true; else return false;" --%>
- </td>
- </tr>
- </c:forEach>
- </table>
- <div align="center">
- <c:choose>
- <c:when test="${page.hasPrePage}">
- <a href="AdminNewsManageList?currentPage=1&category=${newsList[0].category}">首頁</a> |
- <a href="AdminNewsManageList?currentPage=${page.currentPage -1 }&category=${newsList[0].category}">上一頁</a>
- </c:when>
- <c:otherwise>
- 首頁 | 上一頁
- </c:otherwise>
- </c:choose>
- <c:choose>
- <c:when test="${page.hasNextPage}">
- <a href="AdminNewsManageList?currentPage=${page.currentPage + 1 }&category=${newsList[0].category}">下一頁</a> |
- <a href="AdminNewsManageList?currentPage=${page.totalPage }&category=${newsList[0].category}">尾頁</a>
- </c:when>
- <c:otherwise>
- 下一頁 | 尾頁
- </c:otherwise>
- </c:choose>
- 當前爲第${page.currentPage}頁,共${page.totalPage}頁
- </div>
- </body>
- </html>
界面顯示如下:
着重看下<c:forEach>標籤,和<c:choose>標籤,很是使用!