java - 如何将 Apache Tiles 集成到 Spring Boot Web 应用程序中?

标签 java spring spring-boot apache-tiles

我想在我的 Spring Boot 应用程序中添加 apache 磁贴。

就像我们在 application.properties 文件中配置前缀和后缀一样,我也想知道如何配置tiles.xml。

我在 POM.xml 文件中添加了 apache 磁贴的 Maven 依赖项,但是当我从 Controller 返回磁贴定义的名称时,它给出了 404 错误。

这是我的 POM.xml 文件

    <!-- 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>

这是我的tiles.xml 文件

<?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>

这是我的 Controller 方法

@Controller
public class LandingPage {

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

这是我的 TilesConfig.java 文件

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;
   }
}

最佳答案

您需要做的第一件事是在配置中使用@EnableWebMvc。我已添加以下配置 您可以尝试将以下配置添加到您的应用程序

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();
    }
}

关于java - 如何将 Apache Tiles 集成到 Spring Boot Web 应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58103373/

相关文章:

java - 使用spring读取属性表单文件

java - Spring Boot搭建常用服务的pom结构

java - 如何在 Spring Boot XML 文件中注册一个 servlet?

java - 如何在 Spring Boot 应用程序中使用 Hibernate Validation 进行 Bean 验证?

java - Spring Boot Java Controller 的单元测试

java - 在heroku上部署而不保留gradlew文件

java - 在 Java 程序中使用 Linux Strace 命令

java - 是否可以计算 Javacv 或 opencv 中特定行的宽度?

java - 为什么 jmap -histo 的结果与 jmap -dump 不同

java - 使用外部 .cfg 文件而不是在构建时定义属性文件