Java Learning Chapter--jstl Foundation

Source: Internet
Author: User

jstl what?

JSTL (JSP standard TAGLIBRARY,JSP Standards Tag library) is a constantly intact open source JSP tag library.

Why do you use Jstl

Our JSPs are handy for developing information presentation pages, and embed Java code (scriptlet, expressions, and declarations) code to implement related logic controls.

See the following procedure. However, this can lead to problems such as the following:

JSP maintenance difficult to add, error prompts do not understand. Not easy to debug; The Division of labor does not understand, (that is, JSP developers are art, is also a program ape);

Finally add the program development cost;


<% if (Session.getattribute ("user"). Equals ("member")) {%>  <p>welcome, member!</p>  <%} else {%>  <p>welcome, guest!</p>  <%}%>

Origin:

The original intention of JSTL is to separate the foreground artwork from the development. Very good to achieve the goal of layered development. We know that most artists are familiar with the label.


JSTL's biggest advantage

1: Make the page code look neater. Display logic at a glance, style unified, easy to maintain

2 : Large numbers of script code (Scriptlets) in JSP can be greatly reduced by using the logical operations provided by JSTL (such as iteration, inference)

3 : Very good implementation of the idea of layering!

4 : Easier for artists to learn and master, because part of the art work is to deal with a variety of labels

5 : And the same can be defined on its own extension!


Disadvantages:


1 , for the front-office artists, maintenance staff added the learning cost

use a brief introduction:

Assuming you want to use JSTL, you must refer to the Jstl.jar and Standard.jar two packages.

and add the following at the top of the JSP file:


<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>   <%@ taglib uri= "http://java.sun.com/ Jsp/jstl/fmt "prefix=" FMT "%>  

Main label Categories:

<c:> Core Tag Library

Contains labels related to expressions, process labels when output JSP page content, iteration action labels, generating and manipulating URL tags.

The JSTL Core Tag library (c tag) label has 13, functionally divided into 4 categories:

1. Expression control Tags: out, set, remove, catch

2. Process Control Label: If, choose, when, otherwise

3. Loop Label: ForEach, Fortokens

4.URL action tag: import, URL, redirect

<fmt:> Formatting Tag Libraries

Defines the operations used to format data (especially numbers and dates) and also supports the internationalization of JSP pages using localized resource bundles


<fn:> function Tag Library

Provides a set of frequently used El functions that are used primarily to process strings and that can be used directly in JSPs


Jstl is generally used in conjunction with EL, so look at El first.


EL (Expression Language) Purpose: To make JSP easier to write


Syntax structure: ${expression}

Example: <%=request.getparameter ("username")% > equivalent to ${param. Username}


Let's apply JSTL to change the JSP page:

Use JSTL Before:


<td><select name= "category" Class= "Select1" id= "category" ><%for (int i=0; I<itemcategorylist.size ( ); i++) {itemcategory IC = (itemcategory) itemcategorylist.get (i); String selectedstring = "", if (Item.getitemcategory (). GetId (). Equals (Ic.getid ())) {selectedstring = "selected";} %><option value= "<%=ic.getid ()%>" <%=selectedstring%>><%=ic.getname ()%></option ><%}%></select></td>


Use JSTL after:


<td><select name= "category" Class= "Select1" id= "category" ><!--traverse the list array. Store values in Itemcategory--><c:foreach items= "${itemcategorylist}" var= "Itemcategory" ><c:set var= " Selectedstring "/><!--assume item.itemCategory.id equals itemcategory.id--><c:if test=" ${item.itemcategory.id eq Itemcategory.id} "><!--store the value of selected in selectedstring variable--><c:set value=" selected "Var=" selectedstring " /></c:if><option value= "${itemcategory.id}" ${selectedstring}>${itemcategory.name}</option> </c:forEach></select></td>

Summary:


for JSTL say. It further splits the JSP page. isolate the Java Code in the JSP page without having to be interested in The implementation of Java code. Just consider the beautification of the interface can be, so the responsibility of a single, the division of labor understand.

in doing DRP when the project is summarized. In fact, many of the techniques we learn today are actually abstract, expanding and extending. is for the reuse of the program and the ease of adding! We are all constantly in the abstract. Keep summarizing. Continue to learn with simplicity! Make our programs more change-oriented.




Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Java Learning Chapter--jstl Foundation

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.