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

📄 includetag.java

📁 Tree taglib,生成树的标签库
💻 JAVA
字号:
/*
    Copyright 2004 Jenkov Development

    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 com.jenkov.prizetags.template.impl;

import com.jenkov.prizetags.base.BaseTag;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.jsp.JspException;
import java.io.IOException;

/**
 * @author Jakob Jenkov
 *         Copyright 2004 Jenkov Development
 */
public class IncludeTag extends BaseTag{
    protected String templateParam = TemplateConstants.DEFAULT_TEMPLATE_PARAM;
    protected String resource      = null;
    protected String uri           = null;
    protected String content       = null;
    protected String optional      = null;


    public String getTemplateParam() {
        return templateParam;
    }

    public void setTemplateParam(String templateParam) {
        this.templateParam = templateParam;
    }

    public String getResource() {
        return resource;
    }

    public void setResource(String resource) {
        this.resource = resource;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getOptional() {
        return optional;
    }

    public void setOptional(String optional) {
        this.optional = optional;
    }


    public int doStartTag() throws JspException {
        if(getResource() == null && getUri() == null && getContent() == null){
            throw new JspException("You must specify either a resource from a template, or a uri, or content to include");
        }

        if(getResource() != null){
            includeResource();
        } else if(getUri() != null) {
            includeUri();
        } else {
            includeContent();
        }

        return SKIP_BODY;
    }

    private void includeContent() throws JspException{
        String templateName = getTemplateName();
        Template template = getTemplate(templateName);

        String content = (String) template.getInclude(getContent());

        try {
            if(content != null){
                pageContext.getOut().print(content);
            } else if(!isOptional()){
                throw new JspException("Content " + getContent() + " not found in template " + getTemplateName());
            }
        } catch (IOException e) {
            throw new JspException("Error including content: " + getContent(), e);
        }

    }

    private void includeUri() throws JspException{
        RequestDispatcher dispatcher = pageContext.getRequest().getRequestDispatcher(getUri());
        if(dispatcher == null){
            throw new JspException("The content " + getUri() + " could not be found");
        }
        try {
            pageContext.getOut().flush();
            dispatcher.include(pageContext.getRequest(), pageContext.getResponse());
        } catch (ServletException e) {
            throw new JspException("Error including content: " + getUri(), e);
        } catch (IOException e) {
            throw new JspException("Error including content: " + getUri(), e);
        }
    }

    private void includeResource() throws JspException {
        Template template = getTemplate(getTemplateName());

        debug("include resource start");
        try {
            RequestDispatcher dispatcher = null;
            debug("finding resource: " + getResource());
            String resource = template.getInclude(getResource());

            debug("found resouce: " + resource);

            if(resource != null){
                dispatcher = pageContext.getRequest().getRequestDispatcher(resource);
            } else if(! isOptional()){
                String text = "Resource " + getResource() + " not found in template " + getTemplateName();
                debug(text);
                throw new JspException(text);
            }

            debug("dispatcher: " + dispatcher);
            if(dispatcher != null){
                debug("including resource: " + getResource());
                pageContext.getOut().flush();
                dispatcher.include(pageContext.getRequest(), pageContext.getResponse());
                debug("including resource: " + getResource() + " DONE !");
            } else if(!isOptional()){
                throw new JspException("No dispatcher for resource: " + getResource()
                        + " (" + template.getInclude(getResource()) + ")");
            }
        } catch (ServletException e) {
            throw new JspException("Error including resource: " + getResource() + " from template: " + getTemplateName(), e);
        } catch (IOException e) {
            throw new JspException("Error including resource: " + getResource() + " from template: " + getTemplateName(), e);
        }
    }

    private Template getTemplate(String templateName) throws JspException {
        Template template = (Template) pageContext.getRequest().getAttribute(templateName);
        if(template == null) {
            throw new JspException("No template found in request attribute '" +
                templateName + "'");
        }
        return template;
    }

    private String getTemplateName() throws JspException {
        String templateName = pageContext.getRequest().getParameter(getTemplateParam());
        if(templateName == null){
            templateName = (String) pageContext.getRequest().getAttribute(getTemplateParam());
        }
        if(templateName == null){
            throw new JspException("No template name found in request parameter or attribute '" +
                getTemplateParam() + "'");
        }
        return templateName;
    }

    private boolean isOptional(){
        return "true".equals(getOptional());
    }

}

⌨️ 快捷键说明

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