springboot中jsp配置tiles全过程

 更新时间:2022年10月27日 10:28:24   作者:阿拉的梦想  
这篇文章主要介绍了springboot中jsp配置tiles全过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

tiles是jsp的前端框架;像fream标签一样可以把多个页面组合起来;

完成后的目录结构:

1.pom.xml中添加依赖

      <!-- Add Apache Tiles into the mix -->
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>3.0.4</version>
        </dependency>

2.新建 tiles.xml

可以放在WEB-INF/tiles/目录里

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>

    <!-- Templates -->

    <definition name="layout.basic" template="/WEB-INF/tiles/basic.jsp">
        <put-attribute name="title" value="Spring Web MVC with Tiles 3" />
        <put-attribute name="header" value="/WEB-INF/tiles/header.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp" />
    </definition>

    <!-- Pages -->  

    <definition name="site.homepage" extends="layout.basic">
        <put-attribute name="body" value="/WEB-INF/tiles/home.jsp" />
    </definition>

</tiles-definitions>

3.新建tiles配置类ConfigurationForTiles.java

@Configuration
public class ConfigurationForTiles {

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

注意tiles.xml文件目录要正确;

4.jsp

1. basic.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
    <head>
        <title><tiles:getAsString name="title" /></title>
    </head>
    <body>
    basic.jsp
        <!-- Header -->
        <tiles:insertAttribute name="header" />
        <!-- Body -->
        <tiles:insertAttribute name="body" />
        <!-- Footer -->
        <tiles:insertAttribute name="footer" />
    </body>
</html>

2.footer.jsp

<div>The Footer   footer.jsp</div>

3.header.jsp

<div>The Header header.jsp</div>

4.home.jsp

<div>
    Main content would go here. Lets try.     home.jsp
</div>

5.控制类

@Controller
public class GreetingController {
    private Log log = LogFactory.getLog(this.getClass());

    @RequestMapping(value = "/home", method=RequestMethod.GET)
    public String home() {
        return "site.homepage";  //这个是    definition 的  name="site.homepage"
    }
}

6.测试

完成!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Spring Cloud Gateway入门解读

    Spring Cloud Gateway入门解读

    本篇文章主要介绍了Spring Cloud Gateway入门解读,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • IDEA创建方法时如何快速添加注释

    IDEA创建方法时如何快速添加注释

    这篇文章主要介绍了IDEA创建方法时如何快速添加注释问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • SpringBoot整合Apache Pulsar教程示例

    SpringBoot整合Apache Pulsar教程示例

    这篇文章主要为大家介绍了SpringBoot整合Apache Pulsar教程示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • 详解Java如何给按钮添加监视器

    详解Java如何给按钮添加监视器

    这篇文章主要介绍了详解Java如何给按钮添加监视器,使用匿名对象、实现接口、实现类、Lambda表达式、注解等,需要的朋友可以参考下
    2023-04-04
  • Java 中HttpURLConnection附件上传的实例详解

    Java 中HttpURLConnection附件上传的实例详解

    这篇文章主要介绍了Java 中HttpURLConnection附件上传的实例详解的相关资料,希望通过本文大家能掌握这样的知识内容,需要的朋友可以参考下
    2017-09-09
  • 使用Spring AntPathMatcher的doMatch方法

    使用Spring AntPathMatcher的doMatch方法

    这篇文章主要介绍了使用Spring AntPathMatcher的doMatch方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 深入剖析java中的集合框架

    深入剖析java中的集合框架

    下面小编就为大家带来一篇深入剖析java中的集合框架。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • Spring Boot @Autowired @Resource属性赋值时机探究

    Spring Boot @Autowired @Resource属性赋值时机探究

    这篇文章主要为大家介绍了Spring Boot @Autowired @Resource属性赋值时机,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • 使用@Validated和@Valid 解决list校验的问题

    使用@Validated和@Valid 解决list校验的问题

    这篇文章主要介绍了使用@Validated和@Valid 解决list校验的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • SpringBoot2零基础到精通之异常处理与web原生组件注入

    SpringBoot2零基础到精通之异常处理与web原生组件注入

    SpringBoot是Spring全家桶的成员之一,基于约定优于配置的思想(即有约定默认值,在不配置的情况下会使用默认值,在配置文件下配置的话会使用配置的值)。SpringBoot是一种整合Spring技术栈的方式(或者说是框架),同时也是简化Spring的一种快速开发的脚手架
    2022-03-03

最新评论