[JAVA] springboot中jsp配置tiles全过程

2170 0
王子 2022-11-9 09:08:55 | 显示全部楼层 |阅读模式
目录

    1.pom.xml中添加依赖2.新建 tiles.xml3.新建tiles配置类ConfigurationForTiles.java4.jsp5.控制类6.测试

tiles是jsp的前端框架;像fream标签一样可以把多个页面组合起来;
完成后的目录结构:



1.pom.xml中添加依赖
  1.       <!-- Add Apache Tiles into the mix -->
  2.         <dependency>
  3.             <groupId>org.apache.tiles</groupId>
  4.             <artifactId>tiles-jsp</artifactId>
  5.             <version>3.0.4</version>
  6.         </dependency>
复制代码
2.新建 tiles.xml

可以放在WEB-INF/tiles/目录里
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <!DOCTYPE tiles-definitions PUBLIC
  3.        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
  4.        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
  5. <tiles-definitions>
  6.     <!-- Templates -->
  7.     <definition name="layout.basic" template="/WEB-INF/tiles/basic.jsp">
  8.         <put-attribute name="title" value="Spring Web MVC with Tiles 3" />
  9.         <put-attribute name="header" value="/WEB-INF/tiles/header.jsp" />
  10.         <put-attribute name="body" value="" />
  11.         <put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp" />
  12.     </definition>
  13.     <!-- Pages -->  
  14.     <definition name="site.homepage" extends="layout.basic">
  15.         <put-attribute name="body" value="/WEB-INF/tiles/home.jsp" />
  16.     </definition>
  17. </tiles-definitions>
复制代码
3.新建tiles配置类ConfigurationForTiles.java


  1. @Configuration
  2. public class ConfigurationForTiles {
  3.     /**
  4.      * Initialise Tiles on application startup and identify the location of the tiles configuration file, tiles.xml.
  5.      *
  6.      * @return tiles configurer
  7.      */
  8.     @Bean
  9.     public TilesConfigurer tilesConfigurer() {
  10.         final TilesConfigurer configurer = new TilesConfigurer();
  11.         configurer.setDefinitions(new String[] { "WEB-INF/tiles/tiles.xml" });
  12.         configurer.setCheckRefresh(true);
  13.         return configurer;
  14.     }
  15.     /**
  16.      * Introduce a Tiles view resolver, this is a convenience implementation that extends URLBasedViewResolver.
  17.      *
  18.      * @return tiles view resolver
  19.      */
  20.     @Bean
  21.     public TilesViewResolver tilesViewResolver() {
  22.         final TilesViewResolver resolver = new TilesViewResolver();
  23.         resolver.setViewClass(TilesView.class);
  24.         return resolver;
  25.     }
  26. }
复制代码
注意tiles.xml文件目录要正确;

4.jsp



1. basic.jsp
  1. <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
  2. <html>
  3.     <head>
  4.         <title><tiles:getAsString name="title" /></title>
  5.     </head>
  6.     <body>
  7.     basic.jsp
  8.         <!-- Header -->
  9.         <tiles:insertAttribute name="header" />
  10.         <!-- Body -->
  11.         <tiles:insertAttribute name="body" />
  12.         <!-- Footer -->
  13.         <tiles:insertAttribute name="footer" />
  14.     </body>
  15. </html>
复制代码
2.footer.jsp
  1. <div>The Footer   footer.jsp</div>
复制代码
3.header.jsp
  1. <div>The Header header.jsp</div>
复制代码
4.home.jsp
  1. <div>
  2.     Main content would go here. Lets try.     home.jsp
  3. </div>
复制代码
5.控制类
  1. @Controller
  2. public class GreetingController {
  3.     private Log log = LogFactory.getLog(this.getClass());
  4.     @RequestMapping(value = "/home", method=RequestMethod.GET)
  5.     public String home() {
  6.         return "site.homepage";  //这个是    definition 的  name="site.homepage"
  7.     }
  8. }
复制代码
6.测试



完成!
以上为个人经验,希望能给大家一个参考,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行