1 7 8 package com.opensymphony.module.sitemesh.freemarker; 9 10 import java.io.IOException ; 11 import java.io.StringWriter ; 12 13 import javax.servlet.ServletException ; 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletResponse ; 16 17 import com.opensymphony.module.sitemesh.HTMLPage; 18 import com.opensymphony.module.sitemesh.RequestConstants; 19 20 import freemarker.ext.servlet.FreemarkerServlet; 21 import freemarker.template.SimpleHash; 22 import freemarker.template.Template; 23 import freemarker.template.TemplateModel; 24 25 31 public class FreemarkerDecoratorServlet extends FreemarkerServlet 32 { 33 36 protected boolean preTemplateProcess( 37 HttpServletRequest request, 38 HttpServletResponse response, 39 Template template, 40 TemplateModel templateModel) 41 throws ServletException , IOException 42 { 43 boolean result=super.preTemplateProcess(request, response, template, templateModel); 44 45 SimpleHash hash = (SimpleHash) templateModel; 46 47 HTMLPage htmlPage = (HTMLPage) request.getAttribute(RequestConstants.PAGE); 48 49 String title, body, head; 50 51 if(htmlPage==null) 52 { 53 title="No Title"; 54 body="No Body"; 55 head="<!-- No head -->"; 56 } 57 else 58 { 59 title=htmlPage.getTitle(); 60 61 StringWriter buffer = new StringWriter (); 62 htmlPage.writeBody(buffer); 63 body=buffer.toString(); 64 65 buffer = new StringWriter (); 66 htmlPage.writeHead(buffer); 67 head=buffer.toString(); 68 69 hash.put("page",htmlPage); 70 } 71 72 hash.put("title",title); 73 hash.put("body",body); 74 hash.put("head",head); 75 hash.put("base",request.getContextPath()); 76 77 82 83 return result; 84 } 85 } 86 | Popular Tags |