Sort out a few things learned in the morning, an example of custom tags. I think the biggest reason for sticking to taking notes is that I forget to watch videos, check materials, and Baidu... In addition, if you organize your own data, your memory will last longer.
Example: Query Information of all students (student ID, name, gender, date of birth, home address)
Create a project XXX. Because it is a custom tag, you do not need to introduce the struts package.
Step 1: Under the tags package, to create a Java class, you must inherit javax. servlet. jsp. tagext. tagsupport and write the following code:
Code:
- Package tags;
- Import java. Io. ioexception;
- Import java. util. arraylist;
- Import javax. servlet. jsp. jspexception;
- Import javax. servlet. jsp. jspwriter;
- Import javax. servlet. jsp. tagext. tagsupport;
- Import Po. student;
- Import Dao. studentdao;
- // Customize the label specification function: displays the details of all students
- // 1. Inheritance: javax. servlet. jsp. tagext. tagsupport
- // 2. Rewrite method doendtag and dostarttag (automatically called by the System)
- Public class displayalltags extends tagsupport {
- Private Static final long serialversionuid = 1l;
- Public displayalltags (){
- System. Out. println ("displayaddtags constructor ");
- }
- Public int dostarttag () throws jspexception {
- System. Out. println ("dostarttag function ");
- Return skip_body; // the empty body label that does not implement the label.
- }
- Public int doendtag () throws jspexception {
- System. Out. println ("doendtag function ");
- // Query the database
- Studentdao = new studentdao ();
- Arraylist = (arraylist) studentdao. querystus ();
- // Get the relevant built-in object through the attribute value pagecontext of the parent class tagsupport
- Jspwriter out = pagecontext. getout ();
- // Print through the traversal loop.
- Try {
- Out. println ("<Table> ");
- Out. println ("<tr> ");
- Out. println ("<TD> Student ID </TD> ");
- Out. println ("<TD> name </TD> ");
- Out. println ("<TD> gender </TD> ");
- Out. println ("<TD> Date of Birth </TD> ");
- Out. println ("<TD> Home address </TD> ");
- Out. println ("</tr> ");
- For (INT I = 0; I <arraylist. Size (); I ++ ){
- Student = (student) arraylist. Get (I );
- Out. println ("<tr> ");
- Out. println ("<TD>" + student. getstuid () + "</TD> ");
- Out. println ("<TD>" + student. getstuname () + "</TD> ");
- Out. println ("<TD>" + student. getstusex () + "</TD> ");
- Out. println ("<TD>" + student. getstubir () + "</TD> ");
- Out. println ("<TD>" + student. getstuadd () + "</TD> ");
- Out. println ("</tr> ");
- }
- Out. println ("<Table> ");
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
- // ================ Pagecontext can also get more built-in objects.
- // Pagecontext. getrequest ();
- // Pagecontext. getresponse ();
- // Pagecontext. getsession ();
- // Pagecontext. getservletcontext ();
- Return eval_page ;//
- }
- }
Step 2: Create a. TLD file under webroot → web-INF (right-click the new file and click OK) and write the code as follows:
Code:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! Doctype taglib public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.1 //" http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd ">
- <Taglib> <! -- Root of the custom library tag -->
- <Tlibversion> 1.2 </tlibversion> <! -- Version -->
- <Jspversion> 1.1 </jspversion> <! -- JSP version -->
- <Shortname> displaytags </shortname> <! -- Tag name -->
- <URI> displaytags </uri> <! -- Names recognized when tags are imported outside the world are very important. -->
- <Tag>
- <Name> displayall </Name> <! -- Tag name -->
- <Tagclass> tags. displayalltags </tagclass> <! -- Corresponding class -->
- </Tag>
- <! -- Tag with attributes -->
- <Tag>
- <Name> displaybysex </Name>
- <Tagclass> tags. displaybysextags </tagclass>
- <Attribute> <! -- Corresponds to the tag attributes. -->
- <Name> sex </Name>
- <Required> true </required> <! -- Is a property that must be written, that is, the tag without the write property cannot be used normally -->
- </Attribute>
- </Tag>
- <! -- 1. Two attributes 2. attributes are not required 3. attributes can be assigned with variables -->
- <Tag>
- <Name> displaybysa </Name>
- <Tagclass> tags. displaybysatags </tagclass>
- <Attribute>
- <Name> sex </Name>
- <Required> false </required>
- </Attribute>
- <Attribute>
- <Name> Add </Name>
- <Required> true </required>
- <Rtexprvalue> true </rtexprvalue> <! -- True: expression can be used to represent -->
- </Attribute>
- </Tag>
- </Taglib>
Step 3: Compile the JSP page with the following code:
Code:
- <% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
- <% @ Taglib uri = "displaytags" prefix = "Stu" %>
- <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
- <HTML>
- <Head>
- <Title> test custom tags </title>
- </Head>
- <Body>
- Print the student list <br>
- <STU: displayall> </STU: displayall>
- <Br> Print the student list (one Property Test) <br>
- <STU: displaybysex sex = "male"> </STU: displaybysex>
- <Br> Print the student list (two attributes are tested) <br>
- <STU: displaybysa add = "Shanghai"> </STU: displaybysa>
- </Body>
- </Html>
The general process is like this. The Dao. Student Class (responsible for interacting with the database) and Po. Student (JavaBean) are also used. displaybysextags. Java and dispalybysatags. Java are similar to displayalltags. java.
Source code: http://u.115.com/file/f26bbf3eea
============================== Video learning notes ============