Customize nested labels to implement if else

Source: Internet
Author: User

Three tag classes: poweriftag is external and used to judge. powernotag is else when poweryestag indicates if (true).]

 

 

Poweriftag. Java

  1. Package AA;
  2. Import java. Io. ioexception;
  3. Import javax. servlet. jsp. jspexception;
  4. Import javax. servlet. jsp. jsptagexception;
  5. Import javax. servlet. jsp. jspwriter;
  6. Import javax. servlet. jsp. tagext. tagsupport;
  7. Public class poweriftag extends tagsupport {
  8. Private string list;
  9. Private string name;
  10. Private Boolean is;
  11. Public Boolean ISIS (){
  12. Return is;
  13. }
  14. Public void setis (Boolean is ){
  15. This. Is = is;
  16. }
  17. Public String getlist (){
  18. Return list;
  19. }
  20. Public void setlist (string list ){
  21. This. List = List;
  22. }
  23. Public String getname (){
  24. Return name;
  25. }
  26. Public void setname (string name ){
  27. This. Name = Name;
  28. }
  29. Public int dostarttag () throws jspexception {
  30. If (name! = NULL &&! Name. Equals ("") & list! = NULL &&! List. Equals (""))
  31. {
  32. // The following key code can be changed as needed.
  33. Object o = This. pagecontext. getsession (). getattribute (name );
  34. If (O instanceof string)
  35. {
  36. String astr [] = List. Split (",");
  37. String ostr = (string) O;
  38. For (INT I = 0; I <astr. length; I ++)
  39. {
  40. If (! Astr [I]. Equals ("") & ostr. indexof (astr [I])>-1)
  41. {
  42. This. Is = true;
  43. Break;
  44. }
  45. }
  46. }
  47. } Else
  48. {
  49. This. Is = false;
  50. }
  51. Return this. eval_page;
  52. }
  53. }

 

Poweryestag. Java

  1. Package AA;
  2. Import java. Io. ioexception;
  3. Import javax. servlet. jsp. jspexception;
  4. Import javax. servlet. jsp. jsptagexception;
  5. Import javax. servlet. jsp. jspwriter;
  6. Import javax. servlet. jsp. tagext. Tag;
  7. Import javax. servlet. jsp. tagext. tagsupport;
  8. Public class poweryestag extends tagsupport {
  9. Public int dostarttag () throws jspexception {
  10. Tag T = findancestorwithclass (this, poweriftag. Class );
  11. If (T! = NULL)
  12. {
  13. Poweriftag parent = (poweriftag) T;
  14. If (parent. ISIS ())
  15. {
  16. Return eval_page;
  17. }
  18. }
  19. Return skip_body;
  20. }
  21. }

Powernotag. Java

  1. Package AA;
  2. Import java. Io. ioexception;
  3. Import javax. servlet. jsp. jspexception;
  4. Import javax. servlet. jsp. jsptagexception;
  5. Import javax. servlet. jsp. jspwriter;
  6. Import javax. servlet. jsp. tagext. Tag;
  7. Import javax. servlet. jsp. tagext. tagsupport;
  8. Public class powernotag extends tagsupport {
  9. Public int dostarttag () throws jspexception {
  10. Tag T = findancestorwithclass (this, poweriftag. Class );
  11. If (T! = NULL)
  12. {
  13. Poweriftag parent = (poweriftag) T;
  14. If (! Parent. ISIS ())
  15. {
  16. Return eval_page;
  17. }
  18. }
  19. Return skip_body;
  20. }
  21. }

Powerif. TLD

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Taglib>
  3. <Tlib-version> 1.0 </tlib-version>
  4. <JSP-version> 1.2 </JSP-version>
  5. <Tag>
  6. <Name> PIF </Name>
  7. <Tag-class> AA. poweriftag </Tag-class>
  8. <Attribute>
  9. <Name> List </Name>
  10. <Required> true </required>
  11. <Rtexprvalue> true </rtexprvalue>
  12. </Attribute>
  13. <Attribute>
  14. <Name> name </Name>
  15. <Required> true </required>
  16. <Rtexprvalue> true </rtexprvalue>
  17. </Attribute>
  18. </Tag>
  19. <Tag>
  20. <Name> pyes </Name>
  21. <Tag-class> AA. poweryestag </Tag-class>
  22. </Tag>
  23. <Tag>
  24. <Name> PNO </Name>
  25. <Tag-class> AA. powernotag </Tag-class>
  26. </Tag>
  27. </Taglib>

Example:

<TD>
<Powerif: PIF name = "functstr" list = "01010102">
<Powerif: pyes>
<A href = "/company. Do? Method = preupdate & companycode =$ {company. companycode} "onclick =" alink (this); ">$ {company. companycode} </a>
</Powerif: pyes>
<Powerif: PNO>
$ {Company. companycode}
</Powerif: PNO>
</Powerif: PIF>
</TD>

 

Why do you need to customize tags? Some labels written by others may not be suitable for your own needs. As I wrote about permission judgment, pay attention to flexibility when using tags.

You can modify the Business Code in the poweriftag according to your needs. Now I am judging the permissions. You can also change the permissions according to your project requirements.

 

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.