KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > TemplatedMVCHandler


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.module.admininterface;
14
15 import info.magnolia.cms.util.FreeMarkerUtil;
16
17 import java.io.IOException JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25
26 /**
27  * A default page (hander) using a freemarker template to render. The default templat name is following the class name.
28  * You can overwrite the getTemplateName() method wich can return a template per view.
29  * @author Philipp Bracher
30  * @version $Revision: 6341 $ ($Author: philipp $)
31  */

32 public class TemplatedMVCHandler extends PageMVCHandler {
33
34     /**
35      * @param name
36      * @param request
37      * @param response
38      */

39     public TemplatedMVCHandler(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
40         super(name, request, response);
41     }
42
43     /**
44      * Returns the template name for the rendering. The standard implementation used the classname to build the path.
45      * @param viewName the view name to render
46      * @return the template name (including path)
47      */

48     protected String JavaDoc getTemplateName(String JavaDoc viewName) {
49         return FreeMarkerUtil.createTemplateName(this.getClass(), "html");
50     }
51
52     /**
53      * Renders the template. The handlers is passed with the name 'this'.
54      */

55     public void renderHtml(String JavaDoc view) throws IOException JavaDoc {
56
57         String JavaDoc template = this.getTemplateName(view);
58         if (template != null) {
59
60             Map JavaDoc data = new HashMap JavaDoc();
61             data.put("this", this);
62
63             PrintWriter JavaDoc writer;
64
65             try {
66                 writer = getResponse().getWriter();
67             }
68             catch (IllegalStateException JavaDoc e) {
69                 // getResponse().getOutputStream() has already been called
70
writer = new PrintWriter JavaDoc(getResponse().getOutputStream());
71             }
72             FreeMarkerUtil.process(template, data, writer);
73         }
74     }
75
76 }
77
Popular Tags