Learning Notes _java_day13_jstl Tag library (1, 2, 3, 4, 5, 6, 7, 8)

Source: Internet
Author: User

1. A Label language

Day13

L JSTL Tag Library (emphasis)

L Custom Label (understanding)

L MVC design pattern (focus in focus)

L Java three-layer framework (emphasis in focus)

Jstl Tag Library

1 What is Jstl

JSTL is an Apache extension to El expressions (that is, Jstl relies on EL), Jstl is the tag language! Jstl tag is very convenient since use, it and JSP action tag must be, but it is not a JSP built-in tags, need our own guide package, as well as the designated tag library!

If you are using MyEclipse to develop Javaweb, then when you publish the project to Tomcat, you will find that MyEclipse will store the Jstl jar package in the Lib directory! If you do not use myeclipse development then you need to import this JSTL jar package yourself: Jstl-1.2.jar.

2 Jstl Tag Library

JSTL contains four tag libraries:

L Core: Key Tag Library, we study the focus;

FMT: Format tag Library, only need to learn two tags can;

L SQL: Database tag Library, no need to learn, it is obsolete;

L xml:xml Tag Library, no need to learn, it is out of date.

3 Importing Tag libraries using taglib directives

In addition to JSP action tags, using other third-party tag libraries requires:

L Guide Package;

L Use the TAGLIB directive to import the tag library in the JSP page using the tag;

Here is the core tag library for importing Jstl:

<%@ taglib prefix= "C" uri= "Http://java.sun.com/jstl/core"%>

L prefix= "C": Specify the prefix of the tag library, this prefix can give a random value, but everyone will use the core tag library when the prefix is specified as C;

L uri= "Http://java.sun.com/jstl/core": Specify the URI of the tag library, it is not necessarily a real URL, but it can let JSP find the tag library description file;

4 Core Tag Library common tags

4.1 Out and set

Out

<c:out value= "AAA"/>

Output AAA String Constants

<c:out value= "${aaa}"/>

Same as ${AAA}

<c:out value= "${aaa}" default= "xxx"/>

Output XXX string when ${aaa} does not exist

<%

Request.setattribute ("A", "<script>alert (' hello ');</script>");

%>

<c:out value= "${a}" default= "xxx" escapexml= "false"/>

When EscapeXML is false, "<", ">" is not converted. This may be subject to JavaScript attacks.

Set

<c:set var= "A" value= "Hello"/>

In PageContext, add the data with name A,value to Hello.

<c:set var= "A" value= "Hello" scope= "session"/>

Add the data named A,value to Hello in the session.

4.2 Remove

<%

Pagecontext.setattribute ("A", "PageContext");

Request.setattribute ("A", "session");

Session.setattribute ("A", "session");

Application.setattribute ("A", "Application");

%>

<c:remove var= "a"/>

<c:out value= "${a}" default= "None"/>

Delete the data named a in all domains!

<c:remove var= "A" scope= "page"/>

Delete the data of name a in PageContext!

4.3 URL

The URL tag adds sessionid when a URL rewrite is required.

<c:url value= "/"/>

Output Context Path:/day08_01/

<c:url value= "/" var= "a" scope= "request"/>

Assign the result that should be output to variable a. Range for request

<c:url value= "/aservlet"/>

Output:/day08_01/aservlet

<c:url value= "/aservlet" >

<c:param name= "username" value= "abc"/>

<c:param name= "password" value= "123"/>

</c:url>

Output:/day08_01/aservlet?username=abc&password=123

If the parameter contains Chinese, then URL encoding will be used automatically!

4.4 If

If the test property of the If label must be a Boolean value, if the value of test is true, then the contents of the If label are executed, otherwise it is not executed.

<c:set var="a" value="Hello"/>[c1]

<c:if test= "${notempty a[c2]}" >

<c:out value= "${a}"/>

</c:if>

4.5 Choose

The Choose tag corresponds to the IF/ELSE if/else structure in Java. When the label's test is true, the content of this when is executed. The contents of the otherwise tag are executed when all when label test is false.

<c:set var="score" value= "${param.score}"/>

<c:choose>

<c:when test= "${score > | | score < 0}" > Wrong score: ${score}</C:WHEN>[C3]

<c:when test= "${score >=" >a level </C:WHEN>[C4]

<c:when test= "${score >=" >b level </c:when>

<c:when test= "${score >=" >c level </c:when>

<c:when test= "${score >=" > Class D </c:when>

<c:otherwise >e class </C:OTHERWISE>[C5]

</c:choose>

4.6 ForEach

foreach is currently a loop label, and the foreach tag is used in two ways:

L Use the loop variable, specify the start and end values, like for (int i = 1; i <=; i++) {};

L Loop through the collection, like for (Object O: Collection);

1. Cyclic variable mode:

<c:set var="sum" value="0" />[Cui 6]

<c:foreach var="i" begin="1" end="ten">[cui 7]

<c:set var="sum" value= "${sum + i}"/>[cui 8]

</c:forEach>

<c:out value= "sum = ${sum}"/>

Set Step with step

<c:set var="sum" value="0" />----Set variable sum, size 0

<c:foreach var="i" begin="1" end="Ten" step[cui 9] ="2">----Set Variable i, Greater than 1 less than 10,2 growth step

<c:set var="sum" value= "${sum + i}"/>---Set variable sum, size sum+i

</c:forEach>

<c:out value= "sum = ${sum}"/>----output variable sum

2. Iterate through the collection or array mode:

<%

String[] names = {"Zhangsan", "LiSi", "Wangwu", "Zhaoliu"};---prepare an array

Pagecontext.setattribute ("ns", names); ---into a domain

%>

<c:foreach var="Item[Cui] " items= "${ns}[Cui one] " >----items Specify who to cycle, He can be an array or--a set, Var assigns each element in an array or collection to a variable specified by var

<c:out value= "name: ${item}[Cui] "/><br/>

</c:forEach>

Traverse List

<%

list<string> names = new arraylist<string> ();

Names.add ("Zhangsan");

Names.add ("LiSi");

Names.add ("Wangwu");

Names.add ("Zhaoliu");

Pagecontext.setattribute ("ns", names); ----- This sentence can not be omitted, first placed in a domain, in order to do the following operations;

%>

<c:foreach var="item" items= "${ns}" >[cui]

<c:out value= "name: ${item}"/><br/>

</c:forEach>

Traverse Map

<%

map<string,string> stu = new linkedhashmap<string,string> ();

Stu.put ("number", "n_1001");

Stu.put ("name", "Zhangsan");

Stu.put ("Age", "23");

Stu.put ("Sex", "male");

Pagecontext.setattribute ("Stu", Stu);

%>

<c:foreach var="Item[Cui] " items= "${stu}" >

<c:out value= "${item.key}: ${item.value}[Cui] "/><br/>

</c:forEach>

The ForEach tag also has a property: Varstatus, which is used to specify the name of the variable that receives the "loop state", for example: <foreach varstatus= "vs" .../> then you can use the VS variable to get the state of the loop. (Not much to use)

L Count:int type, current to traverse the number of elements;

L Index:int Type, subscript of the current element;

L First:boolean Type, whether it is the first element;

L Last:boolean Type, whether it is the last element;

L Current:object Type, which represents the current project.

<c:foreach var="item" items= "${ns}" varstatus="vs"[cui] >

<c:if test= "${vs.first}[Cui] " > First row:</c:if>

<c:if test= "${vs.last}[Cui] " > Last line:</c:if>

<c:out value= " section ${vs.count}[Cui] line : "/>

<c:out value= "[${vs.index}[Cui] ]: "/>

<c:out value= "name: ${vs.current}[Cui] "/><br/>

</c:forEach>

5 FMT Tag library commonly used tags (not many)

The FMT tag library is used to format the output, which usually requires formatting with time and numbers .

Format time:

<%@ taglib prefix="FMT" uri="http://java.sun.com/jsp/jstl/fmt" %>-----Pilot Pack, label pack

......

<%

Date date = new date (); ------New One time

Pagecontext.setattribute ("D", date); -------into a domain

%>

<fmt:formatdate value= "${d}" pattern="Yyyy-mm-dd HH:mm:ss[cui] "/>----Formation

Format numbers:

<%

double D1 = 3.5;

double d2 = 4.4;

Pagecontext.setattribute ("D1", D1);

Pagecontext.setattribute ("D2", D2);

%>

<fmt:formatnumber value= "${d1}" pattern="0.00[Cui] "/><br/>

<fmt:formatnumber value= "${d2}" pattern="#.##[cui] "/>

[C1] Creating a variable named a in the page field

[C2] determines that a variable is not NULL, does not have a specified field, and represents a global domain

[C3]if

[C4]else If

[C5]else

[Cui 6] create page field property sum is 0

[Cui 7] set the loop variable I, the initial value is 1, traverse to 10.

[Cui 8] set sum value to sum + I

[Cui 9] step is 2, the default step is 1. Equivalent to the meaning of i+=2! Instead, the default is i++.

[Cui] assigns the NS to the item.

[Cui]NS is an array that is traversed.

[Cui] print each item

[Cui] is no different from traversing an array!

[Cui] because the traversal is a map, so each item is entry type

[Cui] get entry key and value

[Cui] assigns the loop state to vs

[Cui] if it is the first line, then ${vs.first} is True

[Cui] if it is the last line, then ${vs.last} is True

[Cui] returns the number of rows, starting from 1

[Cui] returns the number of rows, starting from 0

[Cui] current item, same as ${item}

[Cui] outputs the date and time in the specified format.

[Cui] must and can only retain two decimal places, if it is greater than two bits, then only two bits are reserved, and rounded, if less than two bits, then use 0 to complement two bits.

[Cui] shows a maximum of two bits, if less than two, then there are several reservations, do not use 0 to complement. Greater than two bits, only two bits are reserved and rounded!

Learning Notes _java_day13_jstl Tag library (1, 2, 3, 4, 5, 6, 7, 8)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.