tiles

来源:互联网 发布:下载藏文软件 编辑:程序博客网 时间:2024/04/29 22:43
我以前以为J2EE的东东真复杂,又是什么tiger,又是什么tiles,又是什么cocoon啊,又是什么hibernate,名目烦杂,让人不禁望而生畏,这时又不自禁的和微软比较一下,是啊,自由软件有好处,但是名目太烦杂了,真的让人很难接受。今天才真正认识了什么叫做tiles,说白了其实就是个include.但是又搞了一套一套的规则,一套一套的配置,真是让人有点受不了。

        http://struts.apache.org/userGuide/dev_tiles.html上面是这么说得:Tiles builds on the "include" feature provided by the JavaServer Pages specification to provide a full-featured, robust framework for assembling presentation pages from component parts.大意是说tiles是从jsp的“include”发展过来的以提供更加全面特性的,健壮的集成组件框架给页面。说白了,还不是再利用一下吗,搞了这么多名目繁多的东西,让我又要记住一大堆东西,这时才觉得软件业还是有个领袖好啊,大家才不至于迷失方向。

       闲话不多说,我来说说tiles,作用刚才说过了,主要是要想利用它要做到:

1。在web-inf/struts-config.xml中加入如下的配置

<taglib>
    <taglib-uri>struts-tiles.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
  </taglib>

当然你必须的确保这个struts-tiles.tld放在了这个目录其中。如果没有去jarkarta上下载吧,现在是struts1.2.7。它里面包含了。

2。给tiles加个定义文件

          如果想集成定义呢,还得在WEB-INF/目录下create一个tiles-def.xml然后在struts-config.xml中加入如下语句,以让actionServlet init的时候能够把tiles模板配置信息记录下来。    

<plug-in className="org.apache.struts.tiles.TilesPlugin" >
  <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
  <set-property property="definitions-parser-validate" value="true" />
</plug-in>
tiles-def.xml可以象下面这样
<?xml version="1.0" encoding="ISO-8859-1" ?>
 <!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
       "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
   <definition  name="index-definition"  path="/layout.jsp">
      <put name="sidebar" value="sidebar.jsp"/>
      <put name="header"  value="header.jsp"/>  
      <put name="content" value="indexContent.jsp"/>  
      <put name="footer"  value="footer.jsp"/>  
   </definition>
   <definition  name="product-definition"  path="/layout.jsp">
      <put name="sidebar" value="sidebar.jsp"/>
      <put name="header"  value="header.jsp"/>  
      <put name="content" value="productContent.jsp"/>  
      <put name="footer"  value="footer.jsp"/>  
   </definition>
</tiles-definitions>   
当然也可以把definition 写在任何一个jsp页面啦.
<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
<tiles:definition id="definitionName" page="/layouts/myLayout.jsp" >
<tiles:put name="title" value="Page Title" />
<tiles:put name="header" value="/tiles/header.jsp" />
<tiles:put name="footer" value="/tiles/footer.jsp" />
<tiles:put name="menu" value="/tiles/menu.jsp" />
<tiles:put name="body" value="/tiles/helloBody.jsp" />
</tiles:definition>
它会被存为一个scope,而且还可以用scope=session|page|request设定这个definition的使用范围.
3.下面是tiles将要用到的tag和作用的描述。
Tag NameDescriptioninsert Insert a tiles/component/template. definition Create a tile /component / template definition bean. put Put an attribute into tile/component/template context. putList Declare a list that will be pass as attribute to tile. add Add an element to the surrounding list. Equivalent to 'put', but for list element. get Gets the content from request scope that was put there by a put tag. getAsString Render the value of the specified tile/component/template attribute to the current JspWriter useAttribute Use attribute value inside page. importAttribute Import Tile's attribute in specified context. initComponentDefinitions Initialize Tile/Component definitions factory. 

insert就是include了。最常用的结构就是


   
   

Definitions在定义中用,象上面的例子中一样。有几个属性id extends scope page不说也明白吧。

put挺常用的,属性有name,value,type=string|page|templage|definition,direct ,content

还有个useAttribute也挺常用,都比较简单。

4.tiles还使用了commons-logging包以输出不同的信息和调试声明,要想这样,首先在web-inf/classes下面自己动手建两个文件:

commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
simplelog.properties
# Logging detail level,
# Must be one of ("trace", "debug", "info", "warn", "error", or "fatal").
org.apache.commons.logging.simplelog.defaultlog=trace

Definitions在定义中用,象上面的例子中一样。有几个属性id extends scope page不说也明白吧。

put挺常用的,属性有name,value,type=string|page|templage|definition,direct ,content

还有个useAttribute也挺常用,都比较简单。

4.tiles还使用了commons-logging包以输出不同的信息和调试声明,要想这样,首先在web-inf/classes下面自己动手建两个文件:

commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
simplelog.properties
# Logging detail level,
# Must be one of ("trace", "debug", "info", "warn", "error", or "fatal").
org.apache.commons.logging.simplelog.defaultlog=trace

Definitions在定义中用,象上面的例子中一样。有几个属性id extends scope page不说也明白吧。

put挺常用的,属性有name,value,type=string|page|templage|definition,direct ,content

还有个useAttribute也挺常用,都比较简单。

4.tiles还使用了commons-logging包以输出不同的信息和调试声明,要想这样,首先在web-inf/classes下面自己动手建两个文件:

commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
simplelog.properties
# Logging detail level,
# Must be one of ("trace", "debug", "info", "warn", "error", or "fatal").
org.apache.commons.logging.simplelog.defaultlog=trace