自定义标签(Select)

 Step1:编写selectTag.tld文件

 

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  5.     version="2.0">
  6.     <description>select tag</description>
  7.     <display-name>select tag</display-name>
  8.     <tlib-version>1.1</tlib-version>
  9.     <short-name>select</short-name>
  10.     <tag>
  11.         <description>This option tag</description>
  12.         <name>option</name><!-- tag's name -->
  13.         <tag-class>org.eagle.testFunction.SelectTag</tag-class><!-- This is handle Class -->
  14.         <body-content>empty</body-content><!-- whether or not have tag body,empty is havn't,JSP is have-->
  15.         <attribute>
  16.             <description>
  17.                 The test condition that tells whether or not the body
  18.                 content should be processed
  19.             </description>
  20.             <name>style</name><!-- attribute's name -->
  21.             <required>false</required><!-- the attribute is required -->
  22.             <rtexprvalue>false</rtexprvalue><!-- false is plain text    -->
  23.         </attribute>
  24.         <attribute>
  25.             <description>
  26.                 The test condition that tells whether or not the body
  27.                 content should be processed
  28.             </description>
  29.             <name>optionValues</name><!-- attribute's name -->
  30.             <required>false</required><!-- the attribute is required -->
  31.             <rtexprvalue>true</rtexprvalue><!-- false is plain text,true is not -->
  32.         </attribute>
  33.         <attribute>
  34.             <description>
  35.                 The test condition that tells whether or not the body
  36.                 content should be processed
  37.             </description>
  38.             <name>optionTexts</name><!-- attribute's name -->
  39.             <required>false</required><!-- the attribute is required -->
  40.             <rtexprvalue>true</rtexprvalue><!-- false is plain text,true is not -->
  41.         </attribute>
  42.         <attribute>
  43.             <description>
  44.                 The test condition that tells whether or not the body
  45.                 content should be processed
  46.             </description>
  47.             <name>start</name><!-- attribute's name -->
  48.             <required>false</required><!-- the attribute is required -->
  49.             <rtexprvalue>false</rtexprvalue><!-- false is plain text,true is not -->
  50.         </attribute>
  51.         <attribute>
  52.             <description>
  53.                 The test condition that tells whether or not the body
  54.                 content should be processed
  55.             </description>
  56.             <name>end</name><!-- attribute's name -->
  57.             <required>false</required><!-- the attribute is required -->
  58.             <rtexprvalue>false</rtexprvalue><!-- false is plain text,true is not -->
  59.         </attribute>
  60.         <attribute>
  61.             <description>
  62.                 The test condition that tells whether or not the body
  63.                 content should be processed
  64.             </description>
  65.             <name>name</name><!-- attribute's name -->
  66.             <required>false</required><!-- the attribute is required -->
  67.             <rtexprvalue>false</rtexprvalue><!-- false is plain text,true is not -->
  68.         </attribute>
  69.         <attribute>
  70.             <description>
  71.                 The test condition that tells whether or not the body
  72.                 content should be processed
  73.             </description>
  74.             <name>defaultValue</name><!-- attribute's name -->
  75.             <required>false</required><!-- the attribute is required -->
  76.             <rtexprvalue>false</rtexprvalue><!-- false is plain text,true is not -->
  77.         </attribute>
  78.         <attribute>
  79.             <description>
  80.                 The test condition that tells whether or not the body
  81.                 content should be processed
  82.             </description>
  83.             <name>id</name><!-- attribute's name -->
  84.             <required>false</required><!-- the attribute is required -->
  85.             <rtexprvalue>false</rtexprvalue><!-- false is plain text,true is not -->
  86.         </attribute>
  87.         <attribute>
  88.             <name>onchange</name>
  89.             <required>false</required>
  90.             <rtexprvalue>true</rtexprvalue>
  91.         </attribute>
  92.         <attribute>
  93.             <name>onclick</name>
  94.             <required>false</required>
  95.             <rtexprvalue>true</rtexprvalue>
  96.         </attribute>
  97.         <attribute>
  98.             <name>ondblclick</name>
  99.             <required>false</required>
  100.             <rtexprvalue>true</rtexprvalue>
  101.         </attribute>
  102.     </tag>
  103. </taglib>

 Step2:编写处理类org.eagle.testFunction.SelectTag.java

 

  1. /**
  2.  * 
  3.  */
  4. package org.eagle.testFunction;
  5. import java.io.IOException;
  6. import java.util.List;
  7. import javax.servlet.jsp.JspException;
  8. import javax.servlet.jsp.PageContext;
  9. import javax.servlet.jsp.tagext.TagSupport;
  10. /**
  11.  * @author zhenhuaZhu
  12.  * 
  13.  */
  14. @SuppressWarnings("serial")
  15. public class SelectTag extends TagSupport {
  16.     private PageContext pageContext;
  17.     private String defaultValue = "0";
  18.     private List optionValues;
  19.     private List optionTexts;
  20.     private String style;
  21.     private int start;
  22.     private int end;
  23.     private String name;
  24.     private String id;
  25.     private String scope;
  26.     private String onclick;
  27.     private String onchange;
  28.     private String ondblclick;
  29.     private StringBuffer sb;
  30.     @Override
  31.     public int doStartTag() throws JspException {
  32.         sb = new StringBuffer("<select ");
  33.         if (this.name != null) {
  34.             sb.append("name='" + this.name + "' ");
  35.         }
  36.         if (this.id != null) {
  37.             sb.append("id='" + this.id + "' ");
  38.         }
  39.         if (this.onchange != null) {
  40.             sb.append("οnchange='" + this.onchange + "' ");
  41.         }
  42.         if (this.onclick != null) {
  43.             sb.append("οnclick='" + this.onclick + "' ");
  44.         }
  45.         if (this.ondblclick != null) {
  46.             sb.append("οndblclick='" + this.ondblclick + "' ");
  47.         }
  48.         sb.append(">");
  49.         fillDefaultValueObj();
  50.         constractOption();
  51.         sb.append("</select>");
  52.         try {
  53.             this.pageContext.getOut().write(sb.toString());
  54.             sb = null;
  55.         } catch (IOException e) {
  56.             e.printStackTrace();
  57.         }
  58.         return TagSupport.EVAL_PAGE;
  59.     }
  60.     private void fillDefaultValueObj() {
  61.         if (null == this.scope) {
  62.             this.scope = "request";
  63.         }
  64.         if (null == this.name) {
  65.             return;
  66.         }
  67.         Object defaultValueObj = null;
  68.         if ("request".equalsIgnoreCase(this.scope)) {
  69.             defaultValueObj = this.pageContext.getRequest().getAttribute(
  70.                     this.name);
  71.         } else if ("session".equalsIgnoreCase(this.scope)) {
  72.             defaultValueObj = this.pageContext.getSession().getAttribute(
  73.                     this.name);
  74.         } else if ("application".equalsIgnoreCase(this.scope)) {
  75.             defaultValueObj = this.pageContext.getServletContext()
  76.                     .getAttribute(this.name);
  77.         }
  78.         if (null != defaultValueObj) {
  79.             this.defaultValue = (String) defaultValueObj;
  80.         }
  81.     }
  82.     private void constractOption() {
  83.         if (this.start >= 0 && this.end > 0 && this.end >= this.start) {
  84.             for (int i = this.start; i <= this.end; ++i) {
  85.                 if (!this.defaultValue.equalsIgnoreCase(String.valueOf(i))) {
  86.                     sb.append("<option value='" + i + "'" + ">" + i
  87.                             + "</option>");
  88.                 } else {
  89.                     sb.append("<option value='" + i + "'" + "selected>" + i
  90.                             + "</option>");
  91.                 }
  92.             }
  93.             return;
  94.         }
  95.         if (null != this.optionValues) {
  96.             if (null != this.optionTexts) {
  97.                 for (int i = 0, len = this.optionValues.size(); i < len; ++i) {
  98.                     if (!this.defaultValue
  99.                             .equalsIgnoreCase((String) this.optionValues.get(i))) {
  100.                         sb.append("<option value='" + this.optionValues.get(i)
  101.                                 + "'" + ">" + this.optionTexts.get(i)
  102.                                 + "</option>");
  103.                     } else {
  104.                         sb.append("<option value='" + this.optionValues.get(i)
  105.                                 + "'" + "selected>" + this.optionTexts.get(i)
  106.                                 + "</option>");
  107.                     }
  108.                 }
  109.             } else {
  110.                 for (int i = 0, len = this.optionValues.size(); i < len; ++i) {
  111.                     if (!this.defaultValue
  112.                             .equalsIgnoreCase((String) this.optionValues.get(i))) {
  113.                         sb.append("<option value='" + this.optionValues.get(i)
  114.                                 + "'" + ">" + i + "</option>");
  115.                     } else {
  116.                         sb.append("<option value='" + this.optionValues.get(i)
  117.                                 + "'" + "selected>" + i + "</option>");
  118.                     }
  119.                 }
  120.             }
  121.         } else {
  122.             if (0 == this.end) {
  123.                 sb.append("</select>");
  124.             } else if (0 != end) {
  125.                 if (null != this.optionTexts) {
  126.                     for (int i = 0, len = this.optionTexts.size(); i < len; ++i) {
  127.                         if (!this.defaultValue
  128.                                 .equalsIgnoreCase((String) this.optionValues
  129.                                         .get(i))) {
  130.                             sb.append("<option value='" + i + "'" + ">"
  131.                                     + this.optionTexts.get(i) + "</option>");
  132.                         } else {
  133.                             sb.append("<option value='" + i + "'" + "selected>"
  134.                                     + this.optionTexts.get(i) + "</option>");
  135.                         }
  136.                     }
  137.                 } else {
  138.                     for (int i = 0; i < this.end; ++i) {
  139.                         if (!this.defaultValue
  140.                                 .equalsIgnoreCase((String) this.optionValues
  141.                                         .get(i))) {
  142.                             sb.append("<option value='" + i + "'" + ">" + i
  143.                                     + "</option>");
  144.                         } else {
  145.                             sb.append("<option value='" + i + "'" + "selected>"
  146.                                     + i + "</option>");
  147.                         }
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.     }
  153.     @Override
  154.     public void setPageContext(PageContext arg0) {
  155.         this.pageContext = arg0;
  156.     }
  157.     public String getDefaultValue() {
  158.         return defaultValue;
  159.     }
  160.     public void setDefaultValue(String defaultValue) {
  161.         this.defaultValue = defaultValue;
  162.     }
  163.     public void setDefaultValue(int defaultValue) {
  164.         this.defaultValue = String.valueOf(defaultValue);
  165.     }
  166.     public int getEnd() {
  167.         return end;
  168.     }
  169.     public void setEnd(int end) {
  170.         this.end = end;
  171.     }
  172.     public String getId() {
  173.         return id;
  174.     }
  175.     public void setId(String id) {
  176.         this.id = id;
  177.     }
  178.     public String getName() {
  179.         return name;
  180.     }
  181.     public void setName(String name) {
  182.         this.name = name;
  183.     }
  184.     public List getOptionTexts() {
  185.         return optionTexts;
  186.     }
  187.     public void setOptionTexts(List optionTexts) {
  188.         this.optionTexts = optionTexts;
  189.     }
  190.     public List getOptionValues() {
  191.         return optionValues;
  192.     }
  193.     public void setOptionValues(List optionValues) {
  194.         this.optionValues = optionValues;
  195.     }
  196.     public String getScope() {
  197.         return scope;
  198.     }
  199.     public void setScope(String scope) {
  200.         this.scope = scope;
  201.     }
  202.     public int getStart() {
  203.         return start;
  204.     }
  205.     public void setStart(int start) {
  206.         this.start = start;
  207.     }
  208.     public String getStyle() {
  209.         return style;
  210.     }
  211.     public void setStyle(String style) {
  212.         this.style = style;
  213.     }
  214.     public String getOnchange() {
  215.         return onchange;
  216.     }
  217.     public void setOnchange(String onchange) {
  218.         this.onchange = onchange;
  219.     }
  220.     public String getOnclick() {
  221.         return onclick;
  222.     }
  223.     public void setOnclick(String onclick) {
  224.         this.onclick = onclick;
  225.     }
  226.     public String getOndblclick() {
  227.         return ondblclick;
  228.     }
  229.     public void setOndblclick(String ondblclick) {
  230.         this.ondblclick = ondblclick;
  231.     }
  232. }

 Step3:页面调用

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="select" uri="/WEB-INF/selectTag.tld"%>

<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>
<%
 List l = new LinkedList();
 l.add("hello");
 l.add("java");
 l.add("world");
 request.setAttribute("l", l);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">

  <title>My JSP 'index.jsp' starting page</title>

  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

 </head>

 <body>
<select:option optionValues="${l}"/>
 </body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值