1 52 53 package freemarker.ext.jsp; 54 55 import java.io.IOException ; 56 57 import javax.servlet.ServletException ; 58 import javax.servlet.jsp.JspException ; 59 import javax.servlet.jsp.PageContext ; 60 import javax.servlet.jsp.tagext.BodyContent ; 61 import javax.servlet.jsp.tagext.BodyTag ; 62 import javax.servlet.jsp.tagext.Tag ; 63 64 import freemarker.template.SimpleHash; 65 import freemarker.template.Template; 66 67 73 public class FreemarkerTag implements BodyTag 74 { 75 private Tag parent; 76 private BodyContent bodyContent; 77 private PageContext pageContext; 78 private SimpleHash root; 79 private Template template; 80 private boolean caching = true; 81 private String name = ""; 82 83 public boolean getCaching() 84 { 85 return caching; 86 } 87 88 public void setCaching(boolean caching) 89 { 90 this.caching = caching; 91 } 92 93 public void setName(String name) 94 { 95 this.name = name == null ? "" : name; 96 } 97 98 public Tag getParent() 99 { 100 return parent; 101 } 102 103 public void setParent(Tag parent) 104 { 105 this.parent = parent; 106 } 107 108 public int doStartTag() 109 { 110 return EVAL_BODY_BUFFERED; 111 } 112 113 public void setBodyContent(BodyContent bodyContent) 114 { 115 this.bodyContent = bodyContent; 116 } 117 118 public void setPageContext(PageContext pageContext) 119 { 120 this.pageContext = pageContext; 121 root = null; 122 } 123 124 public void doInitBody() 125 { 126 } 127 128 public int doAfterBody() 129 { 130 return SKIP_BODY; 131 } 132 133 public void release() 134 { 135 root = null; 136 template = null; 137 name = ""; 138 } 139 140 public int doEndTag() 141 throws JspException 142 { 143 if (bodyContent == null) 144 return EVAL_PAGE; 145 146 try 147 { 148 if(template == null) 149 { 150 template = new Template(name, bodyContent.getReader()); 151 } 152 153 if(root == null) 154 { 155 root = new SimpleHash(); 156 root.put("page", new JspContextModel(pageContext, JspContextModel.PAGE_SCOPE)); 157 root.put("request", new JspContextModel(pageContext, JspContextModel.REQUEST_SCOPE)); 158 root.put("session", new JspContextModel(pageContext, JspContextModel.SESSION_SCOPE)); 159 root.put("application", new JspContextModel(pageContext, JspContextModel.APPLICATION_SCOPE)); 160 root.put("any", new JspContextModel(pageContext, JspContextModel.ANY_SCOPE)); 161 } 162 template.process(root, pageContext.getOut()); 163 } 164 catch(Exception e) 165 { 166 try 167 { 168 pageContext.handlePageException(e); 169 } 170 catch(ServletException e2) 171 { 172 throw new JspException (e2.getMessage()); 173 } 174 catch(IOException e2) 175 { 176 throw new JspException (e2.getMessage()); 177 } 178 } 179 finally 180 { 181 if(!caching) 182 { 183 template = null; 184 } 185 } 186 187 return EVAL_PAGE; 188 } 189 } 190 | Popular Tags |