之前发了有关套版的Sitemesh3 配置教学

所以这次就换了tiles

不过要跟各位客倌说声抱歉呀~

因为我只会配置Struts2+Tiles呀~

不过如果都要撘配Struts2的话~我会比较喜欢用tiles

开始之前还没有配置Struts2的人可以先去看Struts2 架构配置教学

 

这次配置会用到的jar档如下请先加到lib里面

struts2-tiles-plugin-2.2.3.jar
tiles-jsp-2.0.6.jar
tiles-api-2.0.6.jar
tiles-core-2.0.6.jar
commons-digester-2.0.jar
commons-beanutils-1.7.0.jar
commons-collections-3.1.jar

 

再将tiles-jsp-2.0.6.jar里面的META-INF底下的tld资料夹里面的tiles-jsp.tld这个档案

复制到专案里面的WEB-INF底下

 

再将下列配置加到web.xml里面

    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>

 

讨厌鬼的范例

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <display-name>leather</display-name>
   
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>
    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    
    
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

先在WebContent底下新增一个资料夹要叫什么都可以

讨厌鬼在这边叫tiles

在这个资料夹里面新一个叫leather_base.jsp的版面~当然要叫什么都可以

讨厌鬼的内容如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tiles-jsp.tld" prefix="tiles"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!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">
<link rel="stylesheet" type="text/css" href="../css/leather.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/jquery-ui-1.8.19.custom.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/ui.jqgrid.css"    media="screen" />
<link rel="stylesheet" type="text/css" href="../css/ui.multiselect.css"    media="screen" />
<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.8.19.custom.min.js"></script>
<script type="text/javascript" src="../js/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="../js/grid.locale-cn.js"></script>

<title>讨厌鬼教学</title>
</head>
<body>
    <div class="container">
        <tiles:insertAttribute name="leather_header"/>
        <div class="main">
             <tiles:insertAttribute name='leather_main'/>
        </div>
        <tiles:insertAttribute name="leather_footer"/>
    </div>
</body>
</html>

 

在这边只有两点要注意

1、请加入<%@ taglib uri="/WEB-INF/tiles-jsp.tld" prefix="tiles"%>

2、<tiles:insertAttribute name=""/> 这个语法就类似inculde,等等跟tiles的设定一起看会比较清楚

 

接下来要配置tiles的设定档

在WEB-INF底下新增一个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>
    <definition name="leather.base" template="/tiles/leather_base.jsp">
        <put-attribute name="leather_header" value="/tiles/header_tpl.jsp" />
        <put-attribute name="leather_main" value="/leather/index.jsp" />
        <put-attribute name="leather_footer" value="/tiles/footer_tpl.jsp" />
    </definition>
</tiles-definitions>

在这边请看一下put-attribute的name会跟jsp那边的insertAttribute name对应到

也就是说<tiles:insertAttribute name="leather_header"/>这一段会把/tiles/header_tpl.jsp的内容inculde进来

 

再来就是struts2的部分了

若要在struts2中用到tiles有两种方法

1、继承tiles-default

<package name="default" extends="tiles-default">

</package>

2、加入reslut-type

<result-types>
     <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>

 

讨厌鬼是用2配置如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
    <package name="default" extends="struts-default">
        <result-types>
            <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
        </result-types>
    </package>

    <package name="leather" extends="default" namespace="/leather">
         <action name="index" class="nerdy.action.LeatherAction" method="index">
              <result name="success" type="tiles">leather.base</result>
         </action>
     </package>   
</struts>

 

在result的部分type为tiles时就会去套用到tiles

leather.base是你在tiles.xml中的对应到 definition name="leather.base"的/tiles/leather_base.jsp这个版面

 

配置到这边基本上是好了

大家有没有发现如果result type=tiles 时若为leather.base

是只会回传同一个画面的

如果要不同画面是要在tiles.xml中配置多个definition

大家可能会觉得很麻烦~不过我觉得很好用....

总比sitemesh在修改时要去想url的路径

tiles一个对一个好找多了~

 

 

备注

这篇的这个备注很重要为了让tiles.xml看起来不会这么烦!!!!!!!

讨厌鬼在tiles.xml中的配置

<definition name="leather.base" template="/tiles/leather_base.jsp">
        <put-attribute name="leather_header" value="/tiles/header_tpl.jsp" />
        <put-attribute name="leather_main" value="/leather/index.jsp" />
        <put-attribute name="leather_footer" value="/tiles/footer_tpl.jsp" />
</definition>

在leather_header与leather_footer是固定不变的

会变的只有leather_main

有可能今天是进首页所以是index.jsp

如果是文章页就会变成article.jsp

当然还是要宣告多个

不过可以改成下列的继承方式少打一些字

<definition name="leather.base" template="/tiles/leather_base.jsp">
    <put-attribute name="leather_header" value="/tiles/header_tpl.jsp" />
    <put-attribute name="leather_main" value="" />
    <put-attribute name="leather_footer" value="/tiles/footer_tpl.jsp" />
</definition>
    
<definition name="leather.index" extends="leather.base">
    <put-attribute name="leather_main" value="/leather/index.jsp" />
</definition>

<definition name="leather.article" extends="leather.base">
    <put-attribute name="leather_main" value="/leather/article.jsp" />
</definition>

 

 

我的心得报告

如果你的css版面跟程式都是自己来的话或者版面变化不多时~用什么都无所谓

但如果版面变化多而且是版面是由美编做的话

请用慎选你的套版工具~你永远都不会知道美编什么时候会在背后捅你一刀

相关文章