JSP standard tag Library: JSTL, jsp standard tag jstl

Source: Internet
Author: User

JSP standard tag Library: JSTL, jsp standard tag jstl

JSTL (JSP Standard Tag Library), JSP Standard Tag Library, can be embedded in jsp pages to complete business logic and other functions in the form of tags.

The purpose of jstl is to replace the script code on the jsp page like el.

The JSTL standard tag library has five sub-databases. However, with the development, its core library is often used.

 

1. JSTL download and import

Download JSTL:

Download the jstl jar package from the Apache website. Enter

"Http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/# download the JSTL installation package.

Jakarta-taglibs-standard-1.1.2.zip, and then extract the downloaded JSTL installation package, at this time, under the lib directory can see two JAR files, respectively, jstl. jar and standard. jar.

The jstl. jar file contains the interfaces and related classes defined in the JSTL specification. The standard. jar file contains the. class file used to implement JSTL and the five tag library descriptor files (TLD) in JSTL)

Import two jar packages to the lib of our project

Jsp File Import method:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
2. Common labels of the JSTL core library

If tag example:

<% Request. setAttribute ("count", 10); %> <! -- Jstl labels are often used with el --> <! -- Expression of boolean returned by test --> <c: if test = "$ {count = 9}"> xxxx </c: if>

 

For Loop example:

<! -- ForEach simulate for (int I = 0; I <= 5; I ++) {System. out. println (I)} --> <c: forEach begin = "0" end = "5" var = "I" >$ {I} <br/> </c: forEach>

 

Enhance the for Loop:

<! -- Simulate enhancement for productList --- List <Product> for (Product product: productList) {System. out. println (product. getPname () ;}--> <! -- Items: a set or array var: represents an element in the set --> <c: forEach items = "$ {productList}" var = "pro" >$ {pro. pname} </c: forEach>

 

Simple Application of if labels:

Different information is displayed based on whether the user logs on:

<% // Simulate that the User has successfully logged on. user User = new user (); User. setId (100); user. setName ("Zhang San"); user. setPassword ("123"); session. setAttribute ("user", user); %>

 

<! -- The user has not logged on. --> <c: if test = "$ {empty user}"> <li> <a href = "login. jsp "> logon </a> </li> <a href =" register. jsp "> Registration </a> </li> </c: if> <! -- The user has logged on. --> <c: if test = "$ {! Empty user} "> <li >$ {user. name} </li> <a href = "#"> quit </a> </li> </c: if>

 

For Loop (forEach) label example:

<% // Simulate List <String> strList = new ArrayList <String> (); strList. add ("qwer"); strList. add ("asdf"); strList. add ("zxcv"); strList. add ("1234"); request. setAttribute ("strList", strList); // traverses the List <User> Value List <User> userList = new ArrayList <User> (); User user1 = new User (); user1.setId (2); user1.setName ("lisi"); user1.setPassword ("123"); userList. add (user1); User user2 = new User (); user2.setId (3); user2.setName ("wangwu"); user2.setPassword ("123"); userList. add (user2); application. setAttribute ("userList", userList); // traverses the Map <String, String> value Map <String, String> strMap = new HashMap <String, String> (); strMap. put ("name", "lucy"); strMap. put ("age", "18"); strMap. put ("addr", "China"); strMap. put ("email", "lucy@qq.cn"); session. setAttribute ("strMap", strMap); // traverses the Map <String, User> value Map <String, User> userMap = new HashMap <String, User> (); userMap. put ("user1", user1); userMap. put ("user2", user2); request. setAttribute ("userMap", userMap); %> 

 

Related Article

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.