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

📄 selectcollectiontag.java

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

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.util.MessageResources;

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

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;

public class SelectCollectionTag extends BodyTagSupport {
    private Log logger = LogFactory.getLog(SelectCollectionTag.class);
    /**
     * The message resources for this package.
     */
    protected static MessageResources messages =
            MessageResources.getMessageResources(Constants.Package + ".LocalStrings");

    private Long styleId;
    private String name = "";

    private String property = "";

    private Collection collection;

    private String valueProperty = "value";

    private String labelProperty = "label";

    private String styleClass = "";

    private String onChange = "";

    private String defaultValue = "";

    private String multiple = "";

    private String size = "";

    private String style = "";

    private String message = "";

    private String match[] = null;
    
    protected String provider;
    protected String params;

    /**
     * The session scope key under which our Locale is stored.
     */
    protected String localeKey = Globals.LOCALE_KEY;

    /**
     * @return .
     */
    public String getLocale() {
        return (this.localeKey);
    }

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

    /**
     * The servlet context attribute key for our resources.
     */
    protected String bundle = null;

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

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

    /**
     * @return .
     * @throws JspException .
     */
    public int doStartTag() throws JspException {
        Collection optionsData = this.collection;
        if(optionsData == null) {
        	try {
        		IDataProvider provider = (IDataProvider)ObjectUtil.createObject(this.provider);
        		try {
        			Object value = TagUtils.getInstance().lookup(pageContext, getName(),this.params,null);
        			if(value == null) {
        				optionsData = provider.getData(this.params);
        			} else {
        				optionsData = provider.getData(value.toString());
        			}
        			
            	}catch(Exception e) {
            		optionsData = provider.getData(this.params);
            	}
        	}catch(Exception e) {
        		e.printStackTrace();
        		optionsData = null;
            }
        }
        try {
            Object root = null;
            Object selectedValue = null;
            if (name != null && name.length() > 0) {
                root = TagUtils.getInstance().lookup(pageContext, getName(), null);
                selectedValue = TagUtils.getInstance().lookup(pageContext, getName(), getProperty(), null);
                if (selectedValue != null) {
                    logger.info("selectedValue : " + selectedValue.toString());
                }

            }

            TagUtils.getInstance().write(pageContext,
                    "<select name='"
                            + getProperty()
                            + "' "
                            + (getMultiple() == null
                            || getMultiple().equals("") ? " "
                            : " multiple ")
                            + (getSize() == null
                            || getSize().equals("") ? ""
                            : " size=" + getSize() + " ")
                            + (getStyleId() == null? "" : " id=" + getStyleId() + " ")
                            + (getStyle() == null
                            || getStyle().equals("") ? "  "
                            : " style=" + getStyle() + " ")
                            + (getStyleClass() == null
                            || getStyleClass().equals("") ? ""
                            : " class=" + getStyleClass())
                            + (getOnChange() == null
                            || getOnChange().equals("") ? ""
                            : " onchange=" + getOnChange()
                            + "()") + ">");

            if (defaultValue != null && defaultValue.length() > 0) {
                if (defaultValue.equalsIgnoreCase("true")) {
                    TagUtils.getInstance().write(pageContext, "<option value=''> </option>\n");
                }
                else if (defaultValue.equalsIgnoreCase("false")) {
                    //todo
                }
                else {
                	defaultValue = TagUtils.getInstance().filter(defaultValue);
                    TagUtils.getInstance().write(pageContext, "<option value=''>"
                            + defaultValue + "</option>\n");
                }
            }

            if (optionsData != null) {
                for (Iterator it = optionsData.iterator(); it.hasNext();) {
                    Object option = it.next();
                    if (option != null) {
                        Object label = PropertyUtils.getProperty(option, getLabelProperty());
                        if (message.equalsIgnoreCase("true")) {
                            label = TagUtils.getInstance().message(pageContext, this.bundle, this.localeKey, (String) label);
                        }

                        Object value = PropertyUtils.getProperty(option,
                                getValueProperty());
                        TagUtils.getInstance().write(pageContext, "<option value='"
                                + value + "'  " + isDefault(selectedValue, value, multiple)
                                + ">" + label + "</option>\n");
                    }
                }
            }

            TagUtils.getInstance().write(pageContext, "</select>\n");
        }
        catch (IllegalAccessException ex) {
            throw new JspException(ex);
        }
        catch (InvocationTargetException e) {
            throw new JspException(e);
        }
        catch (NoSuchMethodException e) {
            throw new JspException(e);
        }
        catch (Exception e) {
            throw new JspException(e);
        }
        return BodyTagSupport.EVAL_PAGE;
    }

    /**
     * @param one      .
     * @param other    .
     * @param multiple .
     * @return .
     */
    private static String isDefault(Object one, Object other, String multiple) {
        if (one == null && other == null) {
            return "selected";
        }
        else if (one == null || other == null) {
            return "";
        }
        else if (multiple.equals("true")) {
            if (one instanceof String[]) {
                String[] two = (String[]) one;
                for (int i = 0; i < two.length; i++) {
                    if (other.toString().equals(two[i].toString()))
                        return "selected";
                }
                return "";
            }
            else if (one instanceof Long[]) {
                Long[] two = (Long[]) one;
                for (int i = 0; i < two.length; i++) {
                    if (other.toString().equals(two[i].toString()))
                        return "selected";
                }
                return "";
            }

            return "";

        }
        else {
            return one == other || one.toString().equals(other.toString()) ? "selected"
                    : "";
        }
    }

    /**
     * @return .
     * @throws JspException .
     */
    public int doEndTag() throws JspException {
        return BodyTagSupport.EVAL_PAGE;
    }

    /**
     * @return .
     */
    public static MessageResources getMessages() {
        return messages;
    }

    /**
     * @param messages /
     */
    public static void setMessages(MessageResources messages) {
        SelectCollectionTag.messages = messages;
    }

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

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

    /**
     * @return .
     */
    public String getLabelProperty() {
        return labelProperty;
    }

    /**
     * @param labelProperty .
     */
    public void setLabelProperty(String labelProperty) {
        this.labelProperty = labelProperty;
    }

    /**
     * @return .
     */
    public String getMultiple() {
        return multiple;
    }

    /**
     * @param multiple .
     */
    public void setMultiple(String multiple) {
        this.multiple = multiple;
    }

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

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

    /**
     * @return .
     */
    public String getOnChange() {
        return onChange;
    }

    /**
     * @param onChange .
     */
    public void setOnChange(String onChange) {
        this.onChange = onChange;
    }

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

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

    /**
     * @return .
     */
    public String getSize() {
        return size;
    }

    /**
     * @param size .
     */
    public void setSize(String size) {
        this.size = size;
    }

    /**
     * @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 getValueProperty() {
        return valueProperty;
    }

    /**
     * @param valueProperty ;
     */
    public void setValueProperty(String valueProperty) {
        this.valueProperty = valueProperty;
    }

    /**
     * @return .
     */
    public Collection getCollection() {
        return collection;
    }

    /**
     * @param collection .
     */
    public void setCollection(Collection collection) {
        this.collection = collection;
    }

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

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

	/**
	 * @return Returns the styleId.
	 */
	public Long getStyleId() {
		return styleId;
	}

	/**
	 * @param styleId The styleId to set.
	 */
	public void setStyleId(Long styleId) {
		this.styleId = styleId;
	}

	/**
	 * @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;
	}

	

	


}

⌨️ 快捷键说明

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