使用Apache Tiles3.x構建介面布局(二)

來源:互聯網
上載者:User

標籤:apache tiles   ui   web頁面配置   

    接著上一篇:使用Apache Tiles3.x構建介面布局(一)繼續構建頁面配置。


    上一篇樣本中已經對頁面的公用部分進行了抽取,建立了布局,並且將使用的布局應用的響應頁面了。


1.定義definition來改變公用部分

    現在有個新變化,響應頁面的footer部分需要發生點變化,這個時候仍然可以通過繼承的方式來配置一個definition。

 

  • /WEB-INF/tiles-defs.xml 中新增definition

<definition name="tiles.override.definition" extends="tiles.index.definition"><put-attribute name="footer"value="Copy Right &lt;a href=‘http://aiilive.blog.51cto.com‘&gt;野馬紅塵&lt;/a&gt;"></put-attribute></definition>


 1.1建立響應頁面/example/index_override.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%><tiles:insertDefinition name="tiles.override.definition"><tiles:putAttribute name="title" value="這是一個有Apache Tiles構建的頁面配置." /></tiles:insertDefinition>


 1.2訪問:/tiles/example/index_override.jsp

 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/57/81/wKiom1Sb96DDAm5oAAEhoooRGJE684.jpg" title="eample_index_override.png" width="280" height="209" border="0" hspace="0" vspace="0" style="width:280px;height:209px;" alt="wKiom1Sb96DDAm5oAAEhoooRGJE684.jpg" />

  注意:footer部分發生了改變,使用了tiles.override.definition的footer屬性。



 2.響應頁面中定義body

  •  /example/home.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%><tiles:insertDefinition name="tiles.index.definition"><tiles:putAttribute name="title" value="HOME 頁面." /><tiles:putAttribute name="body"><h3>這是HOME頁面的body</h3><ul><li>Apache Tiles</li><li>Sitemesh</li><li>Smarty</li><li>Freemarker</li><li>Velocity</li></ul></tiles:putAttribute></tiles:insertDefinition>

   

  訪問:/tiles/example/home.jsp

  650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/57/81/wKiom1Sb-gWTWy_ZAAFd6FchcBg656.jpg" title="eample_home.png" width="280" height="272" border="0" hspace="0" vspace="0" style="width:280px;height:272px;" alt="wKiom1Sb-gWTWy_ZAAFd6FchcBg656.jpg" />


 3.通過Servlet來訪問definition(tiles.index.definition),可程式化式!

   

package secondriver.tiles.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.tiles.Attribute;import org.apache.tiles.Definition;import org.apache.tiles.TilesContainer;import org.apache.tiles.access.TilesAccess;import org.apache.tiles.request.ApplicationContext;import org.apache.tiles.request.Request;import org.apache.tiles.request.servlet.ServletRequest;import org.apache.tiles.request.servlet.ServletUtil;public class TilesServlet extends HttpServlet {private static final long serialVersionUID = 2721434552222217027L;@Overrideprotected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {ApplicationContext applicationContext = ServletUtil.getApplicationContext(getServletContext());TilesContainer container = TilesAccess.getContainer(applicationContext);Request currentRequest = new ServletRequest(applicationContext,request, response);Definition definition = container.getDefinition("tiles.index.definition", currentRequest);definition.putAttribute("title", new Attribute("通過servlet訪問"));definition.putAttribute("body", new Attribute("這是通過Servlet處理後的definition."));container.render(definition, currentRequest);/** * 不需要處理的時候可以直接使用definition的name *  * tiles.index.definition沒有定義title屬性 *  * 而template檔案(布局)中用到所以需要在Servlet中進行添加 */// container.render("tiles.index.definition", currentRequest);}}

  訪問:/tiles/tilesservlet

 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/57/7F/wKioL1Sb_h3jkXb1AAFFVrJgc_s618.jpg" title="eample_servlet.png" width="280" height="289" border="0" hspace="0" vspace="0" style="width:280px;height:289px;" alt="wKioL1Sb_h3jkXb1AAFFVrJgc_s618.jpg" />


 注意:

  •  這裡使用了Servlet需要在web.xml中增加Servlet的配置

<servlet><servlet-name>tilesservlet</servlet-name><servlet-class>secondriver.tiles.servlet.TilesServlet</servlet-class></servlet><servlet-mapping><servlet-name>tilesservlet</servlet-name><url-pattern>/tilesservlet</url-pattern></servlet-mapping>
  •  在pom.xml中添加servlet-api:

 

<dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency>



4.通過標籤在響應頁面上配置definition,並插入到頁面中

  

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%><tiles:definition name="tiles.runtime.definiation"template="/layout/index_layout.jsp" extends="tiles.base.definition"><tiles:putAttribute name="title" value="通過標籤配置 definition." /><tiles:putAttribute name="body">通過tiles標籤配置的運行時definition</tiles:putAttribute></tiles:definition><tiles:insertDefinition name="tiles.runtime.definiation"></tiles:insertDefinition>

 

  訪問:/tiles/example/runtime.jsp

  650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/57/7F/wKioL1ScAFvzgdFVAAFkTQLIVLw274.jpg" title="eample_runtime.png" width="280" height="289" border="0" hspace="0" vspace="0" style="width:280px;height:289px;" alt="wKioL1ScAFvzgdFVAAFkTQLIVLw274.jpg" />


本文通過多種方式展示了Tiles的使用和其靈活性。另外Tiles還可以和Freemarker,Velocity,Spring,Struts等多種框架組成使用。


具體可以參考官方文檔:http://tiles.apache.org/framework/whats-new.html。 

另外更多的樣本:http://tiles.apache.org/download.html

本文出自 “野馬紅塵” 部落格,轉載請與作者聯絡!

使用Apache Tiles3.x構建介面布局(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.