JSTL (JSP standard tag Library), JSP standards tag libraries, can be embedded in the JSP page using the form of tags to complete business logic and other functions.
The purpose of the JSTL is to replace the script code in the JSP page, as is the case with El.
The JSTL standard standard Tag Library has 5 sub-libraries, but with the development, his core library is often used today
1. Jstl Download and Import
Jstl Download:
Download the Jstl jar package from the Apache website. Enter
The "http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/" URL downloads the JSTL installation package.
Jakarta-taglibs-standard-1.1.2.zip, and then unzip the downloaded JSTL installation package, you can see two jar files in the Lib directory, respectively, Jstl.jar and Standard.jar.
Where 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 5 tag Library descriptor files (TLDs) in Jstl
Import two jar packages into the lib of our project
How to import JSP files:
<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>
2. Common tags for jstl core libraries
Examples of If tags:
<% Request.setattribute ("Count", ten); %> <!--Jstl tags are often used with El-- <!--test represents the return Boolean expression-- <c:if test= "${count==9}" > xxxx </c:if>
For loop Example:
<!--foreach analog for (int i=0;i<=5;i++) { System.out.println (i)}--- <c: ForEach begin= "0" end= "5" var= "I" > ${i}<br/> </c:forEach>
Enhanced for Loop:
<!--analog enhancement for productlist---list<product> for (Product product:productlist) { System.out.println (Product.getpname ()); } -<!--items: A collection or array var: represents an element in the collection-- <c:foreach items= "${productlist}" var= "Pro" > ${pro.pname} </c:forEach>
Simple application of the IF label:
Displays different information depending on whether the user is logged on or not:
<% //Impersonate the user has logged on successfully to user user = new user (); User.setid (+); User.setname ("Zhang San"); User.setpassword ("123"); Session.setattribute ("user", user); %>
<!--user not logged-in <c:if test= "${empty user}" > <li><a href= "login.jsp" > Login </a> </li> <li><a href= "register.jsp" > Registration </a></li> </c:if> <!-- User is logged-in <c:if test= "${!empty user}" > <li>${user.name}</li> <li><a href= "#" > Exit </a></li> </c:if>
Example of a For loop (ForEach) Label:
<%//Analog list<string> strlist list<string> strlist = new arraylist<string> (); Strlist.add ("Qwer"); Strlist.add ("asdf"); Strlist.add ("ZXCV"); Strlist.add ("1234"); Request.setattribute ("Strlist", strlist); Traverse the value of list<user> 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); Traverse the value of map<string,string> map<string,string> strmap = new hashmap<string,string> (); Strmap.put ("name", "Lucy"); Strmap.put ("Age", "18"); Strmap.put ("addr", "China"); Strmap.put ("Email", "[Email protectED] "); Session.setattribute ("Strmap", Strmap); Traverse the value of map<string,user> map<string,user> UserMap = new hashmap<string,user> (); Usermap.put ("user1", User1); Usermap.put ("User2", user2); Request.setattribute ("UserMap", UserMap); %> JSP standard Tag library: JSTL