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

📄 imgtag.java

📁 这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用struts,hebinate,xml等技术,有丰富的tag,role,navigation,session,dictio
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }

    /**
     * The image source URI.
     */
    protected String src = null;

    public String getSrc() {
        return (this.src);
    }

    public void setSrc(String src) {
        this.src = src;
    }

    /**
     * The message resources key under which we should look up the
     * <code>src</code> attribute for this generated tag, if any.
     */
    protected String srcKey = null;

    public String getSrcKey() {
        return (this.srcKey);
    }

    public void setSrcKey(String srcKey) {
        this.srcKey = srcKey;
    }

    /**
     * Client-side image map declaration.
     */
    protected String usemap = null;

    public String getUsemap() {
        return (this.usemap);
    }

    public void setUsemap(String usemap) {
        this.usemap = usemap;
    }

    /**
     * The vertical spacing around the image.
     */
    protected String vspace = null;

    public String getVspace() {
        return (this.vspace);
    }

    public void setVspace(String vspace) {
        this.vspace = vspace;
    }

    /**
     * The image width.
     */
    protected String width = null;

    public String getWidth() {
        return (this.width);
    }

    public void setWidth(String width) {
        this.width = width;
    }

    // --------------------------------------------------------- Public Methods

    /**
     * Render the beginning of the IMG tag.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {

        // Evaluate the body of this tag
        return (EVAL_BODY_TAG);

    }

    /**
     * Render the end of the IMG tag.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doEndTag() throws JspException {

        // Generate the name definition or image element
        HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
        StringBuffer results = new StringBuffer("<img");
        String tmp = src();
        String srcurl = url(tmp);
        if (srcurl != null) {
            results.append(" src=\"");
            results.append(response.encodeURL(srcurl));
            results.append("\"");
        }
        String lowsrcurl = url(this.lowsrc);
        if (lowsrcurl != null) {
            results.append(" lowsrc=\"");
            results.append(response.encodeURL(lowsrcurl));
            results.append("\"");
        }
        if (imageName != null) {
            results.append(" name=\"");
            results.append(imageName);
            results.append("\"");
        }
        if (height != null) {
            results.append(" height=\"");
            results.append(height);
            results.append("\"");
        }
        if (width != null) {
            results.append(" width=\"");
            results.append(width);
            results.append("\"");
        }
        if (align != null) {
            results.append(" align=\"");
            results.append(align);
            results.append("\"");
        }
        if (border != null) {
            results.append(" border=\"");
            results.append(border);
            results.append("\"");
        }
        if (hspace != null) {
            results.append(" hspace=\"");
            results.append(hspace);
            results.append("\"");
        }
        if (vspace != null) {
            results.append(" vspace=\"");
            results.append(vspace);
            results.append("\"");
        }
        if (ismap != null) {
            results.append(" ismap=\"");
            results.append(ismap);
            results.append("\"");
        }
        if (usemap != null) {
            results.append(" usemap=\"");
            results.append(usemap);
            results.append("\"");
        }
        results.append(prepareStyles());
        results.append(prepareEventHandlers());
        results.append(getElementClose());

        // Print this element to our output writer
        ResponseUtils.write(pageContext, results.toString());

        // Evaluate the reaminder of this page
        return (EVAL_PAGE);

    }

    /**
     * Release any acquired resources.
     */
    public void release() {

        super.release();

        border = null;
        height = null;
        hspace = null;
        imageName = null;
        ismap = null;
        lowsrc = null;
        name = null;
        page = null;
        pageKey = null;
        paramId = null;
        paramName = null;
        paramProperty = null;
        paramScope = null;
        property = null;
        scope = null;
        src = null;
        srcKey = null;
        usemap = null;
        vspace = null;
        width = null;

    }

    // ------------------------------------------------------ Protected Methods

    /**
     * Return the base source URL that will be rendered in the <code>src</code>
     * property for this generated element, or <code>null</code> if there is
     * no such URL.
     *
     * @exception JspException if an error occurs
     */
    protected String src() throws JspException {
    	if( src == null ) return "";
    	if( src.startsWith("/") || src.startsWith(".") ){
    		return src;
    	}
    	
    	String getImgPath="";
    	try{
			StyleManager sm = (StyleManager)
				BeanContainerFactory.getBeanContainer()
					.getBean( StyleManager.BEAN_NAME );
			getImgPath = sm.getImgPath((HttpServletRequest)pageContext.getRequest() );
    	}catch(Exception e){
    		log.error("error in imgTag access StyleManager:"+ e);
    	}
		
		return getImgPath + src ;
    }

    /**
     * Return the specified src URL, modified as necessary with optional
     * request parameters.
     *
     * @param url The URL to be modified (or null if this url will not be used)
     *
     * @exception JspException if an error occurs preparing the URL
     */
    protected String url(String url) throws JspException {

        if (url == null) {
            return (url);
        }

        // Start with an unadorned URL as specified
        StringBuffer src = new StringBuffer(url);

        // Append a single-parameter name and value, if requested
        if ((paramId != null) && (paramName != null)) {
            if (src.toString().indexOf('?') < 0) {
                src.append('?');
            } else {
                src.append("&amp;");
            }
            src.append(paramId);
            src.append('=');
            Object value = RequestUtils.lookup(pageContext, paramName, paramProperty, paramScope);
            if (value != null)
                src.append(RequestUtils.encodeURL(value.toString()));
        }

        // Just return the URL if there is no bean to look up
        if ((property != null) && (name == null)) {
            JspException e = new JspException(messages.getMessage("getter.name"));
            RequestUtils.saveException(pageContext, e);
            throw e;
        }

        if (name == null || property == null) {
            return (src.toString());
        }

        // Look up the map we will be using
        Object mapObject = RequestUtils.lookup(pageContext, name, property, scope);
        Map map = null;
        try {
            map = (Map) mapObject;
        } catch (ClassCastException e) {
            RequestUtils.saveException(pageContext, e);
            throw new JspException(messages.getMessage("imgTag.type"));
        }

        // Append the required query parameters
        boolean question = (src.toString().indexOf("?") >= 0);
        Iterator keys = map.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            Object value = map.get(key);
            if (value == null) {
                if (question) {
                    src.append("&amp;");
                } else {
                    src.append('?');
                    question = true;
                }
                src.append(key);
                src.append('=');
                // Interpret null as "no value specified"
            } else if (value instanceof String[]) {
                String values[] = (String[]) value;
                for (int i = 0; i < values.length; i++) {
                    if (question) {
                        src.append("&amp;");
                    } else {
                        src.append('?');
                        question = true;
                    }
                    src.append(key);
                    src.append('=');
                    src.append(RequestUtils.encodeURL(values[i]));
                }
            } else {

                if (question) {
                    src.append("&amp;");
                } else {
                    src.append('?');
                    question = true;
                }
                src.append(key);
                src.append('=');
                src.append(RequestUtils.encodeURL(value.toString()));
            }
        }

        // Return the final result
        return (src.toString());

    }

}

⌨️ 快捷键说明

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