<%@ taglib prefix="cache" uri="http://java.sun.com/jsp/jstl/core" %> <!-判断是否已经进行缓存--> <cache:if test="${empty cachedContext}"> <!-设置一个application变量保存需要进行缓存的内容--> <cache:set var="cachedContext" scope="application"> 这段内容被缓存! </cache:set> </cache:if> |
<%@ taglib prefix="cache" uri="http://java.sun.com/jsp/jstl/core" %> <!-把变化的内容设置到session变量中--> <cache:if test="${sessionScope.enterNum == null}"> <cache:set var="enterNum" scope="session" value="0"/> </cache:if> <cache:set var="enterNum" value="${enterNum +1}" scope="session"/> <cache:if test="${empty cachedStartContext1}"> <cache:set var="cachedStartContext1" scope="application"> 访问了本页面 </cache:set> </cache:if> <cache:if test="${empty cachedEndContext2}"> <cache:set var="cachedEndContext2" scope="application"> 次数。 </cache:set> |
public class JspUtils { public static Object eval( String expr, Class type, JspContext jspContext) throws JspException { try { if (expr.indexOf("${"} == -1) {return expr; } //没有含有JSP表达式则不做处理。 ExpressionEvaluator evaluator = jspContext.getExpressionEvaluator(); //获取 ExpressionEvaluator以便运算JSP表达式 return evaluator.evaluate(expr, type, jspContext.getVariableResolver(), null);//求出JSP表达式的值 } catch (ELException e) { throw new JspException(e); } } //转换字符串为SCOPE的值 public static int checkScope(String scope) { if ("page".equalsIgnoreCase(scope)) return PageContext.PAGE_SCOPE; else if ("request".equalsIgnoreCase(scope)) return PageContext.REQUEST_SCOPE; else if ("session".equalsIgnoreCase(scope)) return PageContext.SESSION_SCOPE; else if ("application".equalsIgnoreCase(scope)) return PageContext.APPLICATION_SCOPE; else throw new IllegalArgumentException( "错误的SCOPE: " + scope); } } |
public class CacheTag extends SimpleTagSupport { public static final String CACHE_ENABLED = " com.javayou.jspcache.enabled";//WEB的初始变量,控制是否起用缓存功能 private String id; private int scope; //存储缓存的范围 private boolean cacheEnabled;//是否进行缓存 public void doTag() throws JspException, IOException { JspContext jspContext = getJspContext(); ServletContext scontext = ((PageContext) jspContext).getServletContext(); //取得初始变量,判断是否使用缓存功能 String needCache = scontext.getInitParameter(CACHE_ENABLED); if(needCache != null && needCache.equals("true")) { String cachedOut = (String) jspContext.getAttribute(id, scope); //判断是否已经Cache过。 if (cachedOut == null) { StringWriter buffer = new StringWriter(); getJspBody().invoke(buffer); //取得标签的BODY处理内容。 cachedOut = buffer.toString(); jspContext.setAttribute(id, cachedOut, scope);//设置到缓存中 } //计算JSP表达式,得到最终输出。 String evaluatedOutput = (String) JspUtils.eval( cachedOutput, String.class, jspContext); jspContext.getOut().print(evaluatedOutput); jspContext.getOut().print(evaluatedOutput); } else getJspBody().invoke(null); } |
public class DynamicTag extends SimpleTagSupport { private String expr;//设置JSP表达式 public void setExpr(String expr) { this.expr = expr; } public void doTag() throws JspException, IOException { String output = "${" + expr + "}"; CacheTag ancestor = (CacheTag) findAncestorWithClass( this, CacheTag.class);//得到父标签 CacheTag if (ancestor == null || !ancestor.isCacheEnabled()) output = (String) JspUtils.eval( output, String.class, getJspContext()); //求出JSP表达式的值 getJspContext().getOut().print(output); } } |
<tag> <name>cache</name> <tag-class>com.javayou.jspcache.CacheTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>id</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>scope</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <tag> <name>dynamic</name> <tag-class>com.javayou.jspcache.DynamicTag</tag-class> <body-content>empty</body-content> <attribute> <name>expr</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> |
<context-param> <param-name>com.javayou.jspcache.enabled</param-name> <param-value>true</param-value> </context-param> <jsp-config> <taglib> <taglib-uri>http://javayou.com/jspcache<;/taglib-uri> <taglib-location>/WEB-INF/jspcache.tld</taglib-location> </taglib> </jsp-config> |
<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="jc" uri=" http://javayou.com/jspcache" %> <core:set var="counter" value="${counter+1}" scope="session"/> 访问了本页面 <jc:dynamic expr="sessionScope.counter"/> 次! </jc:cache> |
欢迎光临 纳速健身 (https://nasue.com/) | Powered by Discuz! X3.4 |