使用JSP自定义标签做页面片段的缓存
2012-10-15 09:25
701 查看
直接上代码:
调用自定义标签的页面:
module.jsp页面的内容:
自定义标签继承自BodyTagSupport,主要是为了获取标签内部生成的页面代码片段。
每个具体的页面都由很多个模块组成,模块就是类似module.jsp的页面,数据通过自定义标签填充
自定义标签的过程:
1. doStartTag到缓存中去取页面片段的代码是否存在,如果存在则直接跳过标签内容的解析;
2. doInitBody通过service层取数据放置到request中供页面片段使用;
3.
doEndTag,如果内容为空,则生成页面内容片段,放到缓存中;输出到页面;
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, //设置页面片段到缓存中 |
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 > |
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 > |
每个具体的页面都由很多个模块组成,模块就是类似module.jsp的页面,数据通过自定义标签填充
自定义标签的过程:
1. doStartTag到缓存中去取页面片段的代码是否存在,如果存在则直接跳过标签内容的解析;
2. doInitBody通过service层取数据放置到request中供页面片段使用;
3.
doEndTag,如果内容为空,则生成页面内容片段,放到缓存中;输出到页面;
相关文章推荐
- 使用JSP自定义标签做页面片段的缓存
- 页面片段缓存----使用esi标签
- jsp页面使用jstl标签格式化String类型日期
- jsp版本2.1 servlet版本2.5 el表达式、jstl标签学习以及自定义标签(用标签实现业务逻辑和jsp页面的分离)
- jsp页面中使用JSTL标签库的<c:redirect>的一些看法和疑问?
- 看jstl标签做个类似 的 自定义jsp 页面标签
- JSP中如何使用自定义的标签库
- jsp页面动态展示list-使用<select>和<c:forEach>标签
- 在JSP页面中使用标签判断字符串长度的问题
- jsp自定义标签使用
- jsp页面使用struts标签报argument type mismatch异常
- 使用BodyTagSupport类实现Jsp自定义标签
- 基于JSP 自定义标签使用实例介绍
- 关于打包自定义标签,并导入项目使用时,出现java.lang.NoClassDefFoundError: JspException异常,解决方法和注意事项!
- 关于jsp页面自定义jstl标签简单项目配置
- 使用SimpleTag支持自定义JSP标签
- MAVEN项目中JSP页面使用JSTL标签
- 如何在JSP页面中使用JSTL标签实现if和if-else判断,判断条件使用<%=value%>
- Jsp页面图片img标签使用绝对路径进行显示
- JSP自定义标签使用