zoukankan      html  css  js  c++  java
  • jsp jstl标签库核心标签

    JSTL标签库介绍

    JSTL标签库的使用时为了弥补html标签的不足,规范自定义标签的使用而诞生的。使用标签的目的就是不希望在jsp页面中出现java逻辑代码
    全称:JSTL标签库分类

    核心标签库使用说明

        JSTL的核心标签库标签供13个,使用这些标签能够完成JSP页面的基本功能,减少编码工作。
         从功能上可以分为4类:表达式控制标签,流程控制标签,循环表桥,URL操作标签。
    表达式控制标签:out标签、set标签、remove标签、catch标签
    流程控制标签:if标签、choose标签、when标签、otherwise标签
    循环标签: forEach标签、forTokens标签
    URL操作标签:import标签、url标签、redirect标签、param标签
    在JSP页面引入核心标签库的代码为<%@ taglib prefix="gs" uri="http://java.sun.com/jsp/jstl/core" %>
    

    out标签

     
    out标签功能:用来输出数据对象(字符串,表达式)的内容或结果
    在使用java脚本是常使用的方式为:<% out.println("字符串")%>或者<%= 表达式%>,web开发中尽可能的避免在页面使用java脚本,使用<c:out>标签就可以实现以上功能。
    out标签语法:
           <gs:out value="需要显示的信息" [escapeXml="true/false"] [default="默认值"] />
           <gs:out value="要显示的数据对象" [escapeXml="true|false"] [default="默认值"] 这些使用[]属性表示不是必须的。
    

    out标签的属性

    属性名 是否支持EL 属性类型 属性描述
    value true Object 指定需要输出的内容
    escapeXml true Boolean 指定是否将>、<、&、'、"等特殊字符进行HTML编码转换后在进行输出,默认为true
    default true Object 指定如果value属性的值为null时所输出的默认值
    jsp页面:
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jstl/core"  prefix="gs"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    out直接输出字符串:
    <gs:out value="this is tag Value!"></gs:out>
    <br/>
    <!-- escapeXml html字符转义  true 输出为正常文本,false输出为html转义文本-->
    <gs:out value="<a href='www.baidu.com'> 百度 </a>"></gs:out>
    <gs:out value="<a href='www.baidu.com'> 百度 </a>" escapeXml="false"></gs:out>
    <br/>
    ​
    </body>
    </html>
    

    set标签

    set标签功能:用于把某一个对象存在指定的域范围内,或者将一个对象存储到Map或者JavaBean对象中
    set标签语法:
        <gs:set value="值1" var="name1" [scope="page|reqest|session|application"] />
        把一个变量名为name1值为"值1"的变量存储在指定的scope范围内。
        <gs:set var="name2" [scope="page|request|session|application"]>值2</gs:set>
        把一个变量名为name2,值为值2的变量存储在指定的scope范围内。
        
        <gs:set value="值3" target="javaBean对象" property="属性名" />
        把一个值为"值3"赋值给指定的JavaBean的属性名
         <gs:set target="JavaBean对象" property="属性名"/>值4</gs:set>
         把一个值4赋值给指定的JavaBean的属性名
    

    set标签属性

    属性名 是否支持EL 属性类型 属性描述
    value true object 用于指定属性值
    var false string 用于自定要设置的web于属性的名称
    scope false string 用于指定属性所在的web域 page request,session ,application
    target true object 用于指定要设置的属性对象,这个对象必须是JavaBean对象或Java.util.Map对象
    property true string 用于指定当前要设置的属性名称
    jsp页面:
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ page import="jsp.jstl.Person" %>
    <%@ taglib uri="http://java.sun.com/jstl/core"  prefix="gs"%>
    <%@ page isELIgnored="false" %> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
    set设置值:
    <gs:set value="rssgao" var="raouo" scope="request"></gs:set>
    <gs:set value="sssgao" var="saouo" scope="session"></gs:set>
    <gs:set value="assgao" var="aaouo" scope="application"></gs:set>
    <gs:set value="ssgao" var="aouo" ></gs:set>
    <p>${requestScope.raouo}</p>
    <p><gs:out value="${sessionScope.saouo}"></gs:out></p>
    <p>${aouo}</p>
    ​
    <p><%=request.getAttribute("raouo") %></p>
    <p><%=session.getAttribute("saouo") %></p>
    <p><%=application.getAttribute("aaouo") %></p>
    <p><%=pageContext.findAttribute("aouo") %></p> <!-- 未指定scope的范围,会从不同的范围内查找得到相应的值: -->
    
    set设置对象
    <jsp:useBean id="person" class="jsp.jstl.Person" />
    <jsp:setProperty  name="person" property="pname" value="gs"/>
    <gs:set target="${person}" property="age"> 27 </gs:set>
    <gs:set target="${person}" property="addr">HZ</gs:set>
    ​
    <p><%=person.getPname() %></p>
    <gs:out value="${person.addr}"></gs:out>
    </body>
    </html>
    

    remove标签

    remove标签功能:用来从指定的JSP范围内移除指定的变量

    remove标签语法:
        <gs:remove var="变量名" [scope="page|request|session|application"] />
        其中var 属性是必须的,scope可以省略
    
    JSP页面
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jstl/core"  prefix="gs"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <gs:set value="ssgao" var="name" scope="request" />
    <gs:set value="ssgao" var="name" scope="session" />
    ​
    <gs:out value="${sessionScope.name }" />
    <gs:remove var="name" scope="request" />
    <gs:out value="${requestScope.name }" />
    ​
    </body>
    </html>
    

    表达式控制标签

    catche标签
    catch标签功能:用于捕获嵌套在标签体中的内容抛出的异常、
    catch标签语法:
      <gs:catch [var="varName"]> 容易产生异常的代码 </gs:catch>
       var 属性用于标识catch标签捕获的异常对象,它将保存在page这个WEB域中,
    jsp页面
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jstl/core"  prefix="gs"%>
    <%@ page import="jsp.jstl.Person" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    ​
    <gs:catch var="error" >
        <gs:set target="person" property="name">ssgao</gs:set>
    </gs:catch>
    ​
    <gs:out value="${error}"></gs:out>
    ​
    </body>
    </html>
    

    if标签

    if标签功能:用来实现条件控制。
    if标签语法: 
       <gs:if test="testCondition" var="varName" [scope="{page|request|session|application}"]/>
       <gs:if test="testCondition" var="varName" [scope="{page|request|session|application}"]/>
            标签体内容
       </gs:if>
    

    if标签属性

    属性名 是否支持EL 属性类型 属性描述
    test true boolean 决定是否处理标签体中的内容的条件表达式
    var false String 用于指定将test属性的执行结果保存到某个web域中的某个属性的名称
    scope false String 将test属性指行结果保存哪个web域中
    JSP页面
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jstl/core"  prefix="gs"%>
    <%@ page import="jsp.jstl.Person" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <p>${_name}</p>
    <gs:out value="${_name}"/>
    <gs:if test="${_name}=='if'" var="flag" scope="request" />
    <gs:out value="${flag}"/>
    ​
    </body>
    </html>
    

    流程控制标签

    choose、when、otherwise标签
    choose,when,otherwise这3个标签通常情况下是一起使用的,choose标签作为when和otherwise标签的父标签来使用。
    使用choose,when,otherwise三个标签,可以构造类似"if-else if -else"的复杂条件判断结构
    标签语法
    <gs:choose>
      <gs:when test="条件1"> 业务逻辑1 </gs:when>
      <gs:when test="条件2"> 业务逻辑2 </gs:when>
      <gs:otherwise> 业务逻辑 </gs:otherwise>
    </gs:choose>
    JSP页面
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jstl/core"  prefix="gs"%>
    <%@ page import="jsp.jstl.Person" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    choose标签
    <gs:set var="score" value="55"  />
    <gs:out value="${score}" />
    <gs:choose>
        <gs:when test="${score>50}">
            大于50
        </gs:when>
        <gs:when test="${score<40}">
            小于50
        </gs:when>
        <gs:otherwise>
            其他数据信息
        </gs:otherwise>
    </gs:choose>
    ​
    </body>
    </html>
    

    循环标签

    forEach标签

    forEach标签功能:根据循环条件遍历集合中的元素
    forEach标签语法:
     <gs:forEach var="name" items="Collection" varStatus="StatusName" begin="begin" end="end" step="step"> 本体内容 </gs:forEach>
    var 设定变量名用于存储从集合中取出元素
    items指定要遍历的集合
    varStatus设定变量名,该变量用于存放集合中元素的信息
    begin,end用于指定遍历的起始位置和终止位置
    step用于指定循环步长
    
    属性名称 是否支持EL 属性类型 是否必须
    var no String
    items yes Arrays
    Collection
    Iterator
    Eunmeration
    Map
    String[] args
    begin yes int
    end yes int
    step yes int
    varStatus no String

    varStatus的4个状态

    属性名 类型 说明
    index int 当前循环的索引值
    count int 循环的次数
    first boolean 是否为第一个位置
    last boolean 是否为最后一个位置
    JSP页面
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jstl/core"  prefix="gs"%>
    <%@ page import="jsp.jstl.Person,java.util.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <%
        List<String> list=new ArrayList<String>();
        list.add("A");
        list.add("B");
        request.setAttribute("list", list);
    %>
    ​
    <gs:forEach var="item" items="${list }">
        <gs:out value="${item }"></gs:out>
    </gs:forEach>
    ​
    <gs:forEach var="item" items="${list }" begin="1" end="2" step="1">
        <gs:out value="${item }" />
    </gs:forEach>
    ​
    <gs:forEach var="item" items="${list }" begin="1" end="2" varStatus="zt" step="1" >
        <gs:out value="${item }" />
        <gs:out value="${zt.index }" />
        <gs:out value="${zt.count }" />
        <gs:out value="${zt.first }" />
        <gs:out value="${zt.last }" />
    </gs:forEach>
    </body>
    </html>
    

    forTokens标签

    forToken标签功能:用于浏览字符串,并根据指定的字符将字符串截取

    forToken标签语法:
        <gs:forTokens items="strngOfTokens" delims="delimiters" [var ="name" begin="begin" end="end" step="len" varStatus="statusName"]>本体内容</forTokens>
    items:指定被迭代的字符串
    delims:指定使用的分隔符
    var:指定用来存放遍历到的成员
    begin:指定遍历的开始位置(int 型从取值0开始)
    end:指定遍历结束的位置(int 型,默认集合中最后一个元素)
    step;遍历步长
    varStatus:存放遍历到的成员的状态信息
    JSP页面
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jstl/core"  prefix="gs"%>
    <%@ page import="jsp.jstl.Person" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <gs:forTokens var="str" items="北,京,欢,迎,您!" delims=",">
        <gs:out value="${str }" />
    </gs:forTokens>
    <gs:forTokens var="n" items="1*2*3*4*5*6*7" delims="*" begin="1" end="3" varStatus="s">
        
        <gs:out value="${n }" />
        <gs:out value="${s.index }" />
        <gs:out value="${s.count }" />
        <gs:out value="${s.first }" />
        <gs:out value="${s.last }" />
    </gs:forTokens>
    </body>
    </html>
    

    URL操作标签

    import标签

    import标签功能:把其他静态或动态文件包含到本JSP页面,与jsp:include的区别是jsp:import只可以包含同一个web应用中的文件,而import标签则可以包含其他web应用中的文件,甚至是网络上的资源

    import标签语法
     <gs:import url="url" [content="context"] [value="value"] [scope="page|request|session|application"] [charEncoding="encoding"] />
     <gs:import url="url" varReader="name" [context="context"] [charEncoding="encoding"] />
    import标签参数说明
    URL为资源的路径,当引用的资源不存在时,系统就会抛出异常,因此该语句应该放在<gs:catch></gs:catch>语句块中捕获。
    引用资源有两种方式:绝对路径和相对路径
    使用绝对路径的示例:<gs:import url="http://www.baidu.com">
    使用相对路径的示例:<gs:import utl="aa.txt"> aa.txt放在同一个文件目录
    如果以"/"开头表示应用的跟目录、例如tomcat应用程序的根目录文件为webapps.导入webapps文件下的文件bb.txt编写方式为<gs:import url="/bb.txt">
    如果访问webapps管理文件夹中其他的web应用就要用contxt属性
    context属性用于在访问其他web应用文件时,指定根目录。例如,访问root下的index.jsp的实现代码为<gs:import url="/index.jsp" context="root">
    等同于webapps/root/index.jsp
    var scope charEncoding varReader是可选属性
    

    url标签

    url标签功能:用于在JSP页面中构造一个URL地址,其主要目的是实现URL重写

    url标签语法:
     <gs:url value="value" [var="name"] [scope="page|request|session|application"] [context="context"] />
     <gs:url value="value" [scope="page|request|session|application"] [context="context"] >
           <gs:param name="参数名" value="值" />
     </gs:url>
    

    url标签属性

    属性名 是否支持EL 属性类型 属性描述
    value true String 指定要构造的URL
    var false String 指定将构造出的URL结果保存在web域中的属性名称
    scope false String 指定将构造出的URL结果保存那个Web域中
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jstl/core"  prefix="gs"%>
    <%@ page import="jsp.jstl.Person" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    ​
    <gs:url value="http://www.baidu.com" var="url" scope="session">
        <gs:param name="userName" value="15336506416" />
        <gs:param name="pwd">ssgao1987</gs:param>
    </gs:url>
    ​
    <a href="${url }" >百度首页</a>
    </body>
    </html>
    

    redirect标签

    redirect标签的功能:实现请求重定向,同事配合使用param标签在URL中加入指定的参数

    redirect标签的语法:
        <gs:redirect url="url" [context="context"] />
        <gs:redirect url="url" [context="context"] >
            <gs:param name="name1" value="value1" />
        </gs:redirect>
    

    redirect标签的属性

    属性名 是否支持EL 属性类型 属性描述
    url true string 指定要转换或重定向到的目标资源的url地址
    context true string 当要使用相对路径重定向到同一个服务器下的其他WEB应用程序中的资源时 ,context属性指定其他web应用程序的名称
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jstl/core"  prefix="gs"%>
    <%@ page import="jsp.jstl.Person" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    ​
    <gs:redirect url="http://www.baidu.com">
        <gs:param name="userName">gs</gs:param>
        <gs:param name="pwd">ssgao1987</gs:param>
    </gs:redirect>
    ​
    </body>
    </html>
    

    param标签

        在JSP页面进行url的相关操作时,经常要在url后面附加一些参数。<gs:param>标签可以嵌套在import标签、url标签、或redirect标签。为这些标签所使用的url地址附加参数。param标签在为一个url地址附加参数时,将自动对参数值进行url编码,
       ps: 如果传递的是"中国" 则转换为%D6%d0%B9%FA"后再附加到url地址后面,这就是使用param标签的最大好处。
        
    
  • 相关阅读:
    RESTful API设计指南
    Ubuntu16.04 安装openssl
    shell while循环
    linux awk
    vim与shell切换
    shell for循环
    css 固定宽度,自动换行
    php-fpm 与 cgi
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/tmp/mysql.sock'
    linux ps 命令参数详解
  • 原文地址:https://www.cnblogs.com/ssgao/p/8867561.html
Copyright © 2011-2022 走看看