1.Jsp Tag Library (also known as a custom library) can be seen as a set of ways to generate an XML script, which is supported via JavaBeans. Conceptually, the label library is very simple and can be reused code construct. It can greatly simplify the code you enter a variety of entry boxes in the page (like this code:
2. Tag Libraries provides a simple way to establish a reusable code block. But the COM and J2EE are different, you don't need to master any additional skills to create a label library: If you will write a JSP page, you will establish a label library. Finally, the label library also improves the maintenance of the web application. This improvement in maintenance is: easily implements XML-based customizable interfaces on the JSP page.
3. A simple label consists of the elements below:
(1) JavaBeans: In order to get Java and the object-oriented benefits, reusable code should be placed in a separate code container. These JavaBeans are not part of the label library. However, it is the basic code block for your code library to perform related tasks.
(2) Tag processing: This is the true core of the label library. A tag processor will reference any of its resources (your JavaBeans) and all information to access your JSP page (pageContext object). The JSP page also transmits all the tag properties that have been set and the contents in the label on the JSP page to the label processor. After the label processor is processed, it will send it back to your JSP page for processing.
(3) Description of the label library (TLD file): This is a simple XML file that records the properties, information, and location of the label processor. The JSP container learns from this file and how to call a label library.
⑷ Web.xml file: This is an initialization file for your website. In this file, you define the custom label used in the website, and which TLD file is used to describe each custom label.
⑸ Distribution file (a WAR or JAR file): If you want to reuse custom labels, you need a way to transfer it from a project to another. Packing the label library as a JAR file is a simple and effective way.
⑹ Be a label library declaration in your JSP file: very simple, if you want to use the tag, you can use it if you can declare on the page, then you can use it anywhere in the JSP page.
Here is an example
1. Write the label handler first, atmtag.java
// Use the label handler to define the work of the customer label
Import javax.servlet.jsp. *;
// Import the driver of the label
Import javax.servlet.jsp.tagext. *;
Import java.io. *;
Import java.sql. *;
Import java.math. *;
Import java.util. *;
/ / Implement Tagsupport or BodyTagsupport interface
Public Class Atmtag Extends Tagsupport
{
Connection connect = NULL;
Statement State = NULL;
ResultSet Result = NULL;
Public atmtag () THROWS CLASSNOTFOUNDEXCEPTION {
Class.Forname ("Sun.jdbc.odbc.jdbcodbcdriver");
}
// Abstract class TAG method, initialization of label processing programs
Public int dostarttag () THROWS JSptagexception
{
/ / Guide the contents of the JSP engine to process tags
RETURN EVAL_BODY_INCLUDE;
}
// Execute the label task
Public int doendtag () THROWS JSptagexception
{
Try
{
// Write the output to the JSP page
Jspwriter out = pageContext.getut ();
Connect = DriverManager.getConnection ("JDBC: ODBC: TEST", "SA", "");
String strquery = "SELECT CCOUNTER_ID, CADDRESS, McShbalance from counter where mcashbalance <= 10000;"
System.out.println ("Query:" StrQuery);
State = connect.createstatement ();
ResultSet Result = State.executeQuery (STRQUERY);
String Sid = NULL;
String saddress = null;
String sbalance = NULL;
Vector v = new vector ();
While (result.next ())
{
SID = Result.getstring (1);
Saddress = Result.getstring (2);
Sbalance = Result.getstring (3);
v.add (sID);
v.add (saddress);
v.add (sbalance);
v.add (";");
}
For (int I = 0; i { String str = (string) v.elementat (i); PageContext.getut (). Write (str); } } catch (Exception IoE) { System.out.println ("ERROR IN GETTING RESULTS" IOE.TOSTRING ()); } / (I point out the value of the rest of the JSP page Return Eval_page; } } 2. Write the code of the TLD file: // XML verification document Xml Version = "1.0" encoding = "ISO-8859-1"?>
" http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd "> // Tag library version // Tag library reliance JSP version // Tag library name / / Define the name of the label / (Indicate the label processing program class / / Definition of this label / / About this label and additional information tag> taglib> 3. Error page errorpage.jsp code:
<% @ Page isrrorpage = "true"%>
<% = exception.getMessage ()%>
body>
html>
4. Write a JSP file: atm.jsp
<% @ Page Import = "atmtag"%>
<% @ Taglib URI = "Atmjsp.TLD" prefix = "atmjsp"%>
<% @ Page ErrorPage = "ErrorPage.jsp"%>
TD> | table> html>
转载请注明原文地址:https://www.9cbs.com/read-32195.html New Post(0)
|