Java Basic Development-jstl Tag Library

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

in the DRP project, the JSTL tag Library was contacted. Before using JSTL, we used JSP scripts to implement some declarations or expression tasks, and to do some business-related operations, we needed to embed a lot of Java code in the page. In the early development of the DRP project, the use of JSP page to my feeling is that the JSP page is capable of anything, the right is too high. The result is that JSP pages increase the complexity of the program, enhance the complexity of code maintenance, and the difficulty of reading code, and the requirements for programmers are relatively high, for example, although developers who are proficient in page design maintain JSP, they often need to rely on experienced programmers. 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. Wrap common functionality in a custom tag library collection. Unlike HTML tags, these tag libraries are primarily applied to the server side.

Jstl includes five types of standard tag libraries: Core library, format library, XML Library, SQL Library, function library. At the same time Jstl is used with El expressions,E L (expression Language) Purpose: To make jsps easier to write. Personally think 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, for example:

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 "for example:

<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:

The use of JSTL prerequisites requires the introduction of the Jstl.jar and Standard.jar two jar packages.


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 the El expression, usage: prefix + colon + function name): ${fn:length (Hello)}<br><p> <c:foreach items= "${fn:split (str, \" #\ ")}" var= "V" >${v}<br></c:foreach><p> Custom 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 programmers, it will not be the source of the power to discover the problem, what kind of demand, will create what kind of technology and tools. Jstl encapsulates some of the common features of Web applications, and when we use them, we don't need to understand how they are implemented internally, just the functionality that each tag represents on its label. Jstl's appearance weakens the JSP page too much right, makes the JSP page stratification more clear and appears.


Java Basic Development-jstl Tag Library

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.