2009年4月8日 星期三

spring 3.0.0.M2 整合 Apache Tiles 2 模版

一、
tiles相關的jar加到WEB-INF/lib,以3.0.0.M2的情型,必需下載tiles-2.0.7的版本,
tiles-2.1.x以上的版本,尚有問題,tiles本身也相依commons-beanutils 及 commons-digester,
都可以在Tiles-2.0.7-bin.tar.gz下載得到。固與Tiles相關的jar有:
commons-beanutils-1.7.0.jar
commons-digester-1.8.jar
commons-logging-1.1.1.jar
tiles-api-2.0.7.jar
tiles-core-2.0.7.jar
tiles-jsp-2.0.7.jar

二、
在spring的bean設定檔稍做修改,
a在viewResolver將InternalResourceViewResolver改用UrlBasedViewResolver
b並加入viewClass 設定為 org.springframework.web.servlet.view.tiles2.TilesView
c加入 TilesConfigurer
如下:
<!-- test jsp view -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<!-- Tiles 模版 -->
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/templates.xml</value>
</list>
</property>
</bean>

三、
在上面的TilesConfigurer bean中指定了/WEB-INF/defs/templates.xml這個模版定義檔
建立templates.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="/WEB-INF/jsp/test.jsp" template="/WEB-INF/jsp/templates/layout.jsp">
<put-attribute name="title" value="TPMA Tiles tutorial homepage" />
<put-attribute name="header" value="/WEB-INF/jsp/templates/header.jsp" />
<put-attribute name="menu" value="/WEB-INF/jsp/templates/menu.jsp" />
<put-attribute name="body" value="/WEB-INF/jsp/body.jsp" />
<put-attribute name="footer" value="/WEB-INF/jsp/templates/footer.jsp" />
</definition>
</tiles-definitions>

四、
上面的定義了當Controller的return
return new ModelAndView("test", "test", model);
的時後,test會被UrlBasedViewResolver組合成/WEB-INF/jsp/test.jsp,而TilesView則會到,
templates.xml找到/WEB-INF/jsp/test.jsp的定義,並根據layout.jsp頁面組合title、header、
menu、body、footer等頁面,layout.jsp的內容如下:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:getAsString name="title" /></title>
</head>
<body>
<table border="0" width="100%" cellspacing="5">
<tr>
<td colspan="2"><tiles:insertAttribute name="header" /></td>
</tr>
<tr>
<td width="140" valign="top"><tiles:insertAttribute name="menu" /></td>
<td valign="top" align="left"><tiles:insertAttribute name="body" /></td>
</tr>
<tr>
<td colspan="2"><tiles:insertAttribute name="footer" /></td>
</tr>
</table>
</body>
</html>

以tiles:getAsString插入templates.xml定義的value為文字
以tiles:insertAttribute插入templates.xml定義的value的網頁

五、
準備templates.xml中定義的其他jsp頁面。

六、
每個網頁一樣可以使用EL取得model的值
${test.name } ${test.passwd }

19 則留言:

  1. For the reason that the admin of this web site is
    working, no hesitation very quickly it will be renowned,
    due to its feature contents.

    Here is my site; disappointed

    回覆刪除
  2. You're so cool! I don't believe I've truly read through something like this before. So great to discover somebody with unique thoughts on this subject. Really.. thanks for starting this up. This site is one thing that is needed on the web, someone with a bit of originality!

    my weblog forthcoming
    my web site :: rankeronline.com

    回覆刪除
  3. Usually I don't read article on blogs, but I would like to say that this write-up very compelled me to take a look at and do it! Your writing taste has been amazed me. Thanks, quite nice post.

    Feel free to visit my blog ... http://testing.synergy-learning.com/user/profile.php?id=4639

    回覆刪除
  4. Hello There. I discovered your blog the usage of msn.
    This is a very smartly written article. I will be sure to bookmark it
    and return to read more of your useful info. Thank you for the post.
    I'll definitely comeback.

    Review my blog grass-roots

    回覆刪除
  5. After studying many of the sites on your website, I also like your blogging style. I have bookmarked it in my bookmark site list and will check again soon as well. Please take a look
    at my website and let me know what you think.
    movavi video editor
    typing master pro crack
    stardock fences crack
    audials one crack
    wondershare dr fone crack

    回覆刪除
  6. After looking over a number of the blog posts on your
    the site, I seriously like your way of writing a blog.
    I book-marked it to my bookmark website list and will be checking back soon. Take a look at my web site
    as well, and let me know what you think.
    autocad product key
    vmware workstation license key
    nitro pro crack
    sony vegas pro crack

    回覆刪除
  7. Woah! I’m really enjoying the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s very
    difficult to get that “perfect balance” between user friendliness and appearance.
    I must say that you’ve done a very good job with this.
    Also, the blog loads very quickly for me on Firefox.
    Exceptional Blog!
    avid pro tools crack
    idevice manager pro crack
    fonepaw data recovery crack
    wondershare recoverit crack
    avg internet security crack
    wondershare safeeraser crack

    回覆刪除

  8. Nicely written & done.
    I started writing in the last few days and realized that lot of
    writers simply rework old ideas but add very little of value.
    It’s fantastic to read an informative post of some genuine value to myself and your other followers.
    It is on the list of things I need to replicate as a new blogger.
    Reader engagement and content quality are king.
    Some terrific ideas; you’ve most certainly managed to get on my list of blogs to watch!
    Carry on the terrific work!
    Well done,
    Cheryl.
    phpstorm crack

    回覆刪除
  9. Thank you for the good writeup. It in fact was a
    amusement account it. Look advanced to far added agreeable from you!
    However, how could we communicate?
    manycam pro full crack
    turbo studio
    mailbird pro crack
    windows movie maker crack

    回覆刪除
  10. You can of course see your expertise in the work you write.
    The industry needs more passionate writers like you who are not afraid to speak up for their beliefs.
    Followed your heart

    advanced systemcare pro with crack
    flowjo
    utorrent pro
    moodle

    回覆刪除
  11. I have read this website's theory, it gives me information about this site and sent it to a lot of people to read it.

    media player classic home cinema with crack
    picsart photo studio pro crack
    acronis true image
    airmypc

    回覆刪除

  12. The post is quite informative as I could derive a lot on the topic by reading through it.
    Health Tips | Healthy Life Ideas | Health Care News in urdu

    回覆刪除
  13. Thanks for sharing this blog. A great information you shared through this blog.
    Dying Light 2 Crack
    Phoenix Point Crack
    Warparty Crack
    Battlefield Crack

    回覆刪除
  14. Hello there, wonderful blog! Does it take a lot of oof work to manage a business like this? I don't know much about programming,
    but I'd like to build my own blog in the near future. However, if you have any tips or tricks for new blog owners, please share.
    I realise this is off topic, but I had to inquire.
    Omnisphere Free Cracked
    Latest Version of Spyhunter
    M3 Data Recovery License Key Crack
    Pepakura Designer 4 Keycode
    pepakura designer 4 keycode

    回覆刪除
  15. Hello, Dear Thanks for sharing such great content with the US it’s really amazing content so please keep sharing. I also have something for you so please check out
    Adobe Lightroom Ios Macintosh Torrent

    回覆刪除
  16. It's great to have you here. I really like the colours and theme.
    Is this your website? I'd like to start working on my project as soon as possible.
    If you don't mind, I was curious to know where you got this or what theme you're using.
    Thank you.
    FL Studio Ios Mac Torrent

    回覆刪除
  17. Hello, Dear Thanks for sharing such great content with the US it’s really amazing content so please keep sharing. I also have something for you so please check out
    Cubase 8 Ios TorrentMac

    回覆刪除