Step1:编写selectTag.tld文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
- version="2.0">
- <description>select tag</description>
- <display-name>select tag</display-name>
- <tlib-version>1.1</tlib-version>
- <short-name>select</short-name>
- <tag>
- <description>This option tag</description>
- <name>option</name><!-- tag's name -->
- <tag-class>org.eagle.testFunction.SelectTag</tag-class><!-- This is handle Class -->
- <body-content>empty</body-content><!-- whether or not have tag body,empty is havn't,JSP is have-->
- <attribute>
- <description>
- The test condition that tells whether or not the body
- content should be processed
- </description>
- <name>style</name><!-- attribute's name -->
- <required>false</required><!-- the attribute is required -->
- <rtexprvalue>false</rtexprvalue><!-- false is plain text -->
- </attribute>
- <attribute>
- <description>
- The test condition that tells whether or not the body
- content should be processed
- </description>
- <name>optionValues</name><!-- attribute's name -->
- <required>false</required><!-- the attribute is required -->
- <rtexprvalue>true</rtexprvalue><!-- false is plain text,true is not -->
- </attribute>
- <attribute>
- <description>
- The test condition that tells whether or not the body
- content should be processed
- </description>
- <name>optionTexts</name><!-- attribute's name -->
- <required>false</required><!-- the attribute is required -->
- <rtexprvalue>true</rtexprvalue><!-- false is plain text,true is not -->
- </attribute>
- <attribute>
- <description>
- The test condition that tells whether or not the body
- content should be processed
- </description>
- <name>start</name><!-- attribute's name -->
- <required>false</required><!-- the attribute is required -->
- <rtexprvalue>false</rtexprvalue><!-- false is plain text,true is not -->
- </attribute>
- <attribute>
- <description>
- The test condition that tells whether or not the body
- content should be processed
- </description>
- <name>end</name><!-- attribute's name -->
- <required>false</required><!-- the attribute is required -->
- <rtexprvalue>false</rtexprvalue><!-- false is plain text,true is not -->
- </attribute>
- <attribute>
- <description>
- The test condition that tells whether or not the body
- content should be processed
- </description>
- <name>name</name><!-- attribute's name -->
- <required>false</required><!-- the attribute is required -->
- <rtexprvalue>false</rtexprvalue><!-- false is plain text,true is not -->
- </attribute>
- <attribute>
- <description>
- The test condition that tells whether or not the body
- content should be processed
- </description>
- <name>defaultValue</name><!-- attribute's name -->
- <required>false</required><!-- the attribute is required -->
- <rtexprvalue>false</rtexprvalue><!-- false is plain text,true is not -->
- </attribute>
- <attribute>
- <description>
- The test condition that tells whether or not the body
- content should be processed
- </description>
- <name>id</name><!-- attribute's name -->
- <required>false</required><!-- the attribute is required -->
- <rtexprvalue>false</rtexprvalue><!-- false is plain text,true is not -->
- </attribute>
- <attribute>
- <name>onchange</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>onclick</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>ondblclick</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- </taglib>
Step2:编写处理类org.eagle.testFunction.SelectTag.java
- /**
- *
- */
- package org.eagle.testFunction;
- import java.io.IOException;
- import java.util.List;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.PageContext;
- import javax.servlet.jsp.tagext.TagSupport;
- /**
- * @author zhenhuaZhu
- *
- */
- @SuppressWarnings("serial")
- public class SelectTag extends TagSupport {
- private PageContext pageContext;
- private String defaultValue = "0";
- private List optionValues;
- private List optionTexts;
- private String style;
- private int start;
- private int end;
- private String name;
- private String id;
- private String scope;
- private String onclick;
- private String onchange;
- private String ondblclick;
- private StringBuffer sb;
- @Override
- public int doStartTag() throws JspException {
- sb = new StringBuffer("<select ");
- if (this.name != null) {
- sb.append("name='" + this.name + "' ");
- }
- if (this.id != null) {
- sb.append("id='" + this.id + "' ");
- }
- if (this.onchange != null) {
- sb.append("οnchange='" + this.onchange + "' ");
- }
- if (this.onclick != null) {
- sb.append("οnclick='" + this.onclick + "' ");
- }
- if (this.ondblclick != null) {
- sb.append("οndblclick='" + this.ondblclick + "' ");
- }
- sb.append(">");
- fillDefaultValueObj();
- constractOption();
- sb.append("</select>");
- try {
- this.pageContext.getOut().write(sb.toString());
- sb = null;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return TagSupport.EVAL_PAGE;
- }
- private void fillDefaultValueObj() {
- if (null == this.scope) {
- this.scope = "request";
- }
- if (null == this.name) {
- return;
- }
- Object defaultValueObj = null;
- if ("request".equalsIgnoreCase(this.scope)) {
- defaultValueObj = this.pageContext.getRequest().getAttribute(
- this.name);
- } else if ("session".equalsIgnoreCase(this.scope)) {
- defaultValueObj = this.pageContext.getSession().getAttribute(
- this.name);
- } else if ("application".equalsIgnoreCase(this.scope)) {
- defaultValueObj = this.pageContext.getServletContext()
- .getAttribute(this.name);
- }
- if (null != defaultValueObj) {
- this.defaultValue = (String) defaultValueObj;
- }
- }
- private void constractOption() {
- if (this.start >= 0 && this.end > 0 && this.end >= this.start) {
- for (int i = this.start; i <= this.end; ++i) {
- if (!this.defaultValue.equalsIgnoreCase(String.valueOf(i))) {
- sb.append("<option value='" + i + "'" + ">" + i
- + "</option>");
- } else {
- sb.append("<option value='" + i + "'" + "selected>" + i
- + "</option>");
- }
- }
- return;
- }
- if (null != this.optionValues) {
- if (null != this.optionTexts) {
- for (int i = 0, len = this.optionValues.size(); i < len; ++i) {
- if (!this.defaultValue
- .equalsIgnoreCase((String) this.optionValues.get(i))) {
- sb.append("<option value='" + this.optionValues.get(i)
- + "'" + ">" + this.optionTexts.get(i)
- + "</option>");
- } else {
- sb.append("<option value='" + this.optionValues.get(i)
- + "'" + "selected>" + this.optionTexts.get(i)
- + "</option>");
- }
- }
- } else {
- for (int i = 0, len = this.optionValues.size(); i < len; ++i) {
- if (!this.defaultValue
- .equalsIgnoreCase((String) this.optionValues.get(i))) {
- sb.append("<option value='" + this.optionValues.get(i)
- + "'" + ">" + i + "</option>");
- } else {
- sb.append("<option value='" + this.optionValues.get(i)
- + "'" + "selected>" + i + "</option>");
- }
- }
- }
- } else {
- if (0 == this.end) {
- sb.append("</select>");
- } else if (0 != end) {
- if (null != this.optionTexts) {
- for (int i = 0, len = this.optionTexts.size(); i < len; ++i) {
- if (!this.defaultValue
- .equalsIgnoreCase((String) this.optionValues
- .get(i))) {
- sb.append("<option value='" + i + "'" + ">"
- + this.optionTexts.get(i) + "</option>");
- } else {
- sb.append("<option value='" + i + "'" + "selected>"
- + this.optionTexts.get(i) + "</option>");
- }
- }
- } else {
- for (int i = 0; i < this.end; ++i) {
- if (!this.defaultValue
- .equalsIgnoreCase((String) this.optionValues
- .get(i))) {
- sb.append("<option value='" + i + "'" + ">" + i
- + "</option>");
- } else {
- sb.append("<option value='" + i + "'" + "selected>"
- + i + "</option>");
- }
- }
- }
- }
- }
- }
- @Override
- public void setPageContext(PageContext arg0) {
- this.pageContext = arg0;
- }
- public String getDefaultValue() {
- return defaultValue;
- }
- public void setDefaultValue(String defaultValue) {
- this.defaultValue = defaultValue;
- }
- public void setDefaultValue(int defaultValue) {
- this.defaultValue = String.valueOf(defaultValue);
- }
- public int getEnd() {
- return end;
- }
- public void setEnd(int end) {
- this.end = end;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public List getOptionTexts() {
- return optionTexts;
- }
- public void setOptionTexts(List optionTexts) {
- this.optionTexts = optionTexts;
- }
- public List getOptionValues() {
- return optionValues;
- }
- public void setOptionValues(List optionValues) {
- this.optionValues = optionValues;
- }
- public String getScope() {
- return scope;
- }
- public void setScope(String scope) {
- this.scope = scope;
- }
- public int getStart() {
- return start;
- }
- public void setStart(int start) {
- this.start = start;
- }
- public String getStyle() {
- return style;
- }
- public void setStyle(String style) {
- this.style = style;
- }
- public String getOnchange() {
- return onchange;
- }
- public void setOnchange(String onchange) {
- this.onchange = onchange;
- }
- public String getOnclick() {
- return onclick;
- }
- public void setOnclick(String onclick) {
- this.onclick = onclick;
- }
- public String getOndblclick() {
- return ondblclick;
- }
- public void setOndblclick(String ondblclick) {
- this.ondblclick = ondblclick;
- }
- }
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>