jsp中jstl标签库core全解析



jsp中jstl标签库core全解析

首先来认识一下jstl:

\

\

\

\

下面来介绍一下核心标签库:

 

\

前言:jstl是用来辅助el表达式,用来在jsp页面显示复杂结构的数据

一::

查看c.tld可知:

 

    
        Like <%= ... >, but for expressions.

    out
    org.apache.taglibs.standard.tag.rt.core.OutTag
    JSP

Expression to be evaluated.

        value
        true
        true

Default value if the resulting value is null.

        default
        false
        true

Determines whether characters <,>,&,'," in the
resulting string should be converted to their
corresponding character entity codes. Default value is
true.

        escapeXml
        false
        true

 

向界面输出value值(可输出特殊字符):

属性解析: value: 输出的值(el表达式,字符串)

escapeXml:是否将特殊字符转化,默认true

default: value值为null时输出

代码示例:

 

<%
       pageContext.setAttribute("name", "Tom");
       pageContext.setAttribute("name2", "Tom");
    %>

    ${name2}

out会将<>解析成&qt;>,这样保证界面原样输出,而一般的el表达式不转化<>,hyml会解析颜色

 

二:

 

 

        Sets the result of an expression evaluation in a 'scope'

    set
    org.apache.taglibs.standard.tag.rt.core.SetTag
    JSP

Name of the exported scoped variable to hold the value
specified in the action. The type of the scoped variable is
whatever type the value expression evaluates to.

        var
        false
        false

Expression to be evaluated.

        value
        false
        true

	    java.lang.Object

Target object whose property will be set. Must evaluate to
a JavaBeans object with setter property property, or to a
java.util.Map object.

        target
        false
        true

Name of the property to be set in the target object.

        property
        false
        true

Scope for var.

        scope
        false
        false

设置属性:相当于setAttribute();

 

属性搭配一: var:存储容器中的属性变量名

value:属性值

属性搭配二:target:容器中的一个对象

property:对象属性名

value:属性值

scope:容器的种类(四种,二种搭配可选可不选)

代码示例:

 

    

    ${aa}, ${requestScope.aa}

三:

    
        Removes a scoped variable (from a particular scope, if specified).

    remove
    org.apache.taglibs.standard.tag.common.core.RemoveTag
    empty

Name of the scoped variable to be removed.

        var
        true
        false

Scope for var.

        scope
        false
        false

在容器中移除var值属性,默认全部容器

 

属性: var:容器中的属性值

scope:指定的容器

代码示例:

 

     

      ${aa}

四:

 

 

    
	Simple conditional tag, which evalutes its body if the
	supplied condition is true and optionally exposes a Boolean
	scripting variable representing the evaluation of this condition

    if
    org.apache.taglibs.standard.tag.rt.core.IfTag
    JSP

The test condition that determines whether or
not the body content should be processed.

        test
        true
        true
	boolean

Name of the exported scoped variable for the
resulting value of the test condition. The type
of the scoped variable is Boolean.        

        var
        false
        false

Scope for var.

        scope
        false
        false

判断test属性值真假,是否执行里面的代码

 

属性: test:一个返回boolean值得表达式

var:存储test返回值的变量名

scope: 存储var变量的容器

代码示例:

 

    
       dfdfdfdf

NOooooo

五:

 

 

    
	Simple conditional tag that establishes a context for
	mutually exclusive conditional operations, marked by
	 and 

    choose
    org.apache.taglibs.standard.tag.common.core.ChooseTag
    JSP
 

	Subtag of  that includes its body if its
	condition evalutes to 'true'

    when
    org.apache.taglibs.standard.tag.rt.core.WhenTag
    JSP

The test condition that determines whether or not the
body content should be processed.

        test
        true
        true
	boolean
    
        Subtag of  that follows  tags
        and runs only if all of the prior conditions evaluated to
        'false'

    otherwise
    org.apache.taglibs.standard.tag.common.core.OtherwiseTag
    JSP


一种类似于swich-case结构

 

:无属性,标记开始

:相当于case可重复 属性test:会返回为boolea值得表达式

:无属性,相当于defaule语句

代码示例:

 

      

                       优秀

                       良好

                       中等

                       不行

六:普通循环:
增强for循环:

 

 

 

	The basic iteration tag, accepting many different
        collection types and supporting subsetting and other
        functionality

    forEach
    org.apache.taglibs.standard.tag.rt.core.ForEachTag
    org.apache.taglibs.standard.tei.ForEachTEI
    JSP

Collection of items to iterate over.

	items
	false
	true
	java.lang.Object

	    java.lang.Object

If items specified:
Iteration begins at the item located at the
specified index. First item of the collection has
index 0.
If items not specified:
Iteration begins with index set at the value
specified.

	begin
	false
	true
	int

If items specified:
Iteration ends at the item located at the
specified index (inclusive).
If items not specified:
Iteration ends when index reaches the value
specified.

	end
	false
	true
	int

Iteration will only process every step items of
the collection, starting with the first one.

	step
	false
	true
	int

Name of the exported scoped variable for the
current item of the iteration. This scoped
variable has nested visibility. Its type depends
on the object of the underlying collection.

	var
	false
	false

Name of the exported scoped variable for the
status of the iteration. Object exported is of type
javax.servlet.jsp.jstl.core.LoopTagStatus. This scoped variable has nested
visibility.

	varStatus
	false
	false

第一套普通循环相当于:for( var=begin; var<=end; var+=step)
属性:begin:起始值

 

end:结尾值

step:步长值

var:表示目前的循环值

第二套属性搭配相当于:for( var : items )

属性:items:集合/数组的引用名

var:每一项的变量名

varStatus=”"该属性可加可不加:有俩个下级变量:

index:记录当前下标值 items-var型从0开始 begin-end-step型从begin开始

count:记录当前遍历数

代码示例:

 

     <%
        List list2 = new ArrayList();
	     list2.add("aa1111" );
	     list2.add("bb2222");
	     list2.add(200);
	     list2.add(100);
	     request.setAttribute("lis2", list2);
     %>

	     ${aa } ,

    <%
       Map map = new HashMap();
    	map.put("name", "Rose");
    	map.put("age",55);
    	map.put("tel", "13566668888");
    	pageContext.setAttribute("m", map);
    %>

       ${im.key} = ${im.value } 

    <%
       String strs[] ={"aaa","bbb","111","2222"};
       pageContext.setAttribute("strs", strs);
    %>

       ${str} ,,

看看ForEach标签中的varStatus属性—idx.index是元素的下标(从0开始),idx.count是元素的序号(从1开始计数)

    
      ${str} ---- index= ${idx.index}   count= ${idx.count} 

        ${i}    --${idx.count}

七:分隔符拆分

 

从begin-end间,以step长度分割

 

    
	Iterates over tokens, separated by the supplied delimeters

    forTokens
    org.apache.taglibs.standard.tag.rt.core.ForTokensTag
    JSP

String of tokens to iterate over.

	items
	true
	true
	java.lang.String

	    java.lang.String

The set of delimiters (the characters that
separate the tokens in the string).

	delims
	true
	true
	java.lang.String

Iteration begins at the token located at the
specified index. First token has index 0.

	begin
	false
	true
	int

Iteration ends at the token located at the
specified index (inclusive).

	end
	false
	true
	int

Iteration will only process every step tokens
of the string, starting with the first one.

	step
	false
	true
	int

Name of the exported scoped variable for the
current item of the iteration. This scoped
variable has nested visibility.

	var
	false
	false

Name of the exported scoped variable for the
status of the iteration. Object exported is of
type
javax.servlet.jsp.jstl.core.LoopTag
Status. This scoped variable has nested
visibility.

	varStatus
	false
	false

型:

 

属性:items:要分割的表达式

delims:分隔符

var:分割的每一项

代码示例:

 

      
          ${str}

八:相当于动态导入

 

 

    
        Retrieves an absolute or relative URL and exposes its contents
        to either the page, a String in 'var', or a Reader in 'varReader'.

    import
    org.apache.taglibs.standard.tag.rt.core.ImportTag
    org.apache.taglibs.standard.tei.ImportTEI
    JSP

The URL of the resource to import.

        url
        true
        true

Name of the exported scoped variable for the
resource's content. The type of the scoped
variable is String.

        var
        false
        false

Scope for var.

        scope
        false
        false

Name of the exported scoped variable for the
resource's content. The type of the scoped
variable is Reader.

        varReader
        false
        false

Name of the context when accessing a relative
URL resource that belongs to a foreign
context.

        context
        false
        true

Character encoding of the content at the input
resource.

        charEncoding
        false
        true

属性:url:导入的资源路径(可访问)

 

代码示例:

 


九:链接,具有重写url技术

 

 

    
        Creates a URL with optional query parameters.

    url
    org.apache.taglibs.standard.tag.rt.core.UrlTag
    JSP

Name of the exported scoped variable for the
processed url. The type of the scoped variable is
String.

        var
        false
        false

Scope for var.

        scope
        false
        false

URL to be processed.

        value
        false
        true

Name of the context when specifying a relative URL
resource that belongs to a foreign context.

        context
        false
        true

属性:value:资源路径(可访问)

 

代码示例

I18N演示

十:重定向

 

 

    
        Redirects to a new URL.

    redirect
    org.apache.taglibs.standard.tag.rt.core.RedirectTag
    JSP

The URL of the resource to redirect to.

        url
        false
        true

Name of the context when redirecting to a relative URL
resource that belongs to a foreign context.

        context
        false
        true

属性:url:资源路径(可访问)

代码示例

 

<%–