1 9 10 package com.opensymphony.module.sitemesh.velocity; 11 12 import com.opensymphony.module.sitemesh.*; 13 import com.opensymphony.module.sitemesh.util.OutputConverter; 14 15 import org.apache.velocity.Template; 16 import org.apache.velocity.context.Context; 17 import org.apache.velocity.tools.view.servlet.VelocityViewServlet; 18 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import java.io.StringWriter ; 22 23 29 public class VelocityDecoratorServlet extends VelocityViewServlet { 30 public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) throws Exception { 31 HTMLPage htmlPage = (HTMLPage) request.getAttribute(RequestConstants.PAGE); 32 String template; 33 34 context.put("base", request.getContextPath()); 35 36 context.put("req", request); 39 context.put("res", response); 40 41 if (htmlPage == null) { 42 context.put("title", "Title?"); 43 context.put("body", "<p>Body?</p>"); 44 context.put("head", "<!-- head -->"); 45 template = request.getServletPath(); 46 } 47 else { 48 context.put("title", OutputConverter.convert(htmlPage.getTitle())); 49 { 50 StringWriter buffer = new StringWriter (); 51 htmlPage.writeBody(OutputConverter.getWriter(buffer)); 52 context.put("body", buffer.toString()); 53 } 54 { 55 StringWriter buffer = new StringWriter (); 56 htmlPage.writeHead(OutputConverter.getWriter(buffer)); 57 context.put("head", buffer.toString()); 58 } 59 context.put("page", htmlPage); 60 Factory factory = Factory.getInstance(new Config(getServletConfig())); 61 Decorator decorator = factory.getDecoratorMapper().getDecorator(request, htmlPage); 62 template = decorator.getPage(); 63 } 64 65 return getTemplate(template); 66 } 67 } | Popular Tags |