Servlet & JSP (20)

Source: Internet
Author: User
Tags xslt

Jstl has a lot of content. This is the third article devoted to it. Although it is a bit more, as an important part of JSP development, we still have to patiently continue to learn. This article mainly discusses the SQL tag library and XML tag library.

Although we have discussed the MVC development mode before, we usually place database operations in the an component as a model, but for small and simple applications, you may need to write the Database Access Code directly on the JSP page. Jstl provides the SQL tag library, which makes it easier for us to perform database operations. To use the SQL tag library, you must add the following statement:

<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>

The SQL tag library contains <SQL: setdatasource>, <SQL: Query>, <SQL: param>, <SQL: dateparam>, <SQL: Update>, and <SQL: transaction> six tags.

<SQL: setdatasource> tag

It mainly sets the data source. The syntax format is as follows:

<sql:setDataSource dataSource="datasource" url="jdbcurl"[driver="driverClassName"][user="username"][password="password"][var="varName"][scope="{page|request|session|application}"] />

<SQL: Query> tag

<SQL: Query> tags are used to query databases. It has three syntax formats.

1) No tag body

<sql:query sql="sqlquery" var="varName"[scope="{page|request|session|application}"] [dataSource="datasource"][maxRows="maxRows"][startRow="startRow"] />

2) There is a TAG body and the query parameters are executed in the TAG body.

<sql:query sql="sqlquery" var="varName"[scope="{page|request|session|application}"] [dataSource="datasource"][maxRows="maxRows"][startRow="startRow"] >    <sql:param> action</sql:query>

3) There is a TAG body. Specify query statements and query parameters in the TAG body.

<sql:query  var="varName"[scope="{page|request|session|application}"] [dataSource="datasource"][maxRows="maxRows"][startRow="startRow"] >    query optional<sql:param> action</sql:query>

<SQL: param> label

This label is used to set the mark as? In an SQL statement? Is similar to the setxxx () method of preparedstatement. It is used as a sub-tag of <SQL: Query> and <SQL: Update>. The syntax format is as follows:

<sql:param value="value" />

<SQL: dateparam> label

This label uses a value of the date type to set the SQL statement to mark? . It is used as a sub-tag of <SQL: Query> and <SQL: Update>. The syntax format is as follows:

<sql:dateParam value="value" [type="{timestamp|time|date}"] />

<SQL: Update> label

This label is used to execute insert, update, or delete statements. It also has three syntax formats. The first syntax is provided here. For more information, see <SQL: Query>. The syntax format is as follows:

<sql:update sql="sqlUpdate" [dataSource="dataSource"][var="varName"][scope="{page|request|session|application}"]  />

<SQL: Transaction> label

This label is used to create a transaction processing context for the <SQL: Query> and <SQL: Update> subtags. The syntax format is as follows:

<sql:transaction [dataSource="dataSource"] [isolation=isolationlevel]>    <sql:query> and <sql:update> statements</sql:transaction>isolationlevel = "read_committed | read_uncommitted | repeatable_read | serializable"

XML tag Library

XML allows us to operate XML documents without having to know about Dom and sax. Of course, to use it, you must also add the following statement:

<%@taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>

The XML tag library is based on the XPath language and we can use the XPath expression in the select attribute of the tag (xalan processor needs to be configured to copy xalan. jar to the WEB-INF \ lib directory of the Web application ).

Expression syntax supported by the XPath Engine

Expression Ing
$ Foo Pagecontext. findattribute ("foo ")
$ Param: foo Request. getparameter ("foo ")
$ Header: foo Request. getheader ("foo ")
$ COOKIE: foo Map the cookie value to the name foo
$ Initparam: foo Application. getinitparameter ("foo ")
$ Pagination. foo Pagecontext. getattribute ("foo", pagecontext. page_scope)
$ Requestscope. foo Pagecontext. getattribute ("foo", pagecontext. requset_scope)
$ Sessionscope. foo Pagecontext. getattribute ("foo", pagecontext. session_scope)
$ Application. foo Pagecontext. getattribute ("foo", pagecontext. application_scope)
Through these mappings, JSP range variables, request parameters, request headers, cookies, and context initialization parameters can be easily used in XPath expressions. For example,/Foo/BAR [@ x = $ Param: Name] indicates to find the bar sub-element node under the foo element node and the value of the attribute X of the bar element, it is equal to the value of the HTTP request parameter name.

Tags in the XML tag library can be divided into three types by function: XML Core operations, XML flow control operations, and XML Conversion operations.

Core operations

Core XML operations include <X: param>, <X: Out>, and <X: Set>.

<X: param> label

<X: param> used to parse XML documents. There are two types of syntax.

1) specify an XML document through a string or reader object

<x:parse doc="XMLDocument" {var="var" [scope="scope" varDom="var" [scopeDom="scope"]]}[systemId = "systemid"][filter="filter"] />

2) Specify the XML document through the TAG body

<x:parse doc="XMLDocument" {var="var" [scope="scope" varDom="var" [scopeDom="scope"]]}[systemId = "systemid"][filter="filter"] >XML document  to parse</x:parse>

The scope value is the same as the previous one.

<X: Out> label

This label is used to calculate an XPATH expression and output the calculation result to the current jspwriter object. The tag function is similar to <% = expression %> or <C: Out> of the core tag library. The syntax format is as follows:

<x:out select="XPathExpression" [escapeXml]="{true | false}" />

<X: Set> label

This label is used to calculate an XPATH expression and save the calculation result to a range variable. The syntax format is as follows:

<X: Out select = "xpathexpression" [Var = "varname"]

[Scope = "{page | request | session | application}"]/>

Process Control

Including <X: If>, <X: Choose>, <X: When>, <X: otherwise>, and <X: foreach> labels. Similar to the <C: If>, <C: Choose>, and <C: foreach> labels in the core library. The difference is that the XML flow control operation applies the XPath expression.

<X: If> label

Tag syntax formats are divided into two types. There are tags and no tags. The format is as follows:

<x:if select="XPathExpression" [var="varName"][scope="{page|request|session|application}"] />

<X: Choose> tag

This label is used together with <X: When >,< X: otherwise> to implement mutex execution. The syntax format is as follows:

<x:choose>   body content<x:when> and <x:otherwise></x:choose>

<X: When> label

The syntax format is as follows:

<x:when select="XPathExpression" >body content</x:when>

<X: otherwise> tag

The tag syntax is as follows:

<x:otherwise select="XPathExpression" >conditional block</x:otherwise>

<X: foreach> label

This label calculates a given XPath expression and repeats its label body based on the result. The syntax format is as follows:

<X: foreach Var = "varname" select = "xpathexpression" varstatus = "varstatusname" begin = "begin" End = "end" step = "Step"> body content </X: foreach>

Conversion operation

The XML Conversion Operation supports the conversion of XML documents from XSLT style sheets, including <X: Transform> and <X: param> labels.

<X: Transform> label

It converts an XML document using the specified XSLT style. There are three syntax formats.

1) No tag body

<x:transform doc="XMLDocument" xslt="XSLTStyleSheet" [docSystemId="XMLsystemId"][xsltSystemId="[xsltSystemId"][var="varName"][scope="{page|request|session|application}"][result="resultObject"] />

2), 3) the format can be inferred by referring to the combination of <SQL: Query>.

<X: param> label

This label is used to set conversion parameters in the <X: Transform> label. Syntax format:

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

Reprinted please indicate the source: http://blog.csdn.net/iAm333

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.