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

📄 radiocollectiontag.java

📁 struts+spring+hibernate自创框架
💻 JAVA
字号:
package com.pegasus.framework.component.taglib.html;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.IteratorUtils;
import org.apache.struts.Globals;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.util.MessageResources;

import com.pegasus.framework.bo.IDataProvider;
import com.pegasus.framework.util.ObjectUtil;
import com.pegasus.framework.util.StringUtil;


public class RadioCollectionTag extends TagSupport {
    protected static MessageResources messages = MessageResources.getMessageResources("org.apache.struts.taglib.html.LocalStrings");
    protected boolean filter;
    protected String name;
    protected String property;
    private String style;
    private String styleClass;
    private String radioValue;

    protected String collectionName;
    protected String collectionProperty;
    protected String label;
    protected String value;
    protected boolean nowrap = false;
    
    protected String provider;
    protected String params;
    protected String message;
    protected String bundle;
    protected String localeKey = Globals.LOCALE_KEY;
    private String defaultValue;
    
    /**
     * @return .
     */
    public String getLocale() {
        return (this.localeKey);
    }

    /**
     * @param localeKey .
     */
    public void setLocale(String localeKey) {
        this.localeKey = localeKey;
    }

    /**
     *
     */
    public RadioCollectionTag() {
        filter = true;
        label = "label";
        name = "org.apache.struts.taglib.html.BEAN";
        property = null;
        style = null;
        styleClass = null;
        value = "value";
        provider = "";
        params = "";
    }


    /**
     * @return .
     */
    public String getCollection() {
        return collectionName;
    }

    /**
     * @param collectionName .
     */
    public void setCollectionName(String collectionName) {
        this.collectionName = collectionName;
    }

    /**
     * @return .
     */
    public String getCollectionProperty() {
        return collectionProperty;
    }

    /**
     * @param collectionProperty .
     */
    public void setCollectionProperty(String collectionProperty) {
        this.collectionProperty = collectionProperty;
    }

    /**
     * @return .
     */
    public boolean isFilter() {
        return filter;
    }

    /**
     * @param filter .
     */
    public void setFilter(boolean filter) {
        this.filter = filter;
    }

    /**
     * @return .
     */
    public String getLabel() {
        return label;
    }

    /**
     * @param label .
     */
    public void setLabel(String label) {
        this.label = label;
    }

    /**
     * @return .
     */
    public String getName() {
        return name;
    }

    /**
     * @param name .
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return .
     */
    public boolean isNowrap() {
        return nowrap;
    }

    /**
     * @param nowrap .
     */
    public void setNowrap(boolean nowrap) {
        this.nowrap = nowrap;
    }

    /**
     * @return .
     */
    public String getProperty() {
        return property;
    }

    /**
     * @param property .
     */
    public void setProperty(String property) {
        this.property = property;
    }

    /**
     * @return .
     */
    public String getStyle() {
        return style;
    }

    /**
     * @param style .
     */
    public void setStyle(String style) {
        this.style = style;
    }

    /**
     * @return .
     */
    public String getStyleClass() {
        return styleClass;
    }

    /**
     * @param styleClass .
     */
    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    /**
     * @return .
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value .
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * @return .
     */
    public String getRadioValue() {
        return radioValue;
    }

    /**
     * @param radioValue .
     */
    public void setRadioValue(String radioValue) {
        this.radioValue = radioValue;
    }

    /**
     * @param value .
     * @return .
     * @throws JspException .
     */
    protected String formatValue(Object value)
            throws JspException {
        if (value == null) {
        	if(StringUtil.isEmpty(this.defaultValue)) {
        		return "";
        	}
        	value = this.defaultValue;
        }
        return TagUtils.getInstance().filter(value.toString());
    }

    /**
     * @return .
     * @throws JspException .
     */
    public int doStartTag()
            throws JspException {

        Object obj = TagUtils.getInstance().lookup(pageContext, name, null);
        Object avalue = null;
        try {
            avalue = BeanUtils.getProperty(obj, property);
        }
        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));
        }
        radioValue = formatValue(avalue);
        //从内存中查找集合变量
        Object collection = null;
        try {
        	collection = TagUtils.getInstance().lookup(pageContext, collectionName, collectionProperty, null);
        } catch(Exception e) {
        	collection = null;
        }
       
        if(collection == null) {
        	try {
        		IDataProvider provider = (IDataProvider)ObjectUtil.createObject(this.provider);
            	collection = provider.getData(this.params);
        	}catch(Exception e) {
            	collection = null;
            }
        	
        }
        if (collection == null) {
            JspException e = new JspException(messages.getMessage("radioCollectionTag.collection"));
            TagUtils.getInstance().saveException(pageContext, e);
            throw e;
        }
        Iterator iter = getIterator(collection);
        StringBuffer sb = new StringBuffer();
        String stringLabel;
        String stringValue;

        for (; iter.hasNext(); addRadio(sb, stringLabel, stringValue)) {
            Object bean = iter.next();
            Object beanLabel = null;
            Object beanValue = null;
            try {
                beanLabel = PropertyUtils.getProperty(bean, label);
                if (message.equalsIgnoreCase("true")) {
                	beanLabel = TagUtils.getInstance().message(pageContext, this.bundle, this.localeKey, (String) beanLabel);
                }
                if (beanLabel == null)
                    beanLabel = "";
            }
            catch (IllegalAccessException e) {
                JspException jspe = new JspException(messages.getMessage("getter.access", label, bean));
                TagUtils.getInstance().saveException(pageContext, jspe);
                throw jspe;
            }
            catch (InvocationTargetException e) {
                Throwable t = e.getTargetException();
                JspException jspe = new JspException(messages.getMessage("getter.result", label, t.toString()));
                TagUtils.getInstance().saveException(pageContext, jspe);
                throw jspe;
            }
            catch (NoSuchMethodException e) {
                JspException jspe = new JspException(messages.getMessage("getter.method", label, bean));
                TagUtils.getInstance().saveException(pageContext, jspe);
                throw jspe;
            }
            try {
                beanValue = PropertyUtils.getProperty(bean, value);
                
                if (beanValue == null)
                    beanValue = "";
            }
            catch (IllegalAccessException e) {
                JspException jspe = new JspException(messages.getMessage("getter.access", value, bean));
                TagUtils.getInstance().saveException(pageContext, jspe);
                throw jspe;
            }
            catch (InvocationTargetException e) {
                Throwable t = e.getTargetException();
                JspException jspe = new JspException(messages.getMessage("getter.result", value, t.toString()));
                TagUtils.getInstance().saveException(pageContext, jspe);
                throw jspe;
            }
            catch (NoSuchMethodException e) {
                JspException jspe = new JspException(messages.getMessage("getter.method", value, bean));
                TagUtils.getInstance().saveException(pageContext, jspe);
                throw jspe;
            }
            stringLabel = beanLabel.toString();
            stringValue = beanValue.toString();
        }

        TagUtils.getInstance().write(pageContext, sb.toString());
        return 0;
    }

    /**
     *
     */
    public void release() {
        super.release();
        filter = true;
        label = "label";
        name = "org.apache.struts.taglib.html.BEAN";
        property = null;
        style = null;
        styleClass = null;
        value = "value";
    }

    /**
     * @param buffer      .
     * @param stringLabel .
     * @param stringValue .
     */
    protected void addRadio(StringBuffer buffer, String stringLabel, String stringValue) {
        buffer.append("<input type=\"radio\"");
        buffer.append(" name=\"");
        buffer.append(property);
        buffer.append("\"");
        buffer.append(" value=\"");
        if (filter)
            buffer.append(TagUtils.getInstance().filter(stringValue));
        else
            buffer.append(stringValue);
        buffer.append("\"");
        
        
        boolean matched = false;
        if (stringValue == null)
            matched = (radioValue == null);
        else
            matched = stringValue.equals(radioValue);
        if (matched) {
        	buffer.append(" checked=\"checked\"");
        } 
            
        if (style != null) {
            buffer.append(" style=\"");
            buffer.append(style);
            buffer.append("\"");
        }
        if (styleClass != null) {
            buffer.append(" class=\"");
            buffer.append(styleClass);
            buffer.append("\"");
        }
        buffer.append(">");
        if (filter)
            buffer.append(TagUtils.getInstance().filter(stringLabel));
        else
            buffer.append(stringLabel);
        if (nowrap)
            buffer.append("<br>");
        buffer.append("\r\n");
    }

    /**
     * @param collection .
     * @return .
     * @throws JspException .
     */
    protected Iterator getIterator(Object collection)
            throws JspException {
        if (collection.getClass().isArray())
            collection = Arrays.asList((Object[]) collection);
        if (collection instanceof Collection)
            return ((Collection) collection).iterator();
        if (collection instanceof Iterator)
            return (Iterator) collection;
        if (collection instanceof Map)
            return ((Map) collection).entrySet().iterator();
        if (collection instanceof Enumeration)
            return IteratorUtils.asIterator((Enumeration) collection);
        else
            throw new JspException(messages.getMessage("radioCollectionTag.iterator", collection.toString()));
    }


	/**
	 * @return Returns the params.
	 */
	public String getParams() {
		return params;
	}


	/**
	 * @param params The params to set.
	 */
	public void setParams(String params) {
		this.params = params;
	}


	/**
	 * @return Returns the provider.
	 */
	public String getProvider() {
		return provider;
	}


	/**
	 * @param provider The provider to set.
	 */
	public void setProvider(String provider) {
		this.provider = provider;
	}


	/**
	 * @return Returns the bundle.
	 */
	public String getBundle() {
		return bundle;
	}


	/**
	 * @param bundle The bundle to set.
	 */
	public void setBundle(String bundle) {
		this.bundle = bundle;
	}


	/**
	 * @return Returns the message.
	 */
	public String getMessage() {
		return message;
	}


	/**
	 * @param message The message to set.
	 */
	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * @return Returns the localeKey.
	 */
	public String getLocaleKey() {
		return localeKey;
	}


	/**
	 * @param localeKey The localeKey to set.
	 */
	public void setLocaleKey(String localeKey) {
		this.localeKey = localeKey;
	}

	/**
	 * @return Returns the defaultValue.
	 */
	public String getDefaultValue() {
		return defaultValue;
	}

	/**
	 * @param defaultValue The defaultValue to set.
	 */
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}


}

⌨️ 快捷键说明

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