• JSTL标签


    一.概述

    JSTL(JavaServer Pages Standard Tag Library,即JSP标准标签库)包含了在JSP开发中经常用到的一组标签库,这些标签为我们提供了一种不用嵌入Java代码。就可以开发复杂JSP页面的途径。JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的。使用JSLT标签的目的就是不希望在JSP页面中出现Java逻辑代码。 JSTL是由SUN公司推出的,由Apache Jakarta组织负责维护的用于编写和开发JSP页面的一组标准标签。作为开源的标准技术,它一直在不断的完善。JSTL标签库包含了各种标签,如通用标签、条件判断标签和迭代标签等。

    二.标签使用

    在工程中引用JSTL的jar包       对1.1版本的JSTL在工程中进行引用,添加jstl.jar和standard.jar两个包,放置到项目的WebRoot/WEB-INF/lib目录下即可。在MyEclipse集成开发环境中已经集成了JSTL,这个过程可以有工具代为实现。 在需要JSTL的JSP页面使用taglib指令导入标签库描述文件       如果需要使用JSTL核心标签库,在页面上方增加如下一行指令:<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>       如果需要添加SQL标签库、格式标签库、XML标签库和函数标签库,添加的指令如下。例如添加SQL标签库指令:<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 核心标签库
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> sql标签库
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 格式标签库
    <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %> xml标签库
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 函数标签库

    三.JSTL核心标签库

    通用标签库:(1) <c:set>标签               (2) <c:out>标签       (3) <c:remove>标签

    条件标签库:(1) <c:if>标签                  (2) <c:choose>标签

    迭代标签库:(1) <c:forEach>标签       (2) <c:forTokens>标签

    通用标签库

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <base href="<%=basePath%>">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</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">
    </head>
    <body>
    设置变量之前的message值:
    <c:out value="${message}" default="null"></c:out>
    <br>
    <c:set var="message" value="helloword!" scope="page"></c:set>
    设置新值之后的message值:
    <br>
    <c:out value="${message}"></c:out><br>
    <c:remove var="message" scope="page"></c:remove>
    <br>
    移除变量后message的值:
    <c:out value="${message}"default="null"></c:out>
    </body>
    </html>

    条件标签库

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
    <%
        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>JSTL条件标签库的使用</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">
        -->
    
    </head>
    
    <body>
    <c:set var="isLogin" value="${not empty sessionScope.LOGIN_NAME }"></c:set>
    <c:if test="${not isLogin }">
        <form name="form1" method="post" action="doJstlCondition.jsp">
            用户名:<input type="text" name="username"> <br>
            密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"> <br>
            <input type="submit" value="登录">
        </form>
    </c:if>
    <c:if test="${isLogin }">
        <c:out value="${sessionScope.LOGIN_NAME } "></c:out>已经登录!
    </c:if>
    <c:choose>
        <c:when test="${row.v_money<10000 }">
            初级者
        </c:when>
        <c:when test="${row.v_money>=10000 && row.v_money<20000}">
            中级者
        </c:when>
        <c:otherwise>
            高级者
        </c:otherwise>
    </c:choose>
    
    </body>
    </html>
    

    doJstlCondition.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    <%
        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 'doJstlCondition.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">
        -->
    
    </head>
    
    <body>
    <%
        request.setCharacterEncoding("utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        if(username.equals("admin") && password.equals("123")){
            session.setAttribute("LOGIN_NAME", username);
    
            response.sendRedirect("jstlCondition.jsp");
    
        }else{
            out.println("用户名或者密码错误!");
        }
    
    %>
    </body>
    </html>
    

    迭代标签库:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page import="com.chinasofti.entity.Admin" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%
    	Admin admin=null;
    	List adminList=new ArrayList();
    	for(int i=0;i<10;i++){
    		admin=new Admin(i+1,"管理员"+(i+1),"密码"+(i+1));
    		adminList.add(admin);
    	}
    	request.setAttribute("ADMINLIST", adminList);
    %>
    
    
    <%
    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>JSTL迭代标签的使用</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">
    	-->
    
      </head>
      
      <body>
    	<table border="1" width="70%" align="center">
    		<tr>
    			<td>ID</td>
    			<td>用户名</td>
    			<td>密码</td>
    		</tr>
    	<c:forEach var="admin" items="${requestScope.ADMINLIST}" varStatus="status">
    		<tr <c:if test="${status.index%2==1}">style="background-color:yellow;" </c:if>>
    			<td>${admin.id }</td>
    			<td>${admin["loginName"] }</td>
    			<td>${admin.loginPwd }</td>
    		</tr>
    	</c:forEach>
    	</table>
    	<br>
    	<c:forTokens items="China,Russia,France" delims="," var="item">
    		<c:out value="${item }"/> <br>
    	</c:forTokens>
      </body>
    </html>
    
  • 相关阅读:
    如何快速提高编程能力
    Django线上部署实战教程之Nginx+Gunicorn+Django篇
    Navicat for MySQL远程连接报10038的错误
    DjangoBlog部署教程
    用windows下的Anaconda搭建Django虚拟环境
    基础类型及相关操作
    格式化输出编码问题!
    初识python!
    Java学习之多线程二
    Java学习之多线程一
  • 原文地址:https://www.cnblogs.com/jatpeo/p/11767544.html
Copyright © 2020-2023  润新知