KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > view > ViewResolverHelper


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) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.view;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33
34 import org.springframework.beans.factory.BeanFactoryUtils;
35 import org.springframework.beans.factory.ListableBeanFactory;
36 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
37 import org.springframework.core.OrderComparator;
38 import org.springframework.web.servlet.DispatcherServlet;
39 import org.springframework.web.servlet.ModelAndView;
40 import org.springframework.web.servlet.RequestToViewNameTranslator;
41 import org.springframework.web.servlet.View;
42 import org.springframework.web.servlet.ViewResolver;
43 import org.springframework.web.servlet.support.RequestContextUtils;
44 import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator;
45
46 /**
47  * Helper class that resolves views the same way as a {@link DispatcherServlet}.
48  */

49 public class ViewResolverHelper {
50
51     private ArrayList JavaDoc viewResolvers;
52
53     private RequestToViewNameTranslator viewNameTranslator;
54
55     public ViewResolverHelper(ListableBeanFactory beanFactory) {
56         Map JavaDoc matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
57                 beanFactory, ViewResolver.class, true, false);
58
59         if (!matchingBeans.isEmpty()) {
60             this.viewResolvers = new ArrayList JavaDoc(matchingBeans.values());
61             Collections.sort(this.viewResolvers, new OrderComparator());
62         }
63
64         try {
65             this.viewNameTranslator = (RequestToViewNameTranslator) beanFactory.getBean(
66                     DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME,
67                     RequestToViewNameTranslator.class);
68         }
69         catch (NoSuchBeanDefinitionException ex) {
70             this.viewNameTranslator = new DefaultRequestToViewNameTranslator();
71         }
72     }
73
74     public View resolveView(HttpServletRequest JavaDoc request, ModelAndView mv)
75             throws ViewResolutionException {
76
77         if (mv.hasView() && !mv.isReference()) {
78             return mv.getView();
79         }
80         return resolveView(request, mv.getViewName());
81     }
82
83     public View resolveView(HttpServletRequest JavaDoc request, String JavaDoc viewName)
84             throws ViewResolutionException {
85
86         try {
87             if (viewName == null) {
88                 viewName = viewNameTranslator.getViewName(request);
89             }
90             Iterator JavaDoc i = viewResolvers.iterator();
91             while (i.hasNext()) {
92                 ViewResolver viewResolver = (ViewResolver) i.next();
93                 Locale JavaDoc locale = RequestContextUtils.getLocale(request);
94                 View view = viewResolver.resolveViewName(viewName, locale);
95                 if (view != null) {
96                     return view;
97                 }
98             }
99         }
100         catch (Exception JavaDoc e) {
101             throw new ViewResolutionException(viewName, e);
102         }
103         throw new ViewResolutionException(viewName);
104     }
105
106 }
107
Popular Tags