1 5 8 package com.opensymphony.webwork.views.freemarker; 9 10 import com.opensymphony.webwork.ServletActionContext; 11 import com.opensymphony.webwork.views.util.ResourceUtil; 12 import com.opensymphony.webwork.dispatcher.WebWorkResultSupport; 13 import com.opensymphony.xwork.ActionInvocation; 14 import com.opensymphony.xwork.ActionContext; 15 import com.opensymphony.xwork.util.OgnlValueStack; 16 17 import freemarker.template.*; 18 19 import javax.servlet.ServletContext ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 import java.io.IOException ; 23 import java.io.Writer ; 24 import java.util.Locale ; 25 26 27 30 public class FreemarkerResult extends WebWorkResultSupport { 31 33 protected ActionInvocation invocation; 34 protected Configuration configuration; 35 protected ObjectWrapper wrapper; 36 37 42 protected String location; 43 private String pContentType = "text/html"; 44 45 47 public void setContentType(String aContentType) { 48 pContentType = aContentType; 49 } 50 51 55 public String getContentType() { 56 return pContentType; 57 } 58 59 68 public void doExecute(String location, ActionInvocation invocation) throws IOException , TemplateException { 69 this.location = location; 70 this.invocation = invocation; 71 this.configuration = getConfiguration(); 72 this.wrapper = getObjectWrapper(); 73 74 if (!location.startsWith("/")) { 75 ActionContext ctx = invocation.getInvocationContext(); 76 HttpServletRequest req = (HttpServletRequest ) ctx.get(ServletActionContext.HTTP_REQUEST); 77 String base = ResourceUtil.getResourceBase(req); 78 location = base + "/" + location; 79 } 80 81 Template template = configuration.getTemplate(location, deduceLocale()); 82 TemplateModel model = createModel(); 83 84 if (preTemplateProcess(template, model)) { 86 try { 87 template.process(model, getWriter()); 89 } finally { 90 postTemplateProcess(template, model); 92 } 93 } 94 } 95 96 106 protected Configuration getConfiguration() throws TemplateException { 107 return FreemarkerManager.getInstance().getConfigruation(ServletActionContext.getServletContext()); 108 } 109 110 119 protected ObjectWrapper getObjectWrapper() { 120 return configuration.getObjectWrapper(); 121 } 122 123 126 protected Writer getWriter() throws IOException { 127 return ServletActionContext.getResponse().getWriter(); 128 } 129 130 149 protected TemplateModel createModel() throws TemplateModelException { 150 ServletContext servletContext = ServletActionContext.getServletContext(); 151 HttpServletRequest request = ServletActionContext.getRequest(); 152 HttpServletResponse response = ServletActionContext.getResponse(); 153 OgnlValueStack stack = ServletActionContext.getContext().getValueStack(); 154 return FreemarkerManager.getInstance().buildTemplateModel(stack, invocation.getAction(), servletContext, request, response, wrapper); 155 } 156 157 163 protected Locale deduceLocale() { 164 return configuration.getLocale(); 165 } 166 167 170 protected void postTemplateProcess(Template template, TemplateModel data) throws IOException { 171 } 172 173 182 protected boolean preTemplateProcess(Template template, TemplateModel model) throws IOException { 183 Object attrContentType = template.getCustomAttribute("content_type"); 184 185 if (attrContentType != null) { 186 ServletActionContext.getResponse().setContentType(attrContentType.toString()); 187 } else { 188 String contentType = getContentType(); 189 190 if (contentType == null) { 191 contentType = "text/html"; 192 } 193 194 String encoding = template.getEncoding(); 195 196 if (encoding != null) { 197 contentType = contentType + "; charset=" + encoding; 198 } 199 200 ServletActionContext.getResponse().setContentType(contentType); 201 } 202 203 return true; 204 } 205 } 206 | Popular Tags |