使用JSP自定义标签做页面片段的缓存

直接上代码:

01 public class IncludeTag extends BodyTagSupport {
02  
03     private static final long serialVersionUID = 3048295381656105593L;
04     private final String CACHENAME = "TAG_CACHE";
05     private String content;
06  
07     @Override
08     public int doStartTag() throws JspException {
09         content = (String)CacheHelper.get(CACHENAME);
10         if(content != null) {   //尝试从缓存中读取页面片段
11             return SKIP_BODY;
12         }
13         return super.doStartTag();
14     }  
15      
16     @Override
17     public void doInitBody() throws JspException {
18         //Object obj = CacheHelper.get(...);
19         //pageContext.getRequest().setAttribute(key, obj);
20         pageContext.getRequest().setAttribute("key""value");  //这里可以从缓存中读取相关数据放入request中供页面片段使用
21         List<String> lst = new ArrayList<String>();
22         lst.add("item 1");
23         lst.add("item 2");
24         pageContext.getRequest().setAttribute("list", lst);
25         super.doInitBody();
26     }
27  
28     @Override
29     public int doAfterBody() throws JspException {
30         return super.doAfterBody();
31     }
32  
33     @Override
34     public int doEndTag() throws JspException {
35         if(content == null){
36             content = bodyContent.getString().trim();
37             if(content.length() != 0 || !content.equalsIgnoreCase("null")) {
38                 CacheHelper.set(CACHENAME, content);    //设置页面片段到缓存中
39             }
40         }
41          
42         try {
43             pageContext.getResponse().getWriter().print(content);   //输出页面片段内容到页面上
44         catch (IOException e) {
45             e.printStackTrace();
46         }
47          
48         return super.doEndTag();
49     }
50 }

调用自定义标签的页面:

01 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
02 <%@ taglib prefix="my" uri="/WEB-INF/my.tld" %>
03 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04 <html>
05 <head>
06 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
07 <title>test</title>
08 </head><p><body>
09     <my:include>
10         <%@ include file="include.jsp" %>
11     </my:include>
12 </body></p>
13 </html>
module.jsp页面的内容:

01 <%@ page import="java.util.*" %>
02 <%=request.getAttribute("key") %>
03 <div>
04   <ul>
05   <%
06     List lst = (List)request.getAttribute("list");
07     for(int i=0; i<lst.size(); i++) {
08   %>
09     <li><%=lst.get(i) %></li>
10   <%
11     }
12   %>
13   </ul>
14 </div>

自定义标签继承自BodyTagSupport,主要是为了获取标签内部生成的页面代码片段。

每个具体的页面都由很多个模块组成,模块就是类似module.jsp的页面,数据通过自定义标签填充

自定义标签的过程:

1. doStartTag到缓存中去取页面片段的代码是否存在,如果存在则直接跳过标签内容的解析;

2. doInitBody通过service层取数据放置到request中供页面片段使用;

3. doEndTag,如果内容为空,则生成页面内容片段,放到缓存中;输出到页面;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值