KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > portlet > servlet > WebWorkVelocityServlet


1 /*
2  * Copyright (c) 2005 Opensymphony. All Rights Reserved.
3  */

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 JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16 import javax.servlet.jsp.JspFactory JavaDoc;
17 import javax.servlet.jsp.PageContext JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.Writer JavaDoc;
20
21 /**
22  * When WebWorkPortlet doService() include the .vm resource, then
23  * WebWorkVelocityServlet was invoked.
24  * <p/>
25  * Now we can use WebWorkVelocityServlet to render the result velocity VM with
26  * the stackValue stored in ActionContext after WebWork Action was executed.
27  * <p/>
28  * Because PortletRequestDispatcher include() method in WebWorkPortlet
29  * doService() can not filtered by ServletFilter, we must add the SiteMesh
30  * function here if want SiteMesh decoration support.
31  * <p/>
32  *
33  * @author <a HREF="mailto:hu_pengfei@yahoo.com.cn"> Henry Hu </a>
34  * @since 2005-7-6
35  */

36 public class WebWorkVelocityServlet extends WebWorkAbstractServlet {
37
38     //~ Constructors
39
// ///////////////////////////////////////////////////////////
40

41     public WebWorkVelocityServlet() {
42         super();
43     }
44
45     //~ Methods
46
// ////////////////////////////////////////////////////////////////
47

48     protected void doRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
49
50         // save the old PageContext
51
PageContext JavaDoc oldPageContext = ServletActionContext.getPageContext();
52
53         // create a new PageContext
54
JspFactory JavaDoc jspFactory = JspFactory.getDefaultFactory();
55
56         PageContext JavaDoc pageContext = jspFactory.getPageContext(this, request, response, null, true, 8192, true);
57
58         // put the new PageContext into ActionContext
59
ActionContext actionContext = ActionContext.getContext();
60
61         actionContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
62
63         /*
64          * Must put the stack into HttpServletRequest, because the WebWork JSP
65          * Taglib will use it to judge whether there are some errors in stack.
66          */

67         OgnlValueStack stack = ActionContext.getContext().getValueStack();
68
69 //Moved to Parent class doFilter()
70
// request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY,stack);
71
// stack.getContext().put(ServletActionContext.HTTP_REQUEST, request);
72
// stack.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
73
// stack.getContext().put(ServletActionContext.SERVLET_CONTEXT, getServletContext());
74

75         //Moved to WebWorkPortlet doService()
76

77         VelocityManager velocityManager = VelocityManager.getInstance();
78         Context resultContext = velocityManager.createContext(stack, request, response);
79
80         response.setContentType("text/html");
81
82         try {
83             String JavaDoc location = (String JavaDoc) ActionContext.getContext().get("template");
84             Template template = velocityManager.getVelocityEngine().getTemplate(location);
85
86             Writer JavaDoc writer = pageContext.getOut();
87             template.merge(resultContext, writer);
88             writer.flush();
89         } catch (Exception JavaDoc e) {
90             e.printStackTrace();
91         } finally {
92             // perform cleanup
93
jspFactory.releasePageContext(pageContext);
94             actionContext.put(ServletActionContext.PAGE_CONTEXT, oldPageContext);
95         }
96
97     }
98
99 }
Popular Tags