Jstl usage Summary (1) Core tag library and SQL tag Library

Source: Internet
Author: User
Tags xpath tld
Document directory
  • 1. <SQL: setdatasource>
  • 2. <SQL: Query>
  • 3. <SQL: Update>
  • 4. perform functions similar to preparedstatement

Jstl is the standard tag library of JSP. It provides many very practical functions, saving us the trouble of customizing tags, so that there is no scriptlet In the JSP page.

Jstl is just convenient for our development. If it is not used, it will not hinder our development. Generally, jstl is used together with the expression language.

The jstl1.2 package can be obtained when struts is configured in myeclipse.

1. jstl Configuration

Put jstl. jar into Tomcat \ Lib, and put the TLD file in jstl. jar into the WEB-INF. The TLD files provided by him include:

(1) c. TLD core tag library, which provides operations such as iterative output.

(2) format FMT. TLD.

(3) SQL. TLD provides SQL query, update, and other operations.

(4) X. TLD provides XML operations.

(5) fn. TLD provides string function operations, such as contains and startswith.

Ii. Core tag Library

1. <C: Out>

Used for output. The general structure is as follows:<C: Out value = "" escapexml = "" default = ""/>

Value indicates the value to be output;

Escapexml indicates whether to convert the format <to "& lt ";

Default indicates that if value is null, this statement is output;

2. <C: Set>

It is used to set built-in object attributes or custom object attributes. The general structure is as follows:

(1)<C: Set Var = "" value = "" Scope = ""/>

VaR indicates the attribute name;

Value indicates the attribute value;

Scope indicates the storage range;

(2)<C: set target = "" property = "" value = ""/>

Target indicates the object name;

Property indicates the property name;

Value indicates the attribute value;

3. <C: Remove>

Used to remove built-in object attributes. The general structure is as follows:

<C: Remove Var = "" Scope = ""/>

VaR indicates the attribute name;

Scope indicates the attribute range;

4. <C: Catch>

Used to catch exceptions, similar to try-catch. The general structure is as follows:

<C: Catch Var = "">

Statement

</C: Catch>

VaR is used to save the exception information;

5. <C: If>

Similar to the IF statement, the general structure is as follows:

<C: If test = "" Var = "" Scope = "">

Execution statement

</C: If>

Test indicates judgment;

VaR indicates saving the judgment result;

Scope indicates the storage range of the result;

6. <C: Choose>

It is used for multiple judgments. The general structure is as follows:

<C: Choose>

<C: When test = ""> <! -- Used to determine -->

Statement

</C: When>

<C: When test = "">

Statement

</C: When>

<C: otherwise> <! -- If none of the when is available, go to -->

Statement

</C: otherwise>

</C: Choose>

7. <foreach>

Used for iterative output. The general structure is as follows:

<C: foreach items = "" Var = "" [begin = ""] [END = ""] [step = ""]>

$ {Var}

</C: foreach>

Items indicates the output set;

VaR indicates the iterator element;

Begin indicates the starting index;

End indicates the end of the index;

Step indicates the output interval;

8. <C: fortokens>

It is used to separate strings and iterate the output. The general structure is as follows:

<C: fortokens items = "" delims = "" Var = "">

$ {Var}

</C: fortokens>

Items indicates a string;

Delims indicates the delimiter;

VaR indicates the iterator;

9. <C: Redirect>

For client jump, the general structure is as follows:

<C: Redirect url = "">

<C: Param name = "" value = ""/> parameters can be passed;

</C: Redirect>

10. <C: Import>

Contains a webpage. The general structure is as follows:

<C: Import url = "" charencoding = "" Var = "">

<C: Param name = "" value = ""/>

</C: Import>

VaR indicates the saved import content object.

Code example:

<% @ Page contenttype = "text/html" pageencoding = "GBK" Import = "Java. util. * "%> <% @ taglib prefix =" C "uri =" jstl/C "%> <JSP: usebean id =" per "class =" org. person. person "Scope =" page "/> <HTML> Iii. SQL tag Library

 

The function of the SQL tag library is to use only one tag to connect to the database, add, delete, modify, and query the database. This is very convenient, but violates the MVC design pattern: JSP only supports display operations.

 

1. <SQL: setdatasource>

 

This label is used for database connection, as shown in the following figure:

<SQL: setdatasource driver = "com. mySQL. JDBC. driver "url =" JDBC: mysql: // localhost: 3306/xiazdong "user =" root "Password =" 12345 "Var =" ds "/>

In this way, the database is connected.

 

2. <SQL: Query>

 

This label is used for query, for example:

<SQL: Query SQL = "" Var = "result" datasource = "$ {DS}" [startrow = ""] [maxrows = ""]/>

The result in Var stores the result. How can it be displayed after query?

Startrow indicates the row to be displayed.

Maxrows indicates the number of rows that can be displayed on a page.

Therefore, this label can be used for paging.

 

<C: foreach items = "$ {result. Rows}" Var = "ITER">

$ {ITER. name}

$ {ITER. Age}

</C: foreach>

The output can be iterated.

$ {Result. rowcount} The number of returned rows;

3. <SQL: Update>

 

This label is used to update database records, for example:

<SQL: Update SQL = "" Var = "result" datasource = "$ {DS}"/>

 

4. perform functions similar to preparedstatement

 

Can preparedstatement pass? Fill in SQL statements, and then set through set. It can also be implemented in jstl,

Pass<SQL: Param value = ""/> and <SQL: dateparam type = "date" value = ""/>

Shape:

Request. setattribute ("N", "xiazdong ");

<SQL: Query SQL = "Select name from EMP where name =? "Var =" result "datasource =" $ {DS} ">

<SQL: Param value = "$ {n}"/>

</SQL: Query>

 

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ taglib prefix="c" uri="jstl/c"%><%@ taglib prefix="sql" uri="jstl/sql"%>

Iv. xml tag Library

The role of the XML tag library is to be able to perform simple operations on XML files and encapsulate complex steps of Dom or sax. For example, Dom parsing must be: documentbuilderfactory, documentbulder, and a series of other preparations; jstl only needs one label.

1. Introduction to xpath

In the XML tag library, you must use XPath. xml files are like a tree. XPath can find a node in XML.

/Indicates the root node;

// Represents any path of the subnode;

../Indicates the parent node;

@ Indicates the property

2. <X: Out>

This label is used for output, such:

<X: Out select = ""/>

The content in select is XPath. We need to find a node and output its content.

3. <X: parse>

This label is usedAnalysisXML file, such:

<X: parse Var = "" Doc = ""/>

Doc indicates the XML file object;

VaR indicates the parsed root node;

Generally:

<C: Import url = "1.xml" Var =" xml1 "/>

<X: parse Doc = "$ {xml1}" Var = "root"/>

Import and parse;

4. <X: Set>

Save the content of a node in XML to an attribute, for example:

<X: Set select = "" Var = "" Scope = ""/>

Select indicates the XPath path;

VaR indicates the saved property;

Scope indicates the storage range;

5. <X: Choose>

This tag is similar to <C: Choose> in the core tag library, and has multiple judgments;

<X: Choose>

<X: When select = ""> // whether the XPath path exists

......

</X: When>

<X: otherwise>

</X: otherwise>

</X: Choose>

6. <X: foreach>

Iterative output content, such:

<personlist><person><name id="1">xiazdong</name><age>20</age></person><person><name id="2">xzdong</name><age>15</age></person></personlist>

We can iteratively output the person content, because there are two persons;

The code for outputting the above person node through iteration is as follows:

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ taglib prefix="c" uri="jstl/c"%><%@ taglib prefix="x" uri="jstl/x"%>

 

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.