Struts custom tag Learning (3)

Source: Internet
Author: User

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:
  1. Package tags;
  2. Import java. Io. ioexception;
  3. Import java. util. arraylist;
  4. Import javax. servlet. jsp. jspexception;
  5. Import javax. servlet. jsp. jspwriter;
  6. Import javax. servlet. jsp. tagext. tagsupport;
  7. Import Po. student;
  8. Import Dao. studentdao;
  9. // Customize the label specification function: displays the details of all students
  10. // 1. Inheritance: javax. servlet. jsp. tagext. tagsupport
  11. // 2. Rewrite method doendtag and dostarttag (automatically called by the System)
  12. Public class displayalltags extends tagsupport {
  13. Private Static final long serialversionuid = 1l;
  14. Public displayalltags (){
  15. System. Out. println ("displayaddtags constructor ");
  16. }
  17. Public int dostarttag () throws jspexception {
  18. System. Out. println ("dostarttag function ");
  19. Return skip_body; // the empty body label that does not implement the label.
  20. }
  21. Public int doendtag () throws jspexception {
  22. System. Out. println ("doendtag function ");
  23. // Query the database
  24. Studentdao = new studentdao ();
  25. Arraylist = (arraylist) studentdao. querystus ();
  26. // Get the relevant built-in object through the attribute value pagecontext of the parent class tagsupport
  27. Jspwriter out = pagecontext. getout ();
  28. // Print through the traversal loop.
  29. Try {
  30. Out. println ("<Table> ");
  31. Out. println ("<tr> ");
  32. Out. println ("<TD> Student ID </TD> ");
  33. Out. println ("<TD> name </TD> ");
  34. Out. println ("<TD> gender </TD> ");
  35. Out. println ("<TD> Date of Birth </TD> ");
  36. Out. println ("<TD> Home address </TD> ");
  37. Out. println ("</tr> ");
  38. For (INT I = 0; I <arraylist. Size (); I ++ ){
  39. Student = (student) arraylist. Get (I );
  40. Out. println ("<tr> ");
  41. Out. println ("<TD>" + student. getstuid () + "</TD> ");
  42. Out. println ("<TD>" + student. getstuname () + "</TD> ");
  43. Out. println ("<TD>" + student. getstusex () + "</TD> ");
  44. Out. println ("<TD>" + student. getstubir () + "</TD> ");
  45. Out. println ("<TD>" + student. getstuadd () + "</TD> ");
  46. Out. println ("</tr> ");
  47. }
  48. Out. println ("<Table> ");
  49. } Catch (ioexception e ){
  50. E. printstacktrace ();
  51. }
  52. // ================ Pagecontext can also get more built-in objects.
  53. // Pagecontext. getrequest ();
  54. // Pagecontext. getresponse ();
  55. // Pagecontext. getsession ();
  56. // Pagecontext. getservletcontext ();
  57. Return eval_page ;//
  58. }
  59. }

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:
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype taglib public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.1 //" http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd ">
  3. <Taglib> <! -- Root of the custom library tag -->
  4. <Tlibversion> 1.2 </tlibversion> <! -- Version -->
  5. <Jspversion> 1.1 </jspversion> <! -- JSP version -->
  6. <Shortname> displaytags </shortname> <! -- Tag name -->
  7. <URI> displaytags </uri> <! -- Names recognized when tags are imported outside the world are very important. -->
  8. <Tag>
  9. <Name> displayall </Name> <! -- Tag name -->
  10. <Tagclass> tags. displayalltags </tagclass> <! -- Corresponding class -->
  11. </Tag>
  12. <! -- Tag with attributes -->
  13. <Tag>
  14. <Name> displaybysex </Name>
  15. <Tagclass> tags. displaybysextags </tagclass>
  16. <Attribute> <! -- Corresponds to the tag attributes. -->
  17. <Name> sex </Name>
  18. <Required> true </required> <! -- Is a property that must be written, that is, the tag without the write property cannot be used normally -->
  19. </Attribute>
  20. </Tag>
  21. <! -- 1. Two attributes 2. attributes are not required 3. attributes can be assigned with variables -->
  22. <Tag>
  23. <Name> displaybysa </Name>
  24. <Tagclass> tags. displaybysatags </tagclass>
  25. <Attribute>
  26. <Name> sex </Name>
  27. <Required> false </required>
  28. </Attribute>
  29. <Attribute>
  30. <Name> Add </Name>
  31. <Required> true </required>
  32. <Rtexprvalue> true </rtexprvalue> <! -- True: expression can be used to represent -->
  33. </Attribute>
  34. </Tag>
  35. </Taglib>

Step 3: Compile the JSP page with the following code:

Code:
  1. <% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
  2. <% @ Taglib uri = "displaytags" prefix = "Stu" %>
  3. <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
  4. <HTML>
  5. <Head>
  6. <Title> test custom tags </title>
  7. </Head>
  8. <Body>
  9. Print the student list <br>
  10. <STU: displayall> </STU: displayall>
  11. <Br> Print the student list (one Property Test) <br>
  12. <STU: displaybysex sex = "male"> </STU: displaybysex>
  13. <Br> Print the student list (two attributes are tested) <br>
  14. <STU: displaybysa add = "Shanghai"> </STU: displaybysa>
  15. </Body>
  16. </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 ============

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.