cachetag.java

来自「jakarta-taglibs」· Java 代码 · 共 132 行

JAVA
132
字号
/*
 * Copyright 1999,2004 The Apache Software Foundation.
 * 
 * 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 org.apache.taglibs.cache;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

/**
 * <p>Caches a fragment of a JSP page.</p>
 *
 * @author Shawn Bayern
 */

public class CacheTag extends BodyTagSupport {

    //*********************************************************************
    // Private state

    private String nameExpr;				// tag attribute
    private String keyExpr;				// tag attribute

    private int scope;					// parsed tag attribute
    private String name, key;				// parsed tag attribute

    private LRUCache cache;				// cache
    private String cached;				// value from cache

    //*********************************************************************
    // Tag logic

    public int doStartTag() throws JspException {
      evaluateExpressions();
      cache = CacheUtil.getCache(scope, name, pageContext);
      cached = cache.get(key);
      if (cached != null)
        return SKIP_BODY;
      else
        return EVAL_BODY_BUFFERED;
    }

    public int doEndTag() throws JspException {
      try {
	String s = cached;
        if (s == null) {
          if (bodyContent == null || bodyContent.getString() == null)
	    s = "";
          else
            s = bodyContent.getString().trim();
          cache.put(key, s);
        }
        pageContext.getOut().write(s);
      } catch (java.io.IOException ex) {
        throw new JspException(ex);
      }
      return EVAL_PAGE;
    }

    //*********************************************************************
    // Attribute accessors

    public void setScope(String scope) {
      if (scope.equalsIgnoreCase("page"))
	this.scope = PageContext.PAGE_SCOPE;
      else if (scope.equalsIgnoreCase("request"))
        this.scope = PageContext.REQUEST_SCOPE;
      else if (scope.equalsIgnoreCase("session"))
        this.scope = PageContext.SESSION_SCOPE;
      else if (scope.equalsIgnoreCase("application"))
        this.scope = PageContext.APPLICATION_SCOPE;
      else
        throw new IllegalArgumentException("invalid scope");
    }

    public void setName(String nameExpr) {
      this.nameExpr = nameExpr;
    }

    public void setKey(String keyExpr) {
      this.keyExpr = keyExpr;
    }


    //*********************************************************************
    // Constructor and initialization

    public CacheTag() {
      super();
      init();
    }

    private void init() {
      scope = PageContext.APPLICATION_SCOPE;
      name = nameExpr = "";
      key = keyExpr = "";
    }
    

    //*********************************************************************
    // Private utility methods

    private void evaluateExpressions() throws JspException {
      name = (String) ExpressionEvaluatorManager.evaluate(
        "name",
        nameExpr,
        String.class,
        this,
        pageContext);
      key = (String) ExpressionEvaluatorManager.evaluate(
        "key",
        keyExpr,
        String.class,
        this,
        pageContext);
    }

}

⌨️ 快捷键说明

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