View Javadoc

1   /*
2    * Copyright 2002-2007 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 org.springframework.util.StringUtils;
20  import org.springframework.web.servlet.view.AbstractUrlBasedView;
21  import org.springframework.web.servlet.view.UrlBasedViewResolver;
22  
23  /**
24   * <p>Extends <code>UrlBasedViewResolver</code> and provides 
25   * some properties to set tiles values if the view is a 
26   * <code>DynamicTilesView</code>.</p>
27   *
28   * @author David Winterfeldt
29   */
30  public class TilesUrlBasedViewResolver extends UrlBasedViewResolver {
31  
32  	private String tilesDefinitionName = null;
33  	private String tilesBodyAttributeName = null;
34  	private String tilesDefinitionDelimiter = null;
35  
36  	/**
37  	 * Main template name.
38  	 */
39  	public void setTilesDefinitionName(String tilesDefinitionName) {
40  		this.tilesDefinitionName = tilesDefinitionName;
41  	}
42  
43  	/**
44  	 * Tiles body attribute name. 
45  	 */
46  	public void setTilesBodyAttributeName(String tilesBodyAttributeName) {
47  		this.tilesBodyAttributeName = tilesBodyAttributeName;
48  	}
49  
50  	/**
51  	 * Sets Tiles definition delimiter.  
52  	 */
53  	public void setTilesDefinitionDelimiter(String tilesDefinitionDelimiter) {
54  		this.tilesDefinitionDelimiter = tilesDefinitionDelimiter;
55  	}
56  
57  	/**
58  	 * Does everything the <code>UrlBasedViewResolver</code> does and 
59  	 * also sets some Tiles specific values on the view.
60  	 * 
61  	 * @param viewName the name of the view to build
62  	 * @return the View instance
63  	 * @throws Exception if the view couldn't be resolved
64  	 * @see #loadView(String, java.util.Locale)
65  	 */
66  	protected AbstractUrlBasedView buildView(String viewName) throws Exception {
67  		AbstractUrlBasedView view = super.buildView(viewName);
68  		
69  		// if DynamicTilesView, set tiles specific values
70  		if (view instanceof DynamicTilesView) {
71  			DynamicTilesView dtv = (DynamicTilesView)view;
72  			
73  			if (StringUtils.hasLength(tilesDefinitionName)) {
74  				dtv.setTilesDefinitionName(tilesDefinitionName);
75  			}
76  			
77  			if (StringUtils.hasLength(tilesBodyAttributeName)) {
78  				dtv.setTilesBodyAttributeName(tilesBodyAttributeName);
79  			}
80  
81  			if (tilesDefinitionDelimiter != null) {
82  				dtv.setTilesDefinitionDelimiter(tilesDefinitionDelimiter);
83  			}
84  		}
85  		
86  		return view;
87  	}
88  	
89  }