KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > generic > GenericViewController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Carsten Woelk [cwoelk at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.website.generic;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.springframework.web.servlet.ModelAndView;
30 import org.springframework.web.servlet.mvc.AbstractController;
31 import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator;
32
33 /**
34  * <p>Trivial controller that allows you to set a content type for the view using
35  * an exposed configuration property. A view name also is not required to be set
36  * as if ommited the configured viewNameTranslator will translate the view. Per
37  * default Spring is using its {@link DefaultRequestToViewNameTranslator} for
38  * this purpose.</p>
39  *
40  * <p>If you are using FreeMarker as your View technology you might want to configure
41  * the viewNameTranslator with the suffix '.ftl' to work properly:
42  * <code>
43  * <bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator">
44  * <property name="suffix" value=".ftl" />
45  * </bean>
46  * </code>
47  * </p>
48  *
49  * @author Carsten Woelk [cwoelk at neteye dot de]
50  * @since 6.5
51  */

52 public class GenericViewController extends AbstractController {
53
54     private String JavaDoc viewName;
55
56     private String JavaDoc contentType;
57
58
59     /**
60      * Set the name of the view to delegate to
61      * @param viewName the name of the view to delegate to
62      */

63     public void setViewName(String JavaDoc viewName) {
64         this.viewName = viewName;
65     }
66
67     /**
68      * Returns the name of the view to delegate to
69      * @return the name of the view to delegate to
70      */

71     public String JavaDoc getViewName() {
72         return this.viewName;
73     }
74
75     /**
76      * Returns the content type to be set in the header
77      * @return the content type to be set in the header
78      */

79     public String JavaDoc getContentType() {
80         return this.contentType;
81     }
82
83     /**
84      * Sets the content type to be set in the header
85      * @param contentType to be set in the header
86      */

87     public void setContentType(String JavaDoc contentType) {
88         this.contentType = contentType;
89     }
90
91
92     /**
93      * If configured sets the content on the request and returns a {@link ModelAndView}
94      * object with the specified view name if configured. If none has been set
95      * Spring will use its configure viewNameTranslator to resolve the view name.
96      * @see #getViewName()
97      */

98     protected ModelAndView handleRequestInternal(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
99             throws Exception JavaDoc {
100
101         if (contentType != null) {
102             response.setContentType(contentType);
103         }
104
105         return new ModelAndView(getViewName());
106     }
107
108
109
110 }
111
Popular Tags