1 5 package com.opensymphony.webwork.views.velocity; 6 7 import com.opensymphony.webwork.ServletActionContext; 8 import com.opensymphony.webwork.config.Configuration; 9 import com.opensymphony.webwork.views.util.ContextUtil; 10 import com.opensymphony.xwork.ActionContext; 11 import org.apache.velocity.Template; 12 import org.apache.velocity.context.Context; 13 import org.apache.velocity.exception.MethodInvocationException; 14 import org.apache.velocity.exception.ParseErrorException; 15 import org.apache.velocity.exception.ResourceNotFoundException; 16 import org.apache.velocity.runtime.RuntimeSingleton; 17 import org.apache.velocity.servlet.VelocityServlet; 18 19 import javax.servlet.ServletConfig ; 20 import javax.servlet.ServletException ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 import javax.servlet.jsp.JspFactory ; 24 import javax.servlet.jsp.PageContext ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.UnsupportedEncodingException ; 28 import java.io.Writer ; 29 import java.util.Properties ; 30 31 32 37 public class WebWorkVelocityServlet extends VelocityServlet { 38 private VelocityManager velocityManager; 39 40 public WebWorkVelocityServlet() { 41 velocityManager = VelocityManager.getInstance(); 42 } 43 44 public void init(ServletConfig servletConfig) throws ServletException { 45 super.init(servletConfig); 46 47 velocityManager.init(servletConfig.getServletContext()); 49 } 50 51 protected Context createContext(HttpServletRequest request, HttpServletResponse response) { 52 return velocityManager.createContext(ActionContext.getContext().getValueStack(), request, response); 53 } 54 55 protected Template handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Context context) throws Exception { 56 String servletPath = (String ) httpServletRequest.getAttribute("javax.servlet.include.servlet_path"); 57 58 if (servletPath == null) { 59 servletPath = httpServletRequest.getServletPath(); 60 } 61 62 return getTemplate(servletPath, getEncoding()); 63 } 64 65 78 protected Properties loadConfiguration(ServletConfig servletConfig) throws IOException , FileNotFoundException { 79 return velocityManager.loadConfiguration(servletConfig.getServletContext()); 80 } 81 82 87 protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException , UnsupportedEncodingException , Exception { 88 PageContext oldPageContext = ServletActionContext.getPageContext(); 90 91 JspFactory jspFactory = JspFactory.getDefaultFactory(); 93 HttpServletRequest request = (HttpServletRequest ) context.get(ContextUtil.REQUEST); 94 PageContext pageContext = jspFactory.getPageContext(this, request, response, null, true, 8192, true); 95 96 ActionContext actionContext = ActionContext.getContext(); 98 actionContext.put(ServletActionContext.PAGE_CONTEXT, pageContext); 99 100 try { 101 Writer writer = pageContext.getOut(); 102 template.merge(context, writer); 103 writer.flush(); 104 } finally { 105 jspFactory.releasePageContext(pageContext); 107 actionContext.put(ServletActionContext.PAGE_CONTEXT, oldPageContext); 108 } 109 } 110 111 private String getEncoding() { 112 try { 114 return Configuration.getString("webwork.i18n.encoding"); 115 } catch (IllegalArgumentException e) { 116 return RuntimeSingleton.getString(RuntimeSingleton.OUTPUT_ENCODING, DEFAULT_OUTPUT_ENCODING); 117 } 118 } 119 } 120 | Popular Tags |