⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 radio.java

📁 jakarta-taglibs
💻 JAVA
字号:
/*
 * Copyright 1999,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package org.apache.taglibs.input;

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

/**
 *
 *  This class implements the <input:radio> tag, which presents an 
 *  <input type="radio" ... /> form element.
 *
 *  @version 0.90
 *  @author Shawn Bayern
 *  @author Lance Lavandowska
 *  @author Karl von Randow
 */

public class Radio extends TagSupport {

    private String name;        // name of the radio-button group
    private String value;       // value of this particular button
    private String dVal;        // default value if none is found
    private Map attributes;     // attributes of the <input> element
    private String attributesText; // attributes of the <input> element as text
    private String beanId;      // bean id to get default values from

    public int doStartTag() throws JspException {
        try {
            // sanity check (but value CAN be empty)
            if (name == null || name.equals("") || value == null)
                throw new JspTagException(
                    "invalid null or empty 'name' or 'value'");

            // Store beanId in a local variable as we change it
            String beanId = this.beanId;

            // Get default beanId
            if ( beanId == null ) {
                beanId = Util.defaultFormBeanId( this );
            }
            else if ( beanId.length() == 0 ) {
                // An empty beanId means, do not use any bean - not even default
                beanId = null;
            }

            // get what we need from the page
            ServletRequest req = pageContext.getRequest();
            JspWriter out = pageContext.getOut();

            // start building up the tag
            out.print("<input type=\"radio\" ");
            out.print("name=\"" + Util.quote(name) + "\" ");
            out.print("value=\"" + Util.quote(value) + "\" ");

            // include any attributes we've got here
            Util.printAttributes(out, attributes);
            if ( attributesText != null ) {
                out.print( attributesText + " " );
            }

            // check this button if it's the right one
            String target;
            
            // First check the bean value
            String beanValue = ( beanId != null ? Util.beanPropertyValue(
                pageContext.findAttribute( beanId ), name ) : null );
            if ( beanValue != null ) {
                target = beanValue;
            }
            // If no bean value, check the request - if none, use default
            else if (req.getParameter(name) == null) {
                target = dVal;
            }
            else {
                target = req.getParameter(name);
            }
            
            if (target != null && target.equals(value))
                out.print("checked=\"true\" ");

            // end the tag
            out.print("/>");

        } catch (Exception ex) {
            throw new JspTagException(ex.getMessage());
        }
        return SKIP_BODY;
    }

    public void setName(String x) {
        name = x;
    }

    public void setValue(String x) {
        value = x;
    }

    public void setAttributes(Map x) {
        attributes = x;
    }
    
    public void setAttributesText( String x ) {
        attributesText = x;
    }
    
    public void setBean( String x ) {
        beanId = x;
    }

    public void setDefault(String x) {
        dVal = x;
    }
    
    /** Getter for property name.
     * @return Value of property name.
     */
    public String getName() { return name; }
    
    /** Getter for property default.
     * @return Value of property default.
     */
    public String getDefault() { return dVal; }
    
    /** Getter for property bean.
     * @return Value of property bean.
     */
    public String getBean() { return beanId; }
    
    /** Getter for property attributesText.
     * @return Value of property attributesText.
     */
    public String getAttributesText() { return attributesText; }
    
    /** Getter for property attributes.
     * @return Value of property attributes.
     */
    public Map getAttributes() { return attributes; }
}

⌨️ 快捷键说明

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