瓷砖不使用布局

时间:2016-12-17 18:59:02

标签: spring-mvc thymeleaf tiles

应用程序有效,但我的瓷砖有问题。我的应用程序不希望使用布局呈现页面,只需要简单页面(在“内容”目录中)。我认为在tile文件中有些不对劲。我没有任何错误 我确定tile文件的路径是正确的(如果我将更改为其他,它将是错误 - 文件不存在)。

Screenshot

MVC配置的一部分:

@Bean(name = "templateResolver")
    public SpringResourceTemplateResolver createTemplateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCacheable(false);
        return templateResolver;
    }


@Bean(name = "templateEngine")
public SpringTemplateEngine createTemplateEngine(SpringResourceTemplateResolver springResolver) {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(springResolver);
    templateEngine.addDialect(new SpringSecurityDialect());
    templateEngine.addDialect(new TilesDialect());
    templateEngine.setMessageSource(messageSource());
    return templateEngine;
}

@Bean
public ViewResolver createTilesViewResolver(SpringTemplateEngine templateEngine) {
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine);
    viewResolver.setViewClass(ThymeleafTilesView.class);
    viewResolver.setCache(false);
    viewResolver.setCharacterEncoding("UTF-8");
    viewResolver.setOrder(Ordered.LOWEST_PRECEDENCE);
    return viewResolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/").setCachePeriod(cachePeriod);
    registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}

Tiles XML:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
          "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
          "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

<tiles-definitions>

    <definition name="baseLayout" template="/template/layout">
        <put-attribute name="header" value="/template/fragments/header" />
        <put-attribute name="nav" value="/template/fragments/nav" />
        <put-attribute name="footer" value="/template/fragments/footer" />
    </definition> 

    <definition name="*" extends="baseLayout">
        <put-attribute name="title" value="pageTitle/{1}" />
        <put-attribute name="content" value="pageContent/{1}" />
    </definition>

    <definition name="*/*" extends="baseLayout">
        <put-attribute name="title" value="pageTitle/{1}/{2}" />
        <put-attribute name="content" value="pageContent/{1}/{2}" />
    </definition>

    <definition name="content/*/*" extends="baseLayout">
        <put-attribute name="title" value="pageTitle/{1}/{2}" />
        <put-attribute name="content" value="pageContent/{1}/{2}" />
    </definition>

    <definition name="content/*/*/*" extends="baseLayout">
        <put-attribute name="title" value="pageTitle/content/{1}/{2}/{3}" />
        <put-attribute name="content" value="pageContent/content/{1}/{2}/{3}" />
    </definition>

    <definition name="content/*/*/*/*" extends="baseLayout">
        <put-attribute name="title" value="pageTitle/content/{1}/{2}/{3}/{4}" />
        <put-attribute name="content" value="pageContent/content/{1}/{2}/{3}/{4}" />
    </definition>

    <definition name="pageContent/*" template="{1} :: content" />
    <definition name="pageTitle/*" template="{1} :: title" />

    <definition name="pageContent/*/*" template="{1}/{2} :: content" />
    <definition name="pageTitle/*/*" template="{1}/{2} :: title" />

    <definition name="pageContent/*/*/*" template="{1}/{2}/{3} :: content" />
    <definition name="pageTitle/*/*/*" template="{1}/{2}/{3} :: title" />

    <definition name="pageContent/*/*/*/*" template="{1}/{2}/{3}/{4} :: content" />
    <definition name="pageTitle/*/*/*/*" template="{1}/{2}/{3}/{4} :: title" />

    <definition name="pageContent/*/*/*/*/*" template="{1}/{2}/{3}/{4}/{5} :: content" />
    <definition name="pageTitle/*/*/*/*/*" template="{1}/{2}/{3}/{4}/{5} :: title" />

</tiles-definitions>

pom.xml的一部分

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <spring-security-version>4.1.0.RELEASE</spring-security-version>
        <thymeleaf-version>2.1.5.RELEASE</thymeleaf-version>
        <thymeleaf-springsecurity3>2.1.5.RELEASE</thymeleaf-springsecurity3>
        <thymeleaf-springsecurity4>2.1.3.RELEASE</thymeleaf-springsecurity4>
        <thymeleaf-java8time-version>3.0.0.RELEASE</thymeleaf-java8time-version>
        <thymeleaf-extras-tiles-version>2.1.1.RELEASE</thymeleaf-extras-tiles-version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.8.RELEASE</version>
    </parent>

    <dependencies>

        <!-- Java -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>



        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>



        <!-- Spring security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring-security-version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring-security-version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring-security-version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>



        <!-- Thymeleaf -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>${thymeleaf-version}</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring3</artifactId>
            <version>${thymeleaf-version}</version>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>${thymeleaf-version}</version><!--$NO-MVN-MAN-VER$-->
        </dependency>



        <!-- Thymeleaf extras -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity3</artifactId>
            <version>${thymeleaf-springsecurity4}</version><!--$NO-MVN-MAN-VER$-->
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
            <version>${thymeleaf-springsecurity4}</version><!--$NO-MVN-MAN-VER$-->
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
            <version>${thymeleaf-java8time-version}</version>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-tiles2</artifactId>
            <version>${thymeleaf-extras-tiles-version}</version>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-tiles2-spring3</artifactId>
            <version>${thymeleaf-extras-tiles-version}</version>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-tiles2-spring4</artifactId>
            <version>${thymeleaf-extras-tiles-version}</version>
        </dependency>

        <!-- Guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>



        <!-- Spring JPA -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.10.1.RELEASE</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>1.12.2.RELEASE</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <!-- Hibernate framework -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.0.Final</version><!--$NO-MVN-MAN-VER$-->
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.0.Final</version><!--$NO-MVN-MAN-VER$-->
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.3.3.Final</version><!--$NO-MVN-MAN-VER$-->
        </dependency>

        <!-- Apache tiles -->
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-core</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
             <groupId>org.apache.tiles</groupId>
             <artifactId>tiles-servlet</artifactId>
             <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-template</artifactId>
            <version>2.2.2</version>
        </dependency>

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


        <!-- INNE -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-access</artifactId>
        </dependency>

        <!-- <dependency>
          <groupId>nz.net.ultraq.thymeleaf</groupId>
          <artifactId>thymeleaf-layout-dialect</artifactId>
          <version>2.1.2</version>
        </dependency> -->
    </dependencies>

我做错了什么?

感谢您的回答! 祝你今天愉快! :)

1 个答案:

答案 0 :(得分:-1)

模板文件夹看起来像是在视图下。您需要使用正确的路径

定义Tiles.xml

/views/template/layout