Java Basic Development-jstl Tag Library

Source: Internet
Author: User
Tags control label time and date

In the DRP project. Access to the JSTL tag library.

Before using JSTL, we used JSP scripts to implement some declarative or expression tasks, and to do some business-related operations, we had to embed a lot of Java code in the page. Prior to the development of the DRP project. Use JSP page to give me the feeling is. JSP page What work is capable, the right is too high. The result is that JSP pages improve the complexity of the program and enhance the complexity of code maintenance. And the difficulty of reading code. The requirements for procedural apes are also relatively high. For example, although a developer proficient in page design to maintain JSP. It is also often necessary to rely on an experienced program ape. So you need a technique to solve these problems, here's a brief introduction to Jstl:


JSTL (JSP standard tag Library) is the JSP introduced by The Sun Company a standardized tag libraries. You will often use features to wrap to a custom tag library collection.

These tag libraries are different from HTML tags. It is primarily applied with the server side.

Jstl contains five types of standard tag libraries: Core library, format library, XML Library, SQL Library, function library. At the same time Jstl is used in conjunction with El expressions,E L (expression Language) Purpose: To make jsps easier to write.

Personally feel that it does not belong to Jstl, but jstl use El expression, together more convenient to serve the JSP.


El expression

Syntax: $ and {}

Built-in objects: Pagescope,requestscope,sessionscope,applicationscope

Empty operator: Determines whether an object, collection, or string variable is empty or null, such as:

Servlet definition: Request.setattribute ("V2", "" ");

jsp:v1:${empty v1}<br>

"." and "[]" two operators to access the data : For example:

${user.username}    

${users[5].username}

Operator: "==/eq!=/ne </lt >/gt &&/and | | /or!/not//div%/mod "such as:

<li>el expression support for operators </li><br>1+1=${1+1}<BR>10/5=${10/5}<br>10 Div 5=${10 Div 5}<br >10% 3=${10% 3}<br>10 mod 3 = ${mod 3}<br>

Jstl Tag Library:

It is necessary to introduce the Jstl.jar and Standard.jar two jar packages in the use of jstl premises.


1. Core Library: Introduction of <%@ taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "C"%>

C:out Tags: hello (using tags): <c:out value= "${hello}"/><br>

C:set Tags: <c:set value= "root" var= "userid"/>

C:remove Tags: <c:remove var= "userid"/>

Condition control Tags: c:if, c:choose, C:when

<c:choose><c:when test= "${v1 GT v2}" >v1 greater than v2<br></c:when><c:otherwise>v1 less than v2<br ></c:otherwise></c:choose><c:choose><c:when test= "${empty userList}" > No qualifying Data <br ></c:when><c:otherwise> presence of user data <br></c:otherwise></c:choose>

Loop control label

<c:choose><c:when test= "${empty users}" ><tr><td colspan= "3" > No qualifying Data </td></tr ></c:when><c:otherwise><c:foreach items= "${users}" var= "User" ><tr><td>${ User.username}</td><td>${user.age}</td><td>${user.group.name}</td></tr> </c:forEach></c:otherwise></c:choose>


2. Function Library: Introduction of <%@ taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "FN"%>

<body>hello.length (using the length function of Jstl, the function must be placed in an El expression, using the method: Prefix + colon + function name): ${fn:length (Hello)}<br><p> <c:foreach items= "${fn:split (str, \" #\ ")}" var= "V" >${v}<br></c:foreach><p> self-defined function: ${my:say ("Zhang San")} <br></body>


3. Format Library: Introduction of <%@ taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "FM"%>

Here are three ways to get time and date

Today (type= "date"): <fmt:formatdate value= "${today}" type= "date"/><br>today (type= "Time"): <fmt: FormatDate value= "${today}" type= "Time"/><br>today (type= "both"): <fmt:formatdate value= "${today}" type= "Both"/><br>


To traverse a drop-down box instance in a DRP project:

1. Jstl not used

<TR><TD height= "><div" align= "right" ><font color= "#FF0000" >*</font> Category: </div ></td><td><%List<ItemCategory> itemcategorylist = (List) request.getattribute (" Itemcategorylist "),%><select name=" category "Class=" Select1 "id=" category "><%for (iterator< Itemcategory> iter = Itemcategorylist.iterator (); Iter.hasnext ();) {itemcategory IC = Iter.next ();%><option Value= "<%=ic.getid ()%>" ><%=ic.getname ()%></option><%}%></select></td> </tr>

2. Using Jstl

<TR><TD height= "><div" align= "right" ><font color= "#FF0000" >*</font> Category: </div ></td><td><select name= "category" Class= "Select1" id= "category" ><c:foreach items= "${ Itemcategorylist} "var=" Itemcategory "><option value=" ${itemcategory.id} ">${itemcategory.name}</ Option></c:foreach></select></td></tr>


Summarize:

For the program Ape. Will not be the source of power to find the problem. What kind of demand. What kind of technology and tools will be created. Jstl encapsulates some of the common features of Web applications. When we use it, we do not have to understand how it is implemented internally, just need to understand the function that each label on its label represents.

The advent of JSTL weakens the JSP page too much right. Make the JSP page layering clearer and appear.


Java Basic Development-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.