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

📄 linktag.java

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

import org.apache.struts.taglib.TagUtils;
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.taglib.logic.IterateTag;
import org.apache.struts.util.MessageResources;

import javax.servlet.jsp.JspException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;


public class LinkTag extends ControlTag {
    // ----------------------------------------------------- Instance Variables


    /**
     * The body content of this tag (if any).
     */
    protected String text = null;

    // ------------------------------------------------------------- Properties


    /**
     * The anchor to be added to the end of the generated hyperlink.
     */
    protected String anchor = null;

    /**
     * .
     *
     * @return .
     */
    public String getAnchor() {
        return (this.anchor);
    }

    /**
     * @param anchor .
     */
    public void setAnchor(String anchor) {
        this.anchor = anchor;
    }


    /**
     * The logical forward name from which to retrieve the hyperlink URI.
     */
    protected String forward = null;

    /**
     * @return .
     */
    public String getForward() {
        return (this.forward);
    }

    /**
     * @param forward .
     */
    public void setForward(String forward) {
        this.forward = forward;
    }


    /**
     * The hyperlink URI.
     */
    protected String href = null;

    /**
     * @return .
     */
    public String getHref() {
        return (this.href);
    }

    /**
     * @param href .
     */
    public void setHref(String href) {
        this.href = href;
    }


    /**
     * The link name for named links.
     */
    protected String linkName = null;

    /**
     * @return .
     */
    public String getLinkName() {
        return (this.linkName);
    }

    /**
     * @param linkName .
     */
    public void setLinkName(String linkName) {
        this.linkName = linkName;
    }


    /**
     * The message resources for this package.
     */
    protected static MessageResources messages =
            MessageResources.getMessageResources(Constants.Package + ".LocalStrings");


    /**
     * The JSP bean name for query parameters.
     */
    protected String name = null;

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

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


    /**
     * The module-relative page URL (beginning with a slash) to which
     * this hyperlink will be rendered.
     */
    protected String page = null;

    /**
     * @return .
     */
    public String getPage() {
        return (this.page);
    }

    /**
     * @param page .
     */
    public void setPage(String page) {
        this.page = page;
    }


    /**
     * The module-relative action (beginning with a slash) which will be
     * called by this link
     */
    protected String action = null;

    /**
     * @return .
     */
    public String getAction() {
        return (this.action);
    }

    /**
     * @param action .
     */
    public void setAction(String action) {
        this.action = action;
    }


    /**
     * The module prefix (beginning with a slash) which will be
     * used to find the action for this link.
     */
    protected String module = null;

    /**
     * @return .
     */
    public String getModule() {
        return (this.module);
    }

    /**
     * @param module .
     */
    public void setModule(String module) {
        this.module = module;
    }


    /**
     * The single-parameter request parameter name to generate.
     */
    protected String paramId = null;

    /**
     * @return .
     */
    public String getParamId() {
        return (this.paramId);
    }

    /**
     * @param paramId .
     */
    public void setParamId(String paramId) {
        this.paramId = paramId;
    }


    /**
     * The single-parameter JSP bean name.
     */
    protected String paramName = null;

    /**
     * @return .
     */
    public String getParamName() {
        return (this.paramName);
    }

    /**
     * @param paramName .
     */
    public void setParamName(String paramName) {
        this.paramName = paramName;
    }


    /**
     * The single-parameter JSP bean property.
     */
    protected String paramProperty = null;

    /**
     * @return .
     */
    public String getParamProperty() {
        return (this.paramProperty);
    }

    /**
     * @param paramProperty .
     */
    public void setParamProperty(String paramProperty) {
        this.paramProperty = paramProperty;
    }


    /**
     * The single-parameter JSP bean scope.
     */
    protected String paramScope = null;

    /**
     * @return .
     */
    public String getParamScope() {
        return (this.paramScope);
    }

    /**
     * @param paramScope .
     */
    public void setParamScope(String paramScope) {
        this.paramScope = paramScope;
    }


    /**
     * The JSP bean property name for query parameters.
     */
    protected String property = null;

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

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


    /**
     * The scope of the bean specified by the name property, if any.
     */
    protected String scope = null;

    /**
     * @return .
     */
    public String getScope() {
        return (this.scope);
    }

    /**
     * @param scope 。
     */
    public void setScope(String scope) {
        this.scope = scope;
    }


    /**
     * The window target.
     */
    protected String target = null;

    /**
     * @return ./
     */
    public String getTarget() {
        return (this.target);
    }

    /**
     * @param target .
     */
    public void setTarget(String target) {
        this.target = target;
    }


    /**
     * Include transaction token (if any) in the hyperlink?
     */
    protected boolean transaction = false;

    /**
     * @return /
     */
    public boolean getTransaction() {
        return (this.transaction);
    }

    /**
     * @param transaction /
     */
    public void setTransaction(boolean transaction) {
        this.transaction = transaction;
    }

    /**
     * Name of parameter to generate to hold index number
     */
    protected String indexId = null;

    /**
     * @return .
     */
    public String getIndexId() {
        return (this.indexId);
    }

    /**
     * @param indexId /
     */
    public void setIndexId(String indexId) {
        this.indexId = indexId;
    }

    protected boolean useLocalEncoding = false;

    /**
     * @return /
     */
    public boolean isUseLocalEncoding() {
        return useLocalEncoding;
    }

    /**
     * @param b /
     */
    public void setUseLocalEncoding(boolean b) {
        useLocalEncoding = b;
    }

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


    /**
     * Render the beginning of the hyperlink.
     * <p/>
     * Support for indexed property since Struts 1.1
     *
     * @return .
     * @throws javax.servlet.jsp.JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {
        clear();
        // Generate the opening anchor element
        StringBuffer results = new StringBuffer("<a");

        // Special case for name anchors
        if (linkName != null) {
            results.append(" name=\"");
            results.append(linkName);
            results.append("\"");
        }
        // * @since Struts 1.1
        if (linkName == null || forward != null || href != null ||
                page != null || action != null) {
            results.append(" href=\"");
            results.append(calculateURL());
            results.append("\"");
        }
        if (target != null) {
            results.append(" target=\"");
            results.append(target);
            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(prepareStyles());
        results.append(prepareEventHandlers());
        results.append(">");
        if (condition())
            TagUtils.getInstance().write(pageContext, results.toString());

        // Evaluate the body of this tag
        this.text = null;
        return (EVAL_BODY_TAG);

    }


    /**
     * Save the associated label from the body content.
     *
     * @return .
     * @throws JspException if a JSP exception has occurred
     */
    public int doAfterBody() throws JspException {

        if (bodyContent != null) {
            String value = bodyContent.getString().trim();
            if (value.length() > 0)
                text = value;
        }
        return (SKIP_BODY);

    }


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

        // Prepare the textual content and ending element of this hyperlink
        StringBuffer results = new StringBuffer();
        if (text != null) {
            results.append(text);
        }
        results.append("</a>");
        if (condition())
            TagUtils.getInstance().write(pageContext, results.toString());

        return (EVAL_PAGE);

    }


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

        super.release();
        anchor = null;
        forward = null;
        href = null;
        linkName = null;
        name = null;
        page = null;
        action = null;
        paramId = null;
        paramName = null;
        paramProperty = null;
        paramScope = null;
        property = null;
        scope = null;
        target = null;
        text = null;
        transaction = false;

    }

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


    /**
     * Return the complete URL to which this hyperlink will direct the user.
     * Support for indexed property since Struts 1.1
     *
     * @return .
     * @throws JspException if an exception is thrown calculating the value
     */
    protected String calculateURL() throws JspException {

        // Identify the parameters we will add to the completed URL
        Map params = TagUtils.getInstance().computeParameters(pageContext, paramId, paramName, paramProperty, paramScope,
                name, property, scope, transaction);

        // if "indexed=true", add "index=x" parameter to query string
        // * @since Struts 1.1
        if (indexed) {

            // look for outer iterate tag
            IterateTag iterateTag =
                    (IterateTag) findAncestorWithClass(this, IterateTag.class);
            if (iterateTag == null) {
                // This tag should only be nested in an iterate tag
                // If it's not, throw exception
                JspException e = new JspException(messages.getMessage("indexed.noEnclosingIterate"));
                TagUtils.getInstance().saveException(pageContext, e);
                throw e;
            }

            //calculate index, and add as a parameter
            if (params == null) {
                params = new HashMap();             //create new HashMap if no other params
            }
            if (indexId != null) {
                params.put(indexId, Integer.toString(iterateTag.getIndex()));
            }
            else {
                params.put("index", Integer.toString(iterateTag.getIndex()));
            }
        }

        String url;
        try {
            url = TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, href,
                    page, action, module, params, anchor, false, useLocalEncoding);
        }
        catch (MalformedURLException e) {
            TagUtils.getInstance().saveException(pageContext, e);
            throw new JspException(messages.getMessage("rewrite.url", e.toString()));
        }
        return (url);

    }

}

⌨️ 快捷键说明

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