What is a JSTL

JSTL stands for JSP Standard Tag Library.

As the most basic tag library, JSTL provides a series of JSP tags to achieve basic functions: collection traversal, data output, string processing, data formatting and so on!

Why use JSTL

  • EL expressions are not perfect and need JSTL support! In JSP, we have used EL expression in front, realized the powerful function of EL expression: ** Using EL expression can be very convenient to reference some Javabeans and their attributes, will not throw NullPointerException and other errors! ** However, the EL expression is so limited that it cannot iterate over the set to do logical control. At this point, JSTL support is needed!
  • **Scriptlet readability, maintainability and reusability are very poor! **JSTL is very similar to HTML code, following the XML tag syntax, using JSTL to make JSP pages appear clean, very readable, very high reuse, can complete complex functions!
  • Scriptlet output is not recommended in JSPS, JSP tags are recommended.

Steps for using the JSTL tag library:

  1. Import the jstl.jar and standard.jar development packages
  2. Use the tablib directive to import the JSTL tags needed in the JSP page

The core library

  • Core tag library is the core tag library of JSTL, realizing the most basic functions: flow control, iteration output and other operations!
  • Core tag libraries are usually prefixed with c

c:out

  • Just to use it a little bit

	<%
	    session.setAttribute("name"."zhongfucheng"); % >//
       The tag supports the tag body. The data on the default attribute can be written in the tag body
	/ / < c: out value = "${name}" escapeXml = "true" > the data you want find < / c: out >

	<c:out value="${name}" default="The data you asked for can't be found." escapeXml="true"/>

	

Copy the code
  • We found that the above code implements the same effect as the EL expression, ** it’s nice with two more properties,default and escapeXml. ** If we use the two attributes, we use the tag, if we don’t use the two attributes, we use the EL expression.

c:set

  • This tag has five attributes and is a little more complicated to use! The target attribute operates on javabeans or Map objects. The scope attribute represents a Web domain. The value attribute represents a value.

Using the var attribute

  • The value of the var attribute can only operate on types such as Integer, Double, and String. If the value of the var attribute exists, it must not have a property attribute.

  • The following code flow looks like this: create a variable named with the value zhongfucheng and the scope page


	<c:set var="name" value="fucheng" scope="page"/>
	
	${name}

Copy the code
  • Effect:

  • Of course, the set tag also supports the body, and the value can be written inside the body

	<c:set var="name" scope="page">
	    zhongfucheng
	</c:set>

Copy the code
  • Use the var and scope attributes to implement counters
<%-- the server does not know that my variable is of type Integer --%> <% Integer sessionCount =0;
	    Integer applicationCount = 0;
	%>
	<c:set var="sessionCount" value="${sessionCount+1}" scope="session"/>
	
	<c:set var="applicationCount" value="${applicationCount+1}" scope="application"/>

Copy the code
  • Effect:

Using the Target attribute

  • The target attribute is paired with the property attribute, which can only operate on Javabeans or Map objects. The property is the corresponding member variable or key.
  • Since the target attribute operates on a Javabeans or Map object, the object must be retrieved through an EL expression. The taget attribute throws an exception if it does not get data! If you use the target attribute, you must not have the scope attribute.
<%-- Create a JavaBean object and set it to a session-scoped property --%> < JSP :useBean id="person" class="domain.Person" scope="session"/> <%-- Get the person object and set the age property to32--%>
	<c:set target="${person}" property="age" value="32"/>
	
	${person.age}

Copy the code
  • Effect:

c:remove

The remove tag is fairly simple, with only var and scope attributes, which represent domain-scoped properties to be removed

  • Here’s a simple test:
Person :useBean id="person" class="domain. person" scope="session"/> <%-- Get the Person object, Set the age attribute has a value of 32 - % > < c: set target = "${person}" property = "age" value = "32" / > ${person. Age} < br > < % - delete the session attribute - % > < c: remove var="person" scope="session"></c:remove> ${person.age==null?" The person object in the session has been deleted!" "I'm still here!" }Copy the code
  • Effect:

c:catch

This tag is mainly used to handle exceptions raised in the program.

The catch tag is also very simple, with only one var attribute, which encapsulates the information about the exception!

<%-- creates a JavaBean object, Person" scope="session"/> <c:catch var="message"> <c:catch var="message"> <%-- the target attribute can only be an EL expression, now I am a string, can not get the object, will definitely throw an exception! --%> <c:set target="person" property="age" value="32"/> </c:catch> ${message}Copy the code
  • Effect:

c:if

JSTL provides an if tag to implement branch statements, and the test attribute is indispensable.

The var and scope attributes don’t seem very useful to me.

  • Display different pages depending on the parameters passed!
<c:if test="${param.name=='zhongfucheng'}"> <input type="text" name="username"> <input type="password" name="password"><br> <input type="password" >< / C :if> <% --%> <c:if test="${param.name=='ouzicheng'}"> <br> < form type="submit" value=" submit" >< /c:if>Copy the code
  • Pay attention to the parameters in the address bar!


c:choose

The if tag has no else functionality, so if you want something similar to the If else process in Java, you need to use the Choose tag.

The choose tag needs to be used in conjunction with the WHEN and otherwise tags!

<c:choose> <c:when test="${param.name=='zhongfucheng'}"> Zhongfucheng </c:when> <c:when test="${sister. name=='ouzicheng'}"> Don't come over here! </c:otherwise> </c:choose>Copy the code
  • Effect:


c:forEach

ForEach is a circular tag, the Java equivalent of while and for

  • Before, we used EL expression to get the data of the collection and traversed the collection with scriptlet code loop. Now we can discard scriptlet code by learning forEach tag.

  • Set properties to Session. Properties of type List collection


	<%
	    List list = new ArrayList<>();
	    list.add("zhongfucheng");
	    list.add("ouzicheng");
	    list.add("xiaoming");
	
	    session.setAttribute("list", list); % >Copy the code
  • Iterate through the List collection in the session property,items: the collection to be iterated over. Var: current iterated element

	<c:forEach  var="list" items="${list}" >
	    ${list}<br>
	</c:forEach>

Copy the code
  • Effect:

  • Traversing a Map object is slightly different. Let’s see, the var attribute does not hold the objects for each iteration, but map.Entry.


	<%
	    Map map = new HashMap();
	    map.put("1", "zhongfucheng");
	    map.put("2", "xiaohong");
	    map.put("3", "xiaoming");
	
	    session.setAttribute("map",map);
	%>
	
	<c:forEach  var="me" items="${map}" >
	
	    ${me.key}  ${me.value}<br>
	
	</c:forEach>

Copy the code

  • Begin starts at 0 by default, end is the last element of the collection by default, and step is 1 by default

  • VarStatus represents information about the current object being iterated over, and it has the following properties.

    • Index [returns the current number of objects, counting from 0]
    • Count how many objects have been traversed, counting from 1
    • 【 iS it the first one 】
    • Is it the last one?
    • Current the object currently being iterated over
    • Begin meaning in Chinese
    • 3. The last position
    • Step 【 step 】
<c:forEach var="list" items="${list}" varStatus="varStatus" > ${list}Copy the code
  • Effect:


c:forTokens

This tag is similar to a collection of split() and for loops for the String class

The items property of the forTokens tag contains strings, which are split into multiple strings by the delims property.


	<c:forTokens items="zhongfucheng,ouzicheng,xiaoming,xiaohong" var="name" delims="," >
	    ${name}
	</c:forTokens>

Copy the code
  • Effect:


c:import

Import tags are similar to JSP behavior<jsp:include/>And JSP directive<%include>

Import tag attributes:

  1. Url [Specify the path to include, all urls on the Internet can be used]
  2. Context [To access other resources in the same Web container, starting with a “/”]
  3. Var [save the contents of the imported file as String]
  4. Socpe [save range, default is page]
  5. CharEncoding [character coding]
  6. VarReader save the contents of the imported file as Reader

Of course, the import tag is even more powerful! Where is the power? Import tags can be imported from web pages on the Internet, which means CSDN can also be imported!

  • Let’s use it!

	<c:import url="http://www.csdn.net" charEncoding="UTF-8" />

Copy the code
  • When we looked, there was no pattern:

  • Print CSDN source code:
<c:import url="http://www.csdn.net" charEncoding=" utF-8 "var="net"/> CSDN <br><br><br><br><br> <c:out value="${net}" escapeXml="true"></c:out>Copy the code
  • Effect:


c:param

  • When performing URL-related operations on JSP pages, you often append parameters to the URL address. tags can be nested within < C :import>, < C: URL >, or < C: Redirect > tags, with additional parameters for the URL addresses used by these tags.

  • When the tag appends a parameter to a URL address, it will automatically URL-encode the parameter value. For example, if the parameter value passed is “China”, it will be converted to “%d6% D0 %b9% FA” and then append to the URL address. This is the biggest benefit of using the tag.


c:url

Url tags are very useful! When cookies are disabled in the browser, the solution we learned from servlets is response.encodeurl (). Url tag can also achieve such a function, and then with param tag use, it is very practical!

  • Let’s use it with the Param tag.
<c:url value="2. JSP "var="url"> > < / c: param > < / c: url > < a href = "${url}" > I passed the url rewrite! </a>Copy the code
  • Effect:


c:redirect

The Redirect tag is used for the redirect function and can also be used with the Param tag!

  • To use it briefly, redirect to 2.jsp with one parameter:

	<c:redirect url="2.jsp" >
	    <c:param name="name" value="zhongfucheng">
	    </c:param>
	</c:redirect>

Copy the code
  • The parameters are obtained in the 2.jsp


FMT tag library

The FMT tag library is also called the International tag library. I won’t go into details here, but I’ll cover it when I talk about Web internationalization.

Fn method library

Fn method library is also called EL function library, FN tag library. This is explained in detail when the EL expression, can be transferred to my EL expression blog!

Open source project (5.8K STAR) :Github.com/ZhongFuChen…

If you want to follow my updated articles and shared dry goods in real time, you can search Java3y on wechat.

The content of the PDF document is typed by hand. If you don’t understand anything, you can ask me directly (the official account has my contact information).