1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }