Apache Tiles Integration with Spring MVC – Apache Tiles与Spring MVC的集成

最后修改: 2016年 10月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Apache Tiles is a free, open source templating framework purely built on the Composite design pattern.

Apache Tiles是一个免费的、开源的模板框架,纯粹建立在Composite设计模式之上。

A Composite design pattern is a type of structural pattern which composes objects into tree structures to represent whole-part hierarchies and this pattern treats individual objects and composition of objects uniformly. In other words, in Tiles, a page is built by assembling a composition of sub views called Tiles.

复合设计模式是一种结构模式,它将对象组合成树状结构来表示整个部分的层次结构,这种模式对单个对象和对象的组合进行统一处理。换句话说,在Tiles中,一个页面是由称为Tiles的子视图的组合来建立的。

The advantages of this framework over other frameworks include:

与其他框架相比,这个框架的优势包括。

  • re-usability
  • ease in configuration
  • low performance overhead

In this article, we’ll focus on integrating Apache Tiles with Spring MVC.

在这篇文章中,我们将重点讨论将Apache Tiles与Spring MVC整合

2. Dependency Configuration

2.依赖性配置

The first step here is to add the necessary dependency in the pom.xml:

这里的第一步是在pom.xml中添加必要的依赖

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-jsp</artifactId>
    <version>3.0.8</version>
</dependency>

3. Tiles Layout Files

3.瓷砖布局文件

Now we need to define the template definitions and specifically as per each page we will overwrite the template definitions for that specific page:

现在,我们需要定义模板定义,具体到每个页面,我们将覆盖该特定页面的模板定义。

<tiles-definitions>
    <definition name="template-def" 
           template="/WEB-INF/views/tiles/layouts/defaultLayout.jsp">  
        <put-attribute name="title" value="" />  
        <put-attribute name="header" 
           value="/WEB-INF/views/tiles/templates/defaultHeader.jsp" />  
        <put-attribute name="menu" 
           value="/WEB-INF/views/tiles/templates/defaultMenu.jsp" />  
        <put-attribute name="body" value="" />  
        <put-attribute name="footer" 
           value="/WEB-INF/views/tiles/templates/defaultFooter.jsp" />  
    </definition>  
    <definition name="home" extends="template-def">  
        <put-attribute name="title" value="Welcome" />  
        <put-attribute name="body" 
           value="/WEB-INF/views/pages/home.jsp" />  
    </definition>  
</tiles-definitions>

4. ApplicationConfiguration and Other Classes

4.ApplicationConfiguration和其他类

As part of configuration we will create three specific java classes called ApplicationInitializer, ApplicationController and ApplicationConfiguration:

作为配置的一部分,我们将创建三个特定的java类,称为ApplicationInitializer, ApplicationControllerApplicationConfiguration

  • ApplicationInitializer initializes and checks the necessary configuration specified in the ApplicationConfiguration classes
  • ApplicationConfiguration class contains the configuration for integrating Spring MVC with Apache Tiles framework
  • ApplicationController class works in sync with tiles.xml file and redirects to the necessary pages basing on the incoming requests

Let us see each of the classes in action:

让我们看看每个班级的行动。

@Controller
@RequestMapping("/")
public class TilesController {
    @RequestMapping(
      value = { "/"}, 
      method = RequestMethod.GET)
    public String homePage(ModelMap model) {
        return "home";
    }
    @RequestMapping(
      value = { "/apachetiles"}, 
      method = RequestMethod.GET)
    public String productsPage(ModelMap model) {
        return "apachetiles";
    }
 
    @RequestMapping(
      value = { "/springmvc"},
      method = RequestMethod.GET)
    public String contactUsPage(ModelMap model) {
        return "springmvc";
    }
}
public class WebInitializer implements WebApplicationInitializer {
 public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        
        ctx.register(TilesApplicationConfiguration.class);

        container.addListener(new ContextLoaderListener(ctx));

        ServletRegistration.Dynamic servlet = container.addServlet(
          "dispatcher", new DispatcherServlet(ctx));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

There are two important classes which play a key role in configuring tiles in a Spring MVC application. They are TilesConfigurer and TilesViewResolver:

在Spring MVC应用程序中,有两个重要的类在配置瓷砖方面发挥着关键作用。它们是TilesConfigurerTilesViewResolver

  • TilesConfigurer helps in linking the Tiles framework with the Spring framework by providing the path to the tiles-configuration file
  • TilesViewResolver is one of the adapter class provided by Spring API to resolve the tiles view

Finally, in the ApplicationConfiguration class, we used TilesConfigurer and TilesViewResolver classes to achieve the integration:

最后,在ApplicationConfiguration类中,我们使用TilesConfigurerTilesViewResolver类来实现整合。

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.baeldung.spring.controller.tiles")
public class TilesApplicationConfiguration implements WebMvcConfigurer {
    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(
          new String[] { "/WEB-INF/views/**/tiles.xml" });
        tilesConfigurer.setCheckRefresh(true);
        
        return tilesConfigurer;
    }
    
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        TilesViewResolver viewResolver = new TilesViewResolver();
        registry.viewResolver(viewResolver);
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
          .addResourceLocations("/static/");
    }
}

5. Tiles Template Files

5.瓷砖模板文件

Till now we had finished the configuration of Apache Tiles framework and the definition of the template and specific tiles used in the whole application.

到目前为止,我们已经完成了Apache Tiles框架的配置以及整个应用程序中使用的模板和具体的瓷砖的定义。

In this step, we need to create the specific template files which have been defined in the tiles.xml.

在这一步,我们需要创建在 tiles.xml中定义的特定模板文件。

Please find the snippet of the layouts which can be used as a base to build specific pages:

请找到布局的片段,它可以作为一个基础来建立特定的页面。

<html>
    <head>
        <meta 
          http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title><tiles:getAsString name="title" /></title>
        <link href="<c:url value='/static/css/app.css' />" 
            rel="stylesheet">
        </link>
    </head>
    <body>
        <div class="flex-container">
            <tiles:insertAttribute name="header" />
            <tiles:insertAttribute name="menu" />
        <article class="article">
            <tiles:insertAttribute name="body" />
        </article>
        <tiles:insertAttribute name="footer" />
        </div>
    </body>
</html>

6. Conclusion

6.结论

This concludes the integration of Spring MVC with Apache Tiles.

至此,Spring MVC与Apache Tiles的整合工作结束。

You can find the full implementation in the following github project.

你可以在下面的github项目中找到完整的实现。