selectoption.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 131 行

JAVA
131
字号
package bible.jsp.tags;

import java.io.*;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * Title:        SelectOption
 * Description:  Tag extension to set selected option(s) in a select list
 * Copyright:    Copyright (c) 2001
 * Company:      ZeeWare Inc.
 * @author Gary Wells
 * @version 1.0
 */

public class SelectOption extends BodyTagSupport {

  /** Array of values to be selected */
  String[] values;

  /**
   * Returns an array of selected values.
   * @return    the array of selected values.
   */
  public String[] getValue() {
    return values;
  }

  /**
   * Sets the array of selected values to the array parameter.
   * @param     newArrayValues  the desired array of selected values.
   */
  public void setValue(String[] newArrayValues) {
    values = newArrayValues;
  }

  /**
   * Creates an array of selected values from the single value parameter.
   * @param     newValue  the desired array of selected values.
   */
  public void setValue(String newValue) {
    if (newValue != null) {
      values = new String[1];
      values[0] = newValue;
    }
  }

  /**
   * Creates an array of selected values from the single value parameter.
   * @return    the desired character.
   * @exception JspTagException
   *              if an error occurs while writing to the JSP writer.
   * @see       javax.servlet.jsp.JspWriter#write()
   */
  public int doEndTag() throws JspTagException {

    // get body content
    String bodyString = bodyContent.getString();

    if(values != null && values.length > 0) {

      // make a string array of option tags
      StringTokenizer bodyTokens = new StringTokenizer(bodyString,"\n");
      ArrayList optionsList = new ArrayList();
      while (bodyTokens.hasMoreTokens()) {
        optionsList.add(bodyTokens.nextToken());
      }
      String[] optionsArray = new String[optionsList.size()];
      optionsArray = (String[])optionsList.toArray(optionsArray);

      // declare work fields
      String optionBeforeValue;
      String optionAfterValue;
      String optionValue;
      String opt;
      StringBuffer optionsOut = new StringBuffer();
      boolean found = false;

      // parse through the array looking for selected values
      for (int i = 0; i < optionsArray.length; i++) {
        opt = optionsArray[i];
        if (opt.trim().length() > 0) {

          // before option value
          optionBeforeValue = opt.substring(0,opt.indexOf("value="));

          //option value
          optionValue = opt.substring(opt.indexOf("value=")+6,opt.indexOf(">"));
          if (optionValue.startsWith("\""))
            optionValue = optionValue.substring(1,optionValue.length());
          if (optionValue.endsWith("\""))
            optionValue = optionValue.substring(0,optionValue.length()-1);

          // after option value
          optionAfterValue = opt.substring(opt.indexOf("value="),opt.length());

          // if a selected value is found, add to the buffer with "SELECTED"
          found = false;
          for (int j = 0; j < values.length ; j++) {
            if (optionValue.equals(values[j])) {
              found = true;
              optionsOut.append(optionBeforeValue + "SELECTED " + optionAfterValue + "\n");
            }
          }

          // if not found, add to the buffer as is
          if (found == false) {
            optionsOut.append(opt + "\n");
          }
        }
      }

      // write the buffer
      try {
        pageContext.getOut().write(optionsOut.toString());
      } catch (IOException ex) {
        throw new JspTagException("Fatal IO error");
      }

    // no values, write the body content as is
    } else {
      try {
        pageContext.getOut().write(bodyString);
      } catch (IOException ex) {
        throw new JspTagException("Fatal IO error");
      }
    }
    return EVAL_PAGE;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?