Javaweb Learning notes 11--jstl Tag Library

Source: Internet
Author: User
Tags control label i18n switch case tld java web

First, Jstl Introduction:

The Jstl full name is the JavaServer Pages standard Tag Library, the Chinese name is the JSP standards tag function libraries, the latest version is 1.2. JSTL is a standard specification specified by JCP (Java Community Process), which is primarily provided to a Java Web Developer as a standard generic tag library.

Web program developers can use JSTL and El to develop web programs that replace the traditional approach of embedding Java programs directly on the page (Scripting) to improve program readability, maintainability, and convenience.

Since it is a library file, we need to download it first. Download Link: http://tomcat.apache.org/

Click the taglibs in the Red box section to enter the following screen:

Click on the Red Box section to enter the following screen:

, click the latest version to download it.

Once downloaded, the jar package is copied to the/webcontent/web-inf/lib directory of the Javaweb project for use .

JSTL provides a library of tag functions that are divided into five main categories:

    • (1) core Tag Library
    • (2) i18n format tag libraries (i18n-capable formatting tag library)
    • (3) SQL tag libraries (SQL Tag Library)
    • (4) XML tag libraries (XML tag library)
    • (5) Function tag libraries (Functions tag Library

The most commonly used is the first one, and several others are not very common.

The Core Tag library has the following main categories:

1. Expression Tags:

    • <c:out>: Mainly used to display the content of the data, like <%= scripting-language%>
    • <C:SET>: Primarily used to store variables in the JSP scope or in JavaBean properties
    • <c:remove>: Mainly used for removing variables
    • <c:catch>: Mainly used to deal with abnormal conditions that produce errors, and to store the error information (less)

2. Process Control:

    • <c:if>: Use the same as we normally do in programs
    • <c:choose>: Only as parent tags for <c:when> and <c:otherwise>
    • <c:when>/<c:otherwise>: When using <c:when> and <c:otherwise> for Process control, both must be <c:choose> Sub-label (similar to switch Case )

3. Iterative operation:

    • <c:forEach>: Loop traversal, which allows the members of a collection (Collection) to be browsed through sequentially. The mode of operation is that when the conditions are met, the <c:forEach> 's ontology content is continuously repeated.
    • <c:fortokens>: Used to browse all members of a string, whose members are delimited by the definition symbol (delimiters).

Here's a look at these tags in more detail.

Second, the core tag library:

Contains common tasks for Web applications such as looping, assignment of expressions, basic input and output , and so on.

1. Expression Tags:

    • <c:out>: Mainly used to display the content of the data, like <%= scripting-language%>
    • <C:SET>: Primarily used to store variables in the JSP scope or in JavaBean properties
    • <c:remove>: Mainly used for removing variables
    • <c:catch>: Mainly used to deal with abnormal conditions that produce errors, and to store the error information (less)

code example:

    • <c:out> tags

The new Javaweb Project test08,index.jsp code is as follows:

1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2     pageencoding=" UTF-8 "%> 3     <% @taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl /core "%> 4 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 5      request.setattribute (" name "," Smyhvae "); %>12      <c:out value= "${requestscope.name}" ></c:out>15  </body>17 

Lines No. 03, 10, and 14 are the code I added.

03 Lines (Fixed code): Use Jstl do not forget to introduce the tag library. Prefix= "C" is the prefix, "C" represents the expression "<c:out>". The URI is populated with namespaces, and you can choose Http://java.sun.com/jsp/jstl/core with code hints. This URI can be found in the following way: Many TLD-formatted files in the Java reasources/libraries/web App Libraries/jstl.jar/meta-inf directory, which refer to the tag library's declaration file:

The C.tld file in the box represents the core tag library, which opens it to see the URI:

In fact, the No. 03 line of code is fixed write dead, add can, you do not have to delve into.

Once the core tag library is introduced, in the body tag, as long as you enter "<c", you can see many tags through code hints.

We add a property to request on line 10th, and then we can output this value through the Out output label on line 14th (or, of course, with an El expression) and run the program with the following effect:

    • <c:set> tags
1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2     pageencoding=" UTF-8 "%> 3     <% @taglib prefix=" C "uri=" http://java.sun.com/jsp/jstl/ Core "%> 4 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 5      <c:set var= "age" value= "></c:set>"     ${age}  </body>22 

See 18, 19 lines of code.

The effect is as follows:

2. Process Control:

    • <c:if>: Use the same as we normally do in programs
    • <c:choose>: Only as parent tags for <c:when> and <c:otherwise>
    • <c:when>/<c:otherwise>: When using <c:when> and <c:otherwise> for Process control, both must be <c:choose> Sub-label (similar to switch Case )

Create a new Index2.sp file. The code is as follows:

 1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2 pageencoding=" UTF-8 "%> 3 <% @taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%> 4 &L t;! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 5 

15 to 22 behavior if the label (and the function of the IF statement is the same), the contents of the test in 16 rows must be a Boolean: We added the value of name in line 10th, and then judged whether the value of name was empty in line 16 and 20.

25 lines to 38 behavior choose tags (and switch Case function is the same).

To run the program, the effect is as follows:

3. Iterative operation:

    • <c:forEach>: Loop traversal, which allows the members of a collection (Collection) to be browsed through sequentially. The mode of operation is that when the conditions are met, the <c:forEach> 's ontology content is continuously repeated.
    • <c:fortokens>: Used to browse all members of a string, whose members are delimited by the definition symbol (delimiters).

code example:

    • <c:forEach>

Creates the new class user as the traversed object. The User.java code is as follows:

1 package Com.vae.bean; 2  3 public class User {4     private string name, 5     private string sex, 6     private int age, 7 public     User ( {8         super (); 9     }10 public     User (string name, String sex, Int. age) {One         super ();         this.name = name;13< C10/>this.sex = sex;14         this.age = age;15     }16 public     String GetName () {~         name;18     '}19< c16/>public void SetName (string name) {         this.name = name;21     }22 public     String getsex () {%         return sex;24     }25 public     void Setsex (String sex) {$         this.sex = sex;27     }28 public     int getage () {29< C26/>return age;30     }31 public     void Setage (int.) {This.age         = age;33     }34     @Override35 Public     String toString () {         name= "User [+ +] + name +", sex= "+ Sex +", age= "+ Age +"] "; PNs     }38 39     40}

Create a new file index3.jsp with the following code:

 1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2 pageencoding=" UTF-8 "%> 3 <% @page import=" com.vae.bean.user,java.util.* "%> 4 <% @taglib Pre Fix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%> 5 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 6 <c:foreach items= "${requestscope.users}" var= "user" >32 <tr>33 <td> ; ${user.name}</td>34 <td>${user.sex}</td>35 <td>${user.age}</td >36 </tr>37 </c:forEach></body>39 

The core code is 31 lines to 37 rows.

We add the values of multiple user objects to the list in 12 to 19 rows, and put them in the request. Then add a table in line 24th and iterate through the contents of the user object through <c:forEach> in the table, where the two properties in <c:forEach> are used: 31 lines of "items" represent collections that need to be iterated, "Var" Represents a variable that is defined every time a fetch is taken.

To run the program, the effect is as follows:

Properties in <c:forEach>:

We used 31 of the two properties in the above code for <c:forEach>: Items and var. Next, learn about the other properties:

    • Begin: Indicates the index from the beginning of the first few, generally not written;
    • End: Indicates the ending position. If you need to traverse 10 rows, write 10 here.
    • Step: Increment.

The above three attributes are less.

    • Varstatus: The current state. Its state has the following several:

Index indexes starting from 0

Count starts from 1 current traversal count

Current object that is currently being iterated

Whether first is

Is last the final one?

Let's give Varstatus an example. We add further in the index3.jsp code above:

INDEX3.JSP:

 1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2 pageencoding=" UTF-8 "%> 3 <% @page import=" com.vae.bean.user,java.util.* "%> 4 <% @taglib Pre Fix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%> 5 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 6 <th> Status Value </th></tr>32 <c:foreach items= "${requestscope.users}" var= "User"varstatus= "status">34 <tr>35 <td>${user.name}</td> <td> ${user.sex}</td> Notoginseng <td>${user.age}</td> 38<td>39 Index: ${status.index}<br/>40 count: ${status.count} <br/>41 Current: ${status.current}<br/>42 first: ${status.first}<br/>4 3 Last: ${status.last}<br/>44 </td></tr>46 </c:foreach>47 </table>48 </body>49  

The bold portion of 30 rows, the Varstatus attribute in 33 rows, and 38 lines to 44 lines are the code I added further. To run the program, the effect is as follows:

Through the above picture, and then to review the various states of the varstatus, it is clear at a glance:

    • Index indexes starting from 0
    • Count starts from 1 current traversal count
    • Current object that is currently being iterated
    • Whether first is
    • Is last the final one?

code example:

    • <c:forTokens>

Create a new index4.jsp file with the following code:

INDEX4.JSP:

1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2          <c:set var=" likes "value=" Badminton, photography, music "></c:set>13     <c:fortokens items= "${likes}" delims= "," var= "myvalue" >14         ${myvalue}<br/>15     </c:forTokens></ Body>17 

12 Line: The key of the string is likes, the value is "badminton, photography, music".

13 rows: Iterates through the strings in items, and the comma in Delims is the delimiter.

To run the program, the effect is as follows:

"Engineering Documents"

Links: http://pan.baidu.com/s/1qWNU6Xe

Password: itqn

Javaweb Learning notes 11--jstl Tag Library

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.