Spring整合Tiles

1.假设Spring相关的包和配置已经导入成功(后续有时间补上,本项目用的是3.2.0版本)。

2.导入Tiles相关的jar包。

  tiles-api-2.2.2.jar

  tiles-core-2.2.2.jar

  tiles-jsp-2.2.2.jar

  tiles-servlet-2.2.2.jar

  tiles-template-2.2.2.jar

3.在servlet.xml中增加Spring和Tiles的整合以及Tiles的配置。

    <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" >
        <property name="order" value="0" />
    </bean>
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/pbx-views/tiles.xml</value>
            </list>
        </property>
    </bean>

4.定义网站的模版(style.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
    <div>
        <table>
            <tr>
                <td colspan="2">
                    <tiles:insertAttribute name="logo"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <tiles:insertAttribute name="menu"/>
                </td>
            </tr>
            <tr>
                <td>
                    <tiles:insertAttribute name="body"/>
                </td>
                <td>
                    <tiles:insertAttribute name="advers"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <tiles:insertAttribute name="footer"/>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>

5.关联页面模版和Tiles(tiles.xml)

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

<tiles-definitions>
  <!-- layout for index of MYCOLLEGE  -->
  <definition name="baseLayout" template="/WEB-INF/pbx-views/frame/style1.jsp">
    <put-attribute name="logo" value="/WEB-INF/pbx-views/frame/logo.jsp"/>
    <put-attribute name="menu" value="/WEB-INF/pbx-views/frame/menu.jsp"/>
    <put-attribute name="body" value="/WEB-INF/pbx-views/frame/body.jsp"/>
    <put-attribute name="advers" value="/WEB-INF/pbx-views/frame/advers.jsp"/>
    <put-attribute name="footer" value="/WEB-INF/pbx-views/frame/footer.jsp"/>
  </definition>

  <definition name="*" extends="baseLayout">
      <put-attribute name="body" value="/WEB-INF/pbx-views/{1}.jsp"/>
  </definition>
  
</tiles-definitions>

PS:此处用了*适配页面定位。

6.然后就可以在controller中方便的映射了。

@Controller
public class IndexController {
    @RequestMapping(value = "/index")
    public String index() {
                //your service action  
        return "home";
    }
}

7.流程梳理。

7.1  index请求被Spring分发到IndexController;

7.2  IndexController进行相关的业务处理后,返回字符串home;

7.3  字符串home被TilesViewResolver解析后,返回一个

      由logo.jsp/menu.jsp/home.jsp/advers.jsp/footer.jsp组成的一个组合页面。

8. tiles 3.0

tiles 3.0的jar包拆分的很细致。

和Spring 4.2.5关联需要的包如下:

1 tiles-core
2 tiles-api
3 tiles-jsp
4 tiles-request-api
5 tiles-request-servlet
6 commons-digester
7 commons-beanutils
8 tiles-autotag-core-runtime
9 tiles-request-jsp
10 tiles-request-api
11 tiles-template

 

原文地址:https://www.cnblogs.com/yoyotl/p/4626579.html