View Javadoc

1   /*
2    * Copyright 2007-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springbyexample.web.servlet.view.tiles2;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Locale;
22  import java.util.Map;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.tiles.Attribute;
30  import org.apache.tiles.AttributeContext;
31  import org.apache.tiles.Definition;
32  import org.apache.tiles.TilesContainer;
33  import org.apache.tiles.access.TilesAccess;
34  import org.apache.tiles.context.TilesRequestContext;
35  import org.apache.tiles.impl.BasicTilesContainer;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  import org.springframework.js.ajax.AjaxHandler;
39  import org.springframework.js.ajax.SpringJavascriptAjaxHandler;
40  import org.springframework.js.ajax.tiles2.AjaxTilesView;
41  import org.springframework.web.servlet.support.JstlUtils;
42  import org.springframework.web.servlet.support.RequestContext;
43  
44  /**
45   * <p>If the request isn't an AJAX request, <code>DynamicTilesView</code> 
46   * will handle the request.  Otherwise it is expected that an AJAX 
47   * view render will be next in the chain and can handle the request.</p>
48   *
49   * <p><strong>Note</strong>: Most code is copied from <code>AjaxTilesView</code> (giving author credit to original author).  
50   * If the method <code>flattenedAttributeMap</code> also processed temporary 
51   * Tiles context attributes, no changes to this class only <code>renderMergedOutputModel</code> 
52   * would need to be overridden.</p>
53   *
54   * @author Jeremy Grelle
55   * @author David Winterfeldt
56   */
57  public class AjaxDynamicTilesView extends AjaxTilesView {
58  
59      final Logger logger = LoggerFactory.getLogger(AjaxDynamicTilesView.class);
60          
61      final DynamicTilesViewProcessor dynamicTilesViewProcessor = new DynamicTilesViewProcessor();
62  	final AjaxHandler ajaxHandler = new SpringJavascriptAjaxHandler();
63  
64  	/**
65  	 * Renders output using Tiles.
66  	 */
67      protected void renderMergedOutputModel(Map model,
68  	                                       HttpServletRequest request, HttpServletResponse response)
69  	       throws Exception {
70  	    String beanName = getBeanName();
71  	    String url = getUrl();
72  
73          ServletContext servletContext = getServletContext();
74          TilesContainer container = TilesAccess.getContainer(servletContext);
75          if (container == null) {
76              throw new ServletException("Tiles container is not initialized. " + 
77                                         "Have you added a TilesConfigurer to your web application context?");
78          }
79  
80  	    if (ajaxHandler.isAjaxRequest(request, response)) {
81  	        // change URL used to lookup tiles template to correct definition
82  	        String definitionName = dynamicTilesViewProcessor.startDynamicDefinition(beanName, url, request, response, container);
83  	        
84  	        super.renderMergedOutputModel(model, request, response);
85  	        
86  	        dynamicTilesViewProcessor.endDynamicDefinition(definitionName, beanName, request, response, container);
87          } else {
88              exposeModelAsRequestAttributes(model, request);
89  
90              dynamicTilesViewProcessor.renderMergedOutputModel(beanName, url, 
91                                                                servletContext, request, response, container);
92          }
93      }
94  	
95      /**
96       * Check whether the underlying resource that the configured URL points to
97       * actually exists.
98       * @param locale the desired Locale that we're looking for
99       * @return <code>true</code> if the resource exists (or is assumed to exist);
100      * <code>false</code> if we know that it does not exist
101      * @throws Exception if the resource exists but is invalid (e.g. could not be parsed)
102      */
103 	@Override
104     public boolean checkResource(Locale locale) throws Exception {
105 	    // possibly could check if the Tiles template exists or 
106 	    // the file that will be the body of the dynamic context exists
107 	    
108         return true;
109     }
110     
111 }