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>标签,很是使用!