1 4 package com.opensymphony.webwork.portlet.servlet; 5 6 import com.opensymphony.webwork.ServletActionContext; 7 import com.opensymphony.webwork.portlet.velocity.VelocityManager; 8 import com.opensymphony.xwork.ActionContext; 9 import com.opensymphony.xwork.util.OgnlValueStack; 10 import org.apache.velocity.Template; 11 import org.apache.velocity.context.Context; 12 13 import javax.servlet.ServletException ; 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletResponse ; 16 import javax.servlet.jsp.JspFactory ; 17 import javax.servlet.jsp.PageContext ; 18 import java.io.IOException ; 19 import java.io.Writer ; 20 21 36 public class WebWorkVelocityServlet extends WebWorkAbstractServlet { 37 38 41 public WebWorkVelocityServlet() { 42 super(); 43 } 44 45 48 protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 49 50 PageContext oldPageContext = ServletActionContext.getPageContext(); 52 53 JspFactory jspFactory = JspFactory.getDefaultFactory(); 55 56 PageContext pageContext = jspFactory.getPageContext(this, request, response, null, true, 8192, true); 57 58 ActionContext actionContext = ActionContext.getContext(); 60 61 actionContext.put(ServletActionContext.PAGE_CONTEXT, pageContext); 62 63 67 OgnlValueStack stack = ActionContext.getContext().getValueStack(); 68 69 75 77 VelocityManager velocityManager = VelocityManager.getInstance(); 78 Context resultContext = velocityManager.createContext(stack, request, response); 79 80 response.setContentType("text/html"); 81 82 try { 83 String location = (String ) ActionContext.getContext().get("template"); 84 Template template = velocityManager.getVelocityEngine().getTemplate(location); 85 86 Writer writer = pageContext.getOut(); 87 template.merge(resultContext, writer); 88 writer.flush(); 89 } catch (Exception e) { 90 e.printStackTrace(); 91 } finally { 92 jspFactory.releasePageContext(pageContext); 94 actionContext.put(ServletActionContext.PAGE_CONTEXT, oldPageContext); 95 } 96 97 } 98 99 } | Popular Tags |