I haven't seen this before so thought I'd post an example where I've successfully gotten Tiles to work with programmatic rather than xml config with the spring-social-quickstart example for Spring 3.1
This is useful for Spring 3.1 in general and the move away from the clutter of XML.
Updated WebMvcConfig.xml
Code:/* * Copyright 2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.social.quickstart.config; import java.util.HashMap; import java.util.Map; import javax.inject.Inject; import org.apache.tiles.Attribute; import org.apache.tiles.Definition; import org.apache.tiles.context.TilesRequestContext; import org.apache.tiles.definition.UnresolvingLocaleDefinitionsFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.social.connect.UsersConnectionRepository; import org.springframework.social.quickstart.user.UserInterceptor; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.UrlBasedViewResolver; import org.springframework.web.servlet.view.tiles2.TilesConfigurer; import org.springframework.web.servlet.view.tiles2.TilesView; /** * Spring MVC Configuration. * @author Keith Donald */ @Configuration @EnableWebMvc public class WebMvcConfig extends WebMvcConfigurerAdapter { private static final Map<String, Definition> tiles = new HashMap<String,Definition>(); private static final Attribute TEMPLATE = new Attribute("/WEB-INF/views/layout/layout.jsp"); public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new UserInterceptor(usersConnectionRepository)); } public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/signin"); registry.addViewController("/signout"); } @Bean public TilesConfigurer tilesConfigurer() { TilesConfigurer tilesConfigurer = new TilesConfigurer(); tilesConfigurer.setDefinitionsFactoryClass(JavaDefinitionsFactory.class); tilesConfigurer.setDefinitions(new String[] {}); addDefinition("home", "Home", "/WEB-INF/views/home.jsp"); addDefinition("signin", "Sign in", "/WEB-INF/views/signin.jsp"); addDefinition("signin/facebook", "Sign in", "/WEB-INF/views/signin.jsp"); return tilesConfigurer; } private void addDefinition(String name, String title, String body) { Map<String, Attribute> attributes = getDefaultAttributes(); attributes.put("title", new Attribute(title)); attributes.put("body", new Attribute(body)); tiles.put(name, new Definition(name, TEMPLATE, attributes)); } private Map<String, Attribute> getDefaultAttributes() { Map<String, Attribute> attributes = new HashMap<String,Attribute>(); attributes.put("header", new Attribute("/WEB-INF/views/layout/header.jsp")); attributes.put("menu", new Attribute("/WEB-INF/views/layout/menu.jsp")); attributes.put("footer", new Attribute("/WEB-INF/views/layout/footer.jsp")); return attributes; } @Bean public ViewResolver viewResolver() { UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); viewResolver.setViewClass(TilesView.class); return viewResolver; } public static class JavaDefinitionsFactory extends UnresolvingLocaleDefinitionsFactory { public JavaDefinitionsFactory(){} @Override public Definition getDefinition(String name, TilesRequestContext tilesContext) { return tiles.get(name); } } private @Inject UsersConnectionRepository usersConnectionRepository; }
Updates (new required dependencies) to pom.xml
Code:<dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-extras</artifactId> <version>2.2.2</version> </dependency> <!-- Logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.6.1</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> <scope>runtime</scope> </dependency>


Reply With Quote
