Create a JSP 2.0 tag file

xiaoxiao2021-03-06  60

Creating JSP 2.0 tag files Author: Andrei Cioroianu

Learn how to use JSP, JSTL, and SQL to create reusable web templates and database scripts

Download the source code of this article

The tag file is one of the most important new features of JavaServer Pages (JSP) technology, which allows Web developers to create custom tag libraries with JSP syntax. The JSP container automatically converts the JSP tag file to Java code, which is the same as the process of transparently generating Java servlet from the JSP page. It can be said that the tag file hides the complexity of creating a custom JSP tag library. This library can be reused in a web application, which also provides significant benefits even when used for specific applications, as custom tags increase the maintenanceability of the web page. After reading this article, you will learn how to create and use tag files and how to convert existing page clips to tag files.

JSP 2.0 defines three new instructions (<% @ tag%>, <% @ attribute%> and <% @ variable%>) and two new standard operations ( and ) They can only be used in tag files. We will use them in the entire article, and you will learn how to use more advanced tag file features, such as "Dynamic Properties", "Fragment Properties", and "Variables Named depending on the attribute". When the tag file is used at the same time as the JSP Standard Tag Library (JSTL), the tag file can be a powerful tool, while JSTL introduces the expression language used by JSP 2.0 and many JSP operations, including a series of SQL tags. We will use JSTL and advanced JSP characteristics to create tag files for updating and querying the database.

Tag file overview

JSP 1.x allows Web developers to create Java components (called tag handles), which are called from JSP pages by custom tags. The tag handler is similar to the previous Java Servlet because you use a lot of Println () calls to generate HTML content, and then you must compile your Java code. The JSP 2.0 tag file is similar to the JSP page, because you use the JSP syntax, then the JSP container gets your JSP tag file, analyzes these tag files, generates the Java tag handler, and automatically compile them. The tag file is called from the JSP page, the JSP page uses a custom tag that matches the mode.

In order to enable the tag file to be identified by the JSP container, the tag file must be named using the .TAG file extension, and must be placed in your web application / web-inf / tags directory or / web-inf / tags In the subdirectory. If you use this deployment method, you don't have to create any tag library descriptor (TLD) because the JSP library is implemented using the Java tag handler. You can also place the tag file in the .jar file / Meta-INF / tags directory, it is easier, but in this case, you must create TLD and you must re-mark the tag file after each change. Bale.

The tag file and the JSP page use almost the same syntax. The first difference you will notice is the new <% @ tag%> indicator statement, it is equivalent to <% @ Page%>. These two indicator statements have similar properties, but the former is used to mark files, while the latter can only be used in the JSP page. The tag file is not in a separate .tld file to declare its properties and variables, but use <% @ attribute%> and <% @ variable%> indicating statements. When tag files from the JSP page, the custom tag can have the body (between and ), which can be performed by the marker file using the operation. You will see how this is working in the next section. Use tag file instead of page clip

In order to have maintainable web pages, many well-designed JSP 1.x applications use a page clip, which is included in the larger page with or <% @ INCLUDE%>. This is not an ideal method for reusing JSP fragments in a web application. Unlike the page containing solutions, tag files are dedicated to creating reusable page clips. This section compares the syntax of the Outdated JSP 1.x method and a better syntax for the JSP 2.0 tag file.

Will be included to transform to tag files. Let us assume that you have a page named Container.jsp, which uses the

Step 1: Mobile is included in the page. In your application's web-infirectory, create a subdirectory named Tags and move the Part.jsp page into the web-inf / tags.

Step 2: Rename is renamed is included. Since the standard tag file extension is .tag, you must rename part.jsp to part.tag.

Step 3: Edit the tag file. Open the part.tag file for editing, replace the <% @ page%> indicator to similar <% @ tag%> indicator statement. If you encounter a compilation error, you may need to do some other small modifications. Use JSPContext to replace PageContext.

Step 4: Declare the tag library. Insert <% @ Taglib prefix = "tags" tagdir = "/ web-inf / tags"%>.

Step 5: Call the tag file. Replace in Container.jsp is .

Container.jsp (before transition)

Container Page

Container.jsp (after transformation)

<% @ taglib prefix = "tags" tagdir = "/ web-inf / tags"%>

Container Page

Part.jsp (before transition)

<% @ page language = "java"%>

incruded Page

<% = pageContext.getClass (). getName ()%>

Part.tag (after transformation)

<% @ tag language = "java"%>

invoked tag

<% = jspContext.getClass (). getName ()%>

An example of and <% @ include%>. The home page of this example (main.jsp) uses the <% @ include%> indicating two pages (Header.jspf and Footer.jspf). The home page also contains another page (Content.jsp) by using . The main.jsp page provides three parameters (A, B, and C) that are included (A, B, and C) to be included with the standard operation. The Content.jsp page is included in $ {param.a}, $ {param.b} and $ {param.c} output three parameters.

Main.jsp

<% @ include file = "Header.jspf"%>

<% @ include file = "footer.jspf"%>

HEADER.JSPF

header

FOOTER.JSPF

footer

Content.jsp

incruded content

Parameters - $ {param.a}, $ {param.b}, $ {param.c}

Output

An example of a tag file. The JSP page (main.jsp) of this example uses the <% @ taglib%> indicating statement to declare the library containing the tag file, indicating its prefix (tags), and the directory of creating tag files (/ web-INF / Tags). Main.jsp Use the custom tag to call the wrapper.tag file, which has three properties (A, B and C) and some main content (

wrapped content ). Note that you may not use any script to prepare an element without any scripting. Script preparation elements include JSP 1.x declarations (<%! ...%>), JSP 1.x expression (<% = ...%>) and scripTlet (<% ...%>), while JSP 2.0 Supports all these elements. Note that the tag file can be used to use script to prepare an element like any regular JSP page. In fact, if you don't like the Java tag handler to create a JSP library, you will be a good solution from existing JSP page to tag files. This will make your Java Scriptlet can be reused and make your web page easier to read. The JSP container completes heavy work, which automatically generates tag handler classes. The wrapper.tag file is a substitute for three in the previous example containing the page (header.jspf, footer.jspf, and content.jsp). The tag file uses <% @ attribute%> indicating statements declares three properties, and outputs a title, a footer, and the value of the property ($ {A}, $ {B} and $ {C}). The Wrapper.Tag file uses the new standard operation, execute main.jsp of the main body. The tag file determines whether or when to perform the body of . If Wrapper.TAG does not use , the body of is ignored. The marker file can also be used multiple times for so that the main body is executed multiple times.

Main.jsp

<% @ taglib prefix = "tags" tagdir = "/ web-inf / tags"%>

Wrapped Content

Wrapper.tag

<% @ tag body-content = "scripTless"%>

<% @ Attribute Name = "a" required = "true"%>

<% @ Attribute Name = "b" required = "true"%>

<% @ Attribute Name = "c" required = "true"%>

header

attributes - $ {a}, $ {b}, $ {c}

footer

Output

Mark file contrast to and <% @ include%>. Call the custom tag of the tag file has a more tight syntax composite than and . Custom tags can also be nested and indented in natural ways, making the code easy to read. The only one that may feel inconvenience is a limit that does not allow you to use script to prepare an element in a custom tag entity that calls the tag file. Considering the script-free web page may easily maintain, in fact this may be favorable conditions. If you must mix the JSP and Java code, you can move the Java ScriptleT to the tag file.

<% @ TAG%>, <% @ attribute%> and <% @ variable%> indicate the benefits of more tag files:

The property declaration makes the code of the tag file more readily because you only need to find the name of the property by viewing the title file. For the included page, the parameters are not declared at the beginning of the page. If you must use the page developed by others and do not make a document for the parameter, you may have to analyze the source code for the entire page. By default, all properties are optional. If you need an attribute, you can specify it using <% @ attribute name = "..." request = "true"%>. By default, a custom tag that calls the tag file can contain a JSP code without a script. If the tag file does not use , you should use <% @ tag body-content = "empty"%> to specify that the tag file ignores the main content. This declaration prevents an accident in the and JSP content. In this case, the tag file should be called using empty markings such as . By indicating the desired properties and whether the main content is used, you can prevent many common errors, reduce the time spent on debugging the JSP page. This declaration support is not provided on the JSP page included. You can also use a statement similar to <% @ attribute type = "classname"%> to specify the type of attribute. The attribute value will be automatically converted to the specified type. If the conversion fails, an error will appear. If it is included, all parameters are strings, and all required conversions must be written. The new <% @ variable%> indicate statement provides a prompt to the JSP container, which automatically performs tasks such as the export and synchronization of the JSP variable, which helps to be called the communication between the tag file and the call page. We will understand how it works later in this article. If it is included in the JSP page, you must manually implement the communication between the pages, which usually requires more programming operations. <% @ attribute%> and <% @ variable%> indicate statements support those characteristics that cannot be used in JSP page, such as the dynamic properties collected in java.util.map, indicating the properties of the JSP fragment, and the name consisting of attributes The specified variable. These features are used in the examples in the next section.

Using advanced JSP 2.0 features

The following example shows how to develop the true functionality of the JSP tag file. You will learn how to process dynamic properties, how to use variables as an output parameter, how to use attributes to name variables and how to use properties to pass JSP segments to tag files. THEN, We'll Use these Advanced JSP Features To Build Another Set of Tag Files That Query and Update A Database Using The JSP Expression Language Withnin SQL Statements. Dynamic properties are processed in the tag file. This example contains a marker file (Dynamicattr.Tag), which accepts dynamic properties and the use of several other properties declared by <% @ Attribute> indicating statements (ListtAg, Itemtag, and Separetor). A JSP page (DynamiCattr.jsp) uses the tag to call the tag file. The declared attributes can be mixed with dynamic properties because the order of the attribute is not important.

By default, the tag file only accepts the declared properties. In order to support dynamic properties, the tag file must be used <% @ tag Dynamic-attributes = "varname"%> to provide a variable name that keeps all dynamic properties in java.util.map. In our example, the variable is named attrmap. The Dynamicattr.tag file uses JSTL's to overlap the ATTRMAP items, using $ {attr.key} and $ {attr.value} to get the names and values ​​of each attribute.

If you view the output, six dynamic properties (first, second, ... sixth) are not obtained in the order specified by tag in Dynamicattr.JSP. This is because Java.util.map implements the order of controlling its project. Therefore, the tag file must be prepared to handle dynamic properties in any order.

The Dynamicattr.tag file uses the standard operation to generate the HTML element that gets its name at runtime. This new JSP 2.0 feature is independent of dynamic attributes, but we use it in this example to generate an HTML list (ListTAG is "UL", while ItemTAG is "Li").

Dynamicattr.tag

<% @ Tag Dynamic-Attributes = "AttrMap"%>

<% @ attribute name = "listtag" required = "true"%>

<% @ attribute name = "itemtag" required = "true"%>

<% @ attribute name = "separator" required = "true"%>

<% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%>

Dynamic Attributes:

$ {attr.key} $ {separator} $ {attr.value}

Dynamicattr.jsp

<% @ taglib prefix = "demo" tagdir = "/ web-inf / tags / demo"%>

FigSt = "1" second = "2" third = "3"

Listtag = "ul" itemtag = "li"

FOURTH = "4" fiffh = "5" Sixth = "6" />

Output

Export variables from tag files to JSP pages. JSTL's tag allows you to create and change variables in four JSP scope (pages, requests, sessions, and applications). JSP Pages With those marking files called by the JSP page share requests, sessions, and applications. The page range is the default range of , which does not share. Therefore, you can view those variables created within the request, session, and application range as a "global variable", while the variables created within the page range as "local variables".

Since each tag file has its own page range, the local variable of the call page cannot be accessed in the called tag file, and vice versa. The JSP page can use tag properties and global variables to pass the parameters to the called tag. For each property, the JSP container creates a local variable in the page range of the tag file, allowing you to get the value of the property using the $ {attributeName} structure. Therefore, the tag attribute acts as a parameter transmitted by the value, and the global variable acts as the parameters passed by the reference. This change will affect the JSP page if the tag file changes the value of the global variable.

The tag file can use the <% @ variable%> indicator statement to "export" to the call page. You can use <% @ variable name-given = "..."%> to specify the name of the variable in the tag file, and the JSP page can also provide a variable name, we will see this in the VARATTR.JSP example. The type of variable can be indicated by <% @ variable variable-class = "..."%>.

When using <% @ variable%> Declaring variables in the tag file, you must use to set the value of the variable. The JSP container creates another variable with the name given in the JSP page, and the second variable is initialized by the corresponding variable value in the tag file. Changing the variable value of the JSP page does not affect the corresponding variable in the tag file. However, the JSP container updates the variable of the JSP page according to the range attribute of <% @ variable%>, which may be at_begin, nested, or at_end.

If Scope is AT_end, the variable of the JSP page is initialized using the tag file variables after the tag file. If Scope is at_begin, the variable of the JSP page is updated before before each and the execution of the tag file. If Scope is NESTED, only the JSP variable is updated using the value of the tag file variable before and each before each . After the tag file is executed, if it is the case where Nested, the JSP variable is restored to it has the value before calling the tag file. In order to better understand how variables work, let us use them in one example, which includes a tag file (VARSCope.jsp), a JSPE.JSP, and several segments (VarscopeHeader. JSPF, VARSCOPEFOOTER.JSPF and VARSCOPEROW.JSPF). These snippets are only used to create an HTML table. When we study mark file variables in this example, we must use <% @ include%> to separate logic from expressions. In most cases, using tag files is the best solution, but in this case, additional tag files interfere with the variable "export" mechanism we have to study.

The tag file uses a "global" variable (GV), an undeclared "local" variable (LV), and three variables declared by <% @ variable%> (BV, NV, and EV). Varscope.tag files initialize these variables using "Tag A", and then use , and finally change all the values ​​of all variables to "TAG B" string. Varscope.jsp Pages initialize their own range of variables, then use to call the tag file, change the value of the variable to "JSP B" within the main body of . As a result, the output is included behind the source code.

Varscope.tag

<% @ variable name-given = "bv" scope = "at_begin"%>

<% @ variable name-given = "nv" scope = "nested"%>

<% @ variable name-given = "EV" scope = "at_end"%>

<% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%>

<% @ include file = "VARSCOPEROW.TAGF"%>

<% @ include file = "VARSCOPEROW.TAGF"%>

<% @ include file = "VARSCOPEROW.TAGF"%>

<% @ include file = "VARSCOPEROW.TAGF"%>

VARSCOPEROW.TAGF

"$ {Lv == gv and lv == bv and lv == nv and lv == EV}" />

$ {lv! = NULL? LV: "null"}

$ {GV! = NULL? GV: "null"}

$ {Lv! = NULL? LV: "null"}

$ {BV! = NULL? BV: "null"}

$ {Nv! = Null? NV: "null"}

$ {EV! = NULL? EV: "null"}

Varscope.jsp

<% @ taglib prefix = "demo" tagdir = "/ web-inf / tags / demo"%>

<% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%>

<% @ include file = "VARSCopeHeader.jspf"%>

<% @ include file = "VARSCOPEROW.JSPF"%>

<% @ include file = "VARSCOPEROW.JSPF"%>

<% @ include file = "VARSCOPEROW.JSPF"%>

<% @ include file = "VARSCOPEROW.JSPF"%>

<% @ include file = "VARSCOPEFOTER.JSPF"%>

VarscopeHeader.jspf

GV
Global Var
in Request
Scope

LV
Local Var
NOT
DECLARED

BV
Tag Var
Scope:
AT_BEGIN

NV
Tag Var
Scope:
Nested

EV
Tag Var
Scope:
AT_END

VARSCOPEFOTER.JSPF

VARSCOPEROW.JSPF

"$ {Lv == gv and lv == bv and lv == nv and lv == EV}" />

$ {lv! = NULL? LV: "null"}

$ {GV! = NULL? GV: "null"}

$ {Lv! = NULL? LV: "null"}

$ {BV! = NULL? BV: "null"}

$ {Nv! = Null? NV: "null"}

$ {EV! = NULL? EV: "null"}

Output

Use properties to provide names for variables. The previous example shows how to use variables, and the names of those variables are given by the tag file. The following example has a tag file (VARATTR.TAG), which creates a variable, which is provided by the JSP page (VARATTR.JSP) with a value (V) of the marker attribute. In order to be able to operate this variable, the tag file uses <% @ variable name-from-attribute = "v" ... alias = "a" ...%> defines an alias (a).

VARATTR.TAG file utilizes the value of the variable using the value of the value of the value of $ {v}, and utilizes the value of the value of $ {a} output. VARATTR.JSP page Use to call the tag file and use the variable named X. Note that the JSP container automatically creates an X variable, set it to a value of A, ie 123.

VARATTR.TAG

<% @ Attribute Name = "V" required = "true"%>

<% @ variable name-from-attribute = "v"

Variable-class = "java.lang.long"

Alias ​​= "a" scope = "at_end"%>

<% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%>

tag: $ {v} = $ {a}

VARATTR.JSP

<% @ taglib prefix = "demo" tagdir = "/ web-inf / tags / demo"%>

jsp: x = $ {x}

Output

Call the JSP clip from the tag file. Currently, you have learned that the tag file supports both attribute types: the properties and dynamic properties of the simple declaration. The third type is called a fragment property. The JSP page of this example provides two simple properties (Attr1 and Attr2) and a segment properties (Template) using the call tag file (Fragmentattr.tag). All three properties are declared in the tag file, but only template = "true" is declared as fragment properties. Fragmentattr.jsp Use the and standard operation, each define two JSP segments that use and from the tag file. In addition, marker files use <% @ variable%> to declare a nested variable (DATA). The value of this variable is set by the mark file using , while two JSP clips output the value of the variable using $ {data}. / P>

Fragmentattr.tag

<% @ Attribute Name = "Template" fragment = "true"%>

<% @ Attribute Name = "Attr1"%>

<% @ Attribute Name = "Attr2"%>

<% @ variable name-given = "data" scope = "NESTED"%>

<% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%>

Fragmentattr.jsp

<% @ taglib prefix = "demo" tagdir = "/ web-inf / tags / demo"%>

template: $ {data}

Body Content: $ {data}

Output

Update and query the database with tag file

When developing complex enterprise applications, many people prefer to use business JavaBean (EJB) technology to manage object sustainability. Others may prefer to use Java Database Connection (JDBC) standard API to manually optimize database access. These solutions may not be suitable for simple websites supported by the database, which only use JSP as fast as possible to develop as quickly as possible. If you only need to query and update a simple database, the tag library that provides database access feature may be the best solution.

JSTL has a series of simple SQL tags, we will use them in the following examples. Don't call JSTL SQL tags directly from your page, and use tag files to express your expression with database access scripts. This separation allows you to easily switch to another database connection solution in order to improve the performance and scalability of the application.

JSTL is not the only tag library that provides database access feature. Oracle Application Development Framework (ADF) provides a JSP library called a business component data tag that contains a series of more advanced JSP tags related to the database. Note that the ADF feature set is broader, making Oracle ADFs suitable for simple and complex applications. Oracle ADF is bound to JDeveloper 10g, the latter has a visual design tool for ADF.

The following six examples create a table, insert three lines, update one line, delete another row, and perform some queries and then delete the table. All of this is done with the general tag file, which does not depend on the structure of the table.

Step 1: Create a table. The JSP page of this example calls the tag file (create.tag) to create a table, the structure of the table is provided with the main content of the custom tag. The attribute of the table specifies the table name. After the table is created, the JSP page outputs a message and notifies the user that the operation has been completed:

Userid INTEGER,

Name varchar2 (60),

Email varchar2 (60)

table created.

The Create.Tag file contains a taggers (INIT.TAGF) that uses JSTL's tag, set a javax.sql.dataSource variable named tags_db_datasource in the application range. This variable is created only when the variable has not yet exists and will be used with the JSTL tag (which is executed in the SQL statement in its body). Create.tag file dynamically creates a CREATE TABLE statement, use $ {Table} to get the table name, and use to insert the structure of the table:

Create Table $ {Table} ()

To run an example, you must configure a data source called DBTAGS. Declare references to this resource in the Descriptor (Web.xml) of the web application:

JDBC / DBTAGS

javax.sql.datasource

container

The init.tagf fragment gets the path to the resource from a initialization parameter. This initialization parameter is also defined in web.xml:

tags_db_datasource

JDBC / DBTAGS

If you want to use another data source, just replace DBTags as your data source name in the web.xml file.

Init.tagf

<% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%>

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

VAR = "tags_db_datasource" scope = "Application" />

Create.tag

<% @ tag body-content = "scripTless"%>

<% @ attribute name = "Table" Required = "true"%>

<% @ include file = "infit.tagf"%>

Create Table $ {Table} ()

CREATE.JSP

<% @ taglib prefix = "DB" tagdir = "/ web-inf / tags / db"%>

Userid INTEGER,

Name varchar2 (60),

Email varchar2 (60)

table created.

Step 2: Insert. After you have a table, you can use the tag each inserted a line, which uses the following syntax to call the tag file (INSERT.TAG):

An example JSP page (INSERT.JSP) uses to insert the three rows into the people table. When each call, the tag file creates and executes a SQL statement using the following syntax:

INSERT INTO TABLE (Column1, Column2, ...) Values ​​(Value1, Value2, ...)

The column name and value are specified as dynamic properties, so can be used for any table, regardless of how the column is named. The tag file uses to iterate the project of the Java.util.map instance (Columnattr), which holds the dynamic properties of . The name of each dynamic attribute represents a column name and is obtained using $ {v_entry.key}. The tag file uses $ {v_entry.value} to get those that must be inserted into the table.

Within , the tag file creates a list of column names (v_columnnames) and another list containing the parameter flag (V_Parammarkers). JSTL's tag and the collaboration of the transmitting SQL parameter value. After executing the loop does not generate any output, the tag file produces the SQL statement by using the following statement.

Insert INTO $ {Table} ($ {v_columnnames}) VALUES ($ {v_parammarkers})

When INSERT.JSP calls the tag file, tag determines the value of its main body

INSERT INTO People (userid, name, email) VALUES (?,?,?)

Then, uses the JDBC API and passes the parameter value sent by , performs the above SQL statement. Note that columns and their respective values ​​may appear in different order because they are retrieved from java.util.map instances. Therefore, we must ensure that the SQL statement does not depend on the order of the column.

INSERT.TAG

<% @ tag body-content = "empty" Dynamic-attributes = "columnattr"%>

<% @ attribute name = "Table" Required = "true"%>

<% @ include file = "infit.tagf"%>

Value = "$ {v_columnnames} $ {v_separator} $ {v_entry.key}" />

Value = "$ {v_parammarkers} $ {v_separator} $ {'' '}" />

INSERT INTO $ {Table} ($ {v_columnnames})

VALUES ($ {v_parammarkers})

INSERT.JSP

<% @ taglib prefix = "DB" tagdir = "/ web-inf / tags / db"%>

Email = "johnsmith@company.com" />

Email = "markjohnson@company.com" />

Email = "BillDavis@company.com" />

rows inserted.

Step 3: Update. Update.jsp Use to change the Email value of a row in the row inserted in Insert.jsp. Update.tag file is similar to insert.tag, but at this time, JSTL's tag is used to execute an UPDATE statement:

Update Table Set Column1 = Value1, Column2 = Value2, ... Where ...

Specify the column and its value as dynamic properties using the following syntax:

WHERE = "..." />

Tag files use dynamic properties to create a list containing a column = structure (v_setlist). Take the column value to by using in the previous example. Then, update.tag generates a SQL statement using the following statement

Update $ {Table} set $ {v_setlist} where $ {where}

When the update.jsp calls the tag file, the tag obtains the value of its main body.

UPDATE PEOPLE SET Email =? Where userid = 2 Unlike tags that can only be used in insertion, tags can be used to update multi-line in a single call.

Update.tag

Resource

Use the following resources to test these examples and learn more about JSP 2.0 tag files and JSTL cores and SQL tags. The download source code Tagfiles_src.zip file contains the examples in this article. To run these examples, you need J2SE, J2EE 1.4 application servers, JSTL 1.1, and database servers. Download OC4J 10goracle Application Server Containers for J2EE 10G fully implements J2EE 1.4 specification, including JSP 2.0. You can test these examples using OC4J 10g (10.0.3). It can be used for all major database servers - of course - includes an Oracle database. Download JSTL 1.1 Download JSTL before deployment example and copy JSTL.jar and Standard.jar to the web-INF / lib directory of the web application. Also, don't forget to configure the DBTAGS data source and make sure you can use the correct database driver. Read JSP 2.0 Specification JSP 2.0 specification has a complete chapter specializes in tag files ("Part 1: JSP chapter. 8 tag file"). The new standard operations launched by JSP 2.0 are explained in another chapter ("Part 1: JSP chapter .5 standard operation")). Read JSTL 1.1 Specification JSTL Specification Description Use of , , , , , , , tag. Related Articles and Download How to: Add Custom JSP Tag Library Oracle JDeveloper 10g JSP Sample Code Tutorial to JDeveloper 10g: Learn about the new feature of JSP 2.0, "Tag File" module

<% @ tag body-content = "empty" Dynamic-attributes = "columnattr"%>

<% @ attribute name = "Table" Required = "true"%>

<% @ Attribute Name = "Where" required = "false"%>

<% @ include file = "infit.tagf"%>

Value = "$ {v_setlist} $ {v_separator} $ {v_entry.key} =?" />

Update $ {table} set $ {v_setlist}

where $ {where}

Update.jsp

<% @ taglib prefix = "DB" tagdir = "/ web-inf / tags / db"%>

Email = "markj@company.com" where = "userid = 2" />

row updated.

Step 4: Delete. The following tag files (delete.tag) can be used to delete one or more rows, which is located in the delete.jsp page.

Delete.tag

<% @ tag body-content = "empty"%>

<% @ attribute name = "Table" Required = "true"%>

<% @ Attribute Name = "Where" required = "false"%>

<% @ include file = "infit.tagf"%>

Delete from $ {Table}

where $ {where}

Delete.jsp

<% @ taglib prefix = "DB" tagdir = "/ web-inf / tags / db"%>

row deleded.

Step 5: Select. The next tag file (select.tag) uses JSTL's tag to perform the SELECT statement. The grammar of is included below:

WHERE = "..." GrouPy = "..." "haVing =" ... "Orderby =" ... ">

Process The Result of The Query, One Row At A Time

tag Create and perform the following SQL statement, the clause is specified as the property of the tag.

Select Column from Table Where ...

Group by ... havenne ... Order by ...

When appears in the JSP page, use Select.Tag's operation to call its main body for each line. The JSP page gets line data from a java.util.map created by tag files with JSTL. For example, the value of the current row can be obtained using a JSP structure similar to $ {row.userid}, $ {row.Name}, and $ {row.email}, at which point the tag file assumed by the JSP page is specified should hold data. The variable is named ROW:

$ {row.Userid} ... $ {row.email} ... $ {row.email}

The SELECT.JSP page calls the tag file twice. First, it uses to iterate the row of the relational table, generate an HTML table, then the JSP page uses to calculate the number of lines of the PEOPLE table.

SELECT.TAG

<% @ tag body-content = "scripTless"%>

<% @ attribute name = "var" required = "true"%>

<% @ variable name-from-attribute = "var"

Alias ​​= "v_row" scope = "nested"%>

<% @ attribute name = "Table" Required = "true"%>

<% @ attribute name = "columns" required = "false"%>

<% @ Attribute Name = "Where" required = "false"%>

<% @ Attribute Name = "Groupby" Required = "false"%>

<% @ attribute name = "haVing" required = "false"%>

<% @ Attribute Name = "Orderby" Required = "false"%>

<% @ include file = "infit.tagf"%>

SELECT $ {Empty Column? "*": Columns} from $ {table}

where $ {where}

Group by $ {groupby} Order by $ {orderby}

SELECT.JSP

<% @ taglib prefix = "DB" tagdir = "/ web-inf / tags / db"%>

<% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%>

User ID

Name

Email

$ {row.userid}

$ {row.name}

$ {row.email}

$ {c.n} people.

Output

Step 6: Delete the table. If you want to execute the JSP example again, you must use the Drop.jsp page to delete the table with the drop.tag file. Drop.tag <% @ tag body-content = "scriptless"%>

<% @ attribute name = "Table" Required = "true"%>

<% @ include file = "infit.tagf"%>

DROP TABLE $ {TABLE}

Drop.jsp <% @ taglib prefix = "DB" tagdir = "/ web-inf / tags / db"%>

Table Dropped.

Conclusion The markup file is an easy-to-use solution required for web developers to create a custom tag library. The transition from the Java tag handler to the JSP tag file is similar to the transition from servlet to JSP a few years ago. When developing a Java web application, most dynamic content is now generated by JSP, but servlets are still used as a underlying technology of JSP, and in some cases, servlet is more applicable than JSP. Similarly, the Java tag handler still needs a subtenction technology as a tag file to create a complex tag library, such as JSTL. However, most tag libraries will be created using tag files because the tag file simplifies the development process.

Andrei Cioroianu

DevTools@devsphere.com) is the founder of DevSphere (www.devsphere.com), which is a supplier of Java Frame, XML Consulting and Web Development Services. Andrei Cioroianu has written a lot of Java articles, published by ONJava, JavaWorld and Java Developer's Japan. He also with others with two books (both published by Wrox Press). (Published by Wrox Press) in Java XML Programmer's Reference and Professional Java XML.

转载请注明原文地址:https://www.9cbs.com/read-114235.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.038, SQL: 9