Search code examples
javaspringspring-bootapache-tiles

How to integrate Apache Tiles in a Spring Boot WebApplication?


I want to add apache tiles in my spring boot application.

Like we configure prefix and suffix in application.properties file i want to know how we can configure tiles.xml as well.

I have added maven dependency of apache tiles in POM.xml file, But when i am returning the name of the tile definition from my controller its giving 404 error.

This is my POM.xml file

    <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>3.0.8</version>
    </dependency>

This is my tiles.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC  "-//Apache Software Foundation//DTD Tiles Configuration 
3.0//EN"  "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="home-page"
    template="/WEB-INF/layout/layout.jsp">
    <put-attribute name="body" value="/WEB-INF/pages/landing-page.jsp" />
    <put-attribute name="script" value="" />
    <put-attribute name="stylesheet" value="" />
</definition>
</tiles-definitions>

This is my controller method

@Controller
public class LandingPage {

    @RequestMapping("/")
    public String landingPage() {
        return "home-page";
    }
}

This is my TilesConfig.java file

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;

@Configuration
public class TilesConfig {
    @Bean
    public UrlBasedViewResolver viewResolver() {
        UrlBasedViewResolver tilesViewResolver = new 
        UrlBasedViewResolver();
        tilesViewResolver.setViewClass(TilesView.class);
    return tilesViewResolver;
}

@Bean
public TilesConfigurer tilesConfigurer() {
    TilesConfigurer tilesConfigurer = new TilesConfigurer();
    String[] tilesXml = { "WEB-INF/tiles.xml" };
    tilesConfigurer.setDefinitions(tilesXml);
    return tilesConfigurer;
   }
}

Solution

  • First thing you need to do is to use @EnableWebMvc in your configuration.I have added in below configuration Can you try adding the below configuration to your application

    public class MvcWebApplicationInitializer 
          extends AbstractAnnotationConfigDispatcherServletInitializer {
    
      // Load database and spring security configuration
      @Override
      protected Class<?>[] getRootConfigClasses() {
        return new Class[] { WebAppInitializer.class};
      }
    
      // Load spring web configuration
      @Override
      protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebMvcConfig.class };
      }
    
      @Override
      protected String[] getServletMappings() {
        return new String[] { "/" };
      }
    
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = { "your component scan packages" })
    public class WebMvcConfig implements WebMvcConfigurer {
    
        /**
         * Initialise Tiles on application startup and identify the location of the tiles configuration file, tiles.xml.
         * 
         * @return tiles configurer
         */
        @Bean
        public TilesConfigurer tilesConfigurer() {
            final TilesConfigurer configurer = new TilesConfigurer();
            configurer.setDefinitions(new String[] { "WEB-INF/tiles.xml" });
            configurer.setCheckRefresh(true);
            return configurer;
        }
    
        /**
         * Introduce a Tiles view resolver, this is a convenience implementation that extends URLBasedViewResolver.
         * 
         * @return tiles view resolver
         */
        @Bean
        public TilesViewResolver tilesViewResolver() {
            final TilesViewResolver resolver = new TilesViewResolver();
            resolver.setViewClass(TilesView.class);
            return resolver;
        }
    
        @Override
        public void configureDefaultServletHandling(
          DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    }