您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> jsp编程 >> 正文

JSP自定义标签实现数据字典

作者:佚名    责任编辑:admin    更新时间:2022-06-22

  1、关于JSP标签的好处就不再罗嗦

数据字典就是使用的下拉框,只要定义使用那个字典就会将这个字典可用的内容显示出来

显示字典时只要定义那个字典和属性值就可以显示出字典的显示值

 

       2、首先在web.xml中定义自定义标签加载的引用,两个属性分别是引用的URI和加载路径

  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    
  6.     <welcome-file-list>    
  7.         <welcome-file>index.jsp</welcome-file>    
  8.     </welcome-file-list>    
  9.     <jsp-config>    
  10.         <taglib>    
  11.             <taglib-uri>/tld/web-html</taglib-uri>    
  12.             <taglib-location>    
  13.                 /WEB-INF/tlds/web-html.tld    
  14.             </taglib-location>    
  15.         </taglib>    
  16.     </jsp-config>    
  17. </web-app>   

    3、在web-html.tld中定义自己的标签,数据字典应用的话我们需要一个标签库,三个标签。分别是,select标签,options标签,和现实数据字典的标签,每个标签都对应不同的实现类

  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"      
  3.     "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">    
  4. <taglib>    
  5.     <tlib-version>1.0</tlib-version><!-- 标签库版本 -->    
  6.     <jsp-version>1.2</jsp-version>  <!-- 标签库要求的JSP规范版本 -->    
  7.   &


nbsp; <short-name>html</short-name>   <!-- JSP页面编写工具可以用来创建助记名的可选名字 -->    

  •     <tag>    
  •         <name>select</name>    
  •         <tag-class>com.SelectTag</tag-class>    
  •         <body-content>JSP</body-content>    
  •         <attribute>    
  •             <name>name</name>    
  •             <rtexprvalue>true</rtexprvalue>    
  •         </attribute>    
  •         <attribute>    
  •             <name>style</name>    
  •             <rtexprvalue>true</rtexprvalue>    
  •         </attribute>    
  •     </tag>    
  •     <tag>    
  •         <name>options</name>    
  •         <tag-class>com.OptionsTag</tag-class>    
  •         <body-content>JSP</body-content>    
  •         <attribute>    
  •             <name>collection</name>    
  •             <rtexprvalue>true</rtexprvalue>    
  •         </attribute>    
  •     </tag>    
  •     <tag>    
  •         <name>selectDisplay</name>    
  •         <tag-class>com.SelectDisplay</tag-class>    
  •         <


;body-content>JSP</body-content>    

  •         <attribute>    
  •             <name>collection</name>    
  •             <rtexprvalue>true</rtexprvalue>    
  •         </attribute>    
  •         <attribute>    
  •             <name>name</name>    
  •             <rtexprvalue>true</rtexprvalue>    
  •         </attribute>    
  •         <attribute>    
  •             <name>value</name>    
  •             <rtexprvalue>true</rtexprvalue>    
  •         </attribute>    
  •     </tag>    
  • </taglib>   

     4、实现类

实现类的作用就是在后台拼接所需HTML标签内容,然后由JSP进行输出

实现类最主要的两个方法,一个遇到这个标签开始时输出,一个是结束时输出

如果