这个是Radio taglib的源码:package org.apache.struts.taglib; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.JspWriter; import org.apache.struts.util.BeanUtils; import org.apache.struts.util.MessageResources; /** * Tag for input fields of type "radio". * * @author Craig R. McClanahan * @version $Revision: 1.11 $ $Date: 2001/05/20 01:19:06 $ */public final class RadioTag extends BaseHandlerTag { // ----------------------------------------------------- Instance Variables /** * The message resources for this package. */ protected static MessageResources messages = MessageResources.getMessageResources ("org.apache.struts.taglib.LocalStrings"); /** * The name of the bean containing our underlying property. */ private String name = Constants.BEAN_KEY; public String getName() { return (this.name); } public void setName(String name) { this.name = name; } /** * The property name for this field. */ private String property = null; /** * The server value for this option. */ private String value = null; // ------------------------------------------------------------- Properties /** * Return the property name. */ public String getProperty() { return (this.property); } /** * Set the property name. * * @param name The new property name */ public void setProperty(String property) { this.property = property; } /** * Return the server value. */ public String getValue() { return (this.value); } /** * Set the server value. * * @param value The new server value */ public void setValue(String value) { this.value = value; }
// --------------------------------------------------------- Public Methods /** * Generate the required input tag. * * @exception JspException if a JSP exception has occurred */ public int doStartTag() throws JspException { // Acquire the current value of the appropriate field Object current = null; Object bean = pageContext.findAttribute(name); if (bean == null) throw new JspException (messages.getMessage("getter.bean", name)); try { current = BeanUtils.getSimpleProperty(bean, property); if (current == null) current = ""; } catch (IllegalAccessException e) { throw new JspException (messages.getMessage("getter.access", property, name)); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); throw new JspException (messages.getMessage("getter.result", property, t.toString())); } catch (NoSuchMethodException e) { throw new JspException (messages.getMessage("getter.method", property, name)); } // Create an appropriate "input" element based on our parameters StringBuffer results = new StringBuffer("<input type=\"radio\""); results.append(" name=\""); results.append(this.property); results.append("\""); if (accessKey != null) { results.append(" accesskey=\""); results.append(accessKey); results.append("\""); } if (tabIndex != null) { results.append(" tabindex=\""); results.append(tabIndex); results.append("\""); } results.append(" value=\""); results.append(this.value); results.append("\""); if (value.equals(current)) results.append(" checked"); results.append(prepareEventHandlers()); results.append(prepareStyles()); results.append(">"); // Print this field to our output writer JspWriter writer = pageContext.getOut(); try { writer.print(results.toString()); } catch (IOException e) { throw new JspException (messages.getMessage("common.io", e.toString())); } // Continue processing this page return (EVAL_BODY_TAG); } /** * Optionally render the associated label from the body content. * * @exception JspException if a JSP exception has occurred */ public int doEndTag() throws JspException { if (bodyContent == null) return (EVAL_PAGE); JspWriter writer = pageContext.getOut(); try { writer.println(bodyContent.getString().trim()); } catch (IOException e) { throw new JspException (messages.getMessage("common.io", e.toString())); } // Continue evaluating this page return (EVAL_PAGE); } /** * Release any acquired resources. */ public void release() { super.release(); name = Constants.BEAN_KEY; property = null; value = null; } }
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.JspWriter;
import org.apache.struts.util.BeanUtils;
import org.apache.struts.util.MessageResources;
/**
* Tag for input fields of type "radio".
*
* @author Craig R. McClanahan
* @version $Revision: 1.11 $ $Date: 2001/05/20 01:19:06 $
*/public final class RadioTag extends BaseHandlerTag {
// ----------------------------------------------------- Instance Variables
/**
* The message resources for this package.
*/
protected static MessageResources messages =
MessageResources.getMessageResources
("org.apache.struts.taglib.LocalStrings");
/**
* The name of the bean containing our underlying property.
*/
private String name = Constants.BEAN_KEY; public String getName() {
return (this.name);
} public void setName(String name) {
this.name = name;
}
/**
* The property name for this field.
*/
private String property = null;
/**
* The server value for this option.
*/
private String value = null;
// ------------------------------------------------------------- Properties
/**
* Return the property name.
*/
public String getProperty() { return (this.property); }
/**
* Set the property name.
*
* @param name The new property name
*/
public void setProperty(String property) { this.property = property; }
/**
* Return the server value.
*/
public String getValue() { return (this.value); }
/**
* Set the server value.
*
* @param value The new server value
*/
public void setValue(String value) { this.value = value; }
/**
* Generate the required input tag.
*
* @exception JspException if a JSP exception has occurred
*/
public int doStartTag() throws JspException { // Acquire the current value of the appropriate field
Object current = null;
Object bean = pageContext.findAttribute(name);
if (bean == null)
throw new JspException
(messages.getMessage("getter.bean", name));
try {
current = BeanUtils.getSimpleProperty(bean, property);
if (current == null)
current = "";
} catch (IllegalAccessException e) {
throw new JspException
(messages.getMessage("getter.access", property, name));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException
(messages.getMessage("getter.result",
property, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException
(messages.getMessage("getter.method", property, name));
} // Create an appropriate "input" element based on our parameters
StringBuffer results = new StringBuffer("<input type=\"radio\"");
results.append(" name=\"");
results.append(this.property);
results.append("\"");
if (accessKey != null) {
results.append(" accesskey=\"");
results.append(accessKey);
results.append("\"");
}
if (tabIndex != null) {
results.append(" tabindex=\"");
results.append(tabIndex);
results.append("\"");
}
results.append(" value=\"");
results.append(this.value);
results.append("\"");
if (value.equals(current))
results.append(" checked");
results.append(prepareEventHandlers());
results.append(prepareStyles());
results.append(">"); // Print this field to our output writer
JspWriter writer = pageContext.getOut();
try {
writer.print(results.toString());
} catch (IOException e) {
throw new JspException
(messages.getMessage("common.io", e.toString()));
} // Continue processing this page
return (EVAL_BODY_TAG); }
/**
* Optionally render the associated label from the body content.
*
* @exception JspException if a JSP exception has occurred
*/
public int doEndTag() throws JspException { if (bodyContent == null)
return (EVAL_PAGE); JspWriter writer = pageContext.getOut();
try {
writer.println(bodyContent.getString().trim());
} catch (IOException e) {
throw new JspException
(messages.getMessage("common.io", e.toString()));
} // Continue evaluating this page
return (EVAL_PAGE); }
/**
* Release any acquired resources.
*/
public void release() { super.release();
name = Constants.BEAN_KEY;
property = null;
value = null; }
}