其實我不善于寫作,有些東西僅僅是寫下來.還可以提醒自己.
人生"入"夢,各位一定要保重身體.
注意:FreeMarkerPageFilter是2.2beta發布后才加到CVS上的,所以從CVS獲取新的代碼才能看到這個filter,同目錄下還有一個VelocityPageFilter.
FreeMarkerPageFilter以及VelocityPageFilter都是Sitemesh頁面裝飾器中PageFilter的擴展,有了它,可以直接使用FreeMarker的ftl作為裝飾頁面,可以不在使用SiteMesh帶的FreemarkerDecoratorServlet了,而且還可以享受Action中的ValueStack了.
不過我不推薦在裝飾頁面中使用ValueStack中的值,因為裝飾頁面對應的Action是不確定的,而且也可能修飾的是一個普通頁面,不是一個Action.
我們要有一個Action及其結果頁面,或者一個簡單的頁面,這些不在贅述,自己看自己的了.
首先我們有一個裝飾頁面,示例如下(main.ftl):
<html> <head> <title>JScud Develop:${page.title}</title> ${page.head} </head> <body> ${page.body} <br><br> From: ${base} by <@ww.text name="copyright"/> </body> </html> |
我們可以看到,其中的標記都有page前綴,而不是直接使用body,title等,原因是這個FreeMarkerPageFilter只把page放到了FreeMarker的model中,如果你還是想遵循原來的習慣,那么自己繼承一下FreeMarkerPageFilter即可,非常簡單,然后把body,title,head放到model中即可.不過我覺得現在也挺好,省得變量太短和別的混淆.
SiteMesh的decorators.xml示例如下:
<?xml version="1.0" encoding="ISO-8859-1"?> <decorators defaultdir="/decorators"> <decorator name="main" page="main.ftl"> <pattern>/*</pattern> </decorator> </decorators> |
我們還需要在web.xml中配置過濾器,在這個地方,我走了彎路,因為沒有文檔,結果沒有注意到 ActionContextCleanUp 這個Filter,自己還自作聰明修改了一下FreeMarkerPageFilter,后來才發現ActionContextCleanUp這個Filter的作用,這都是沒有仔細看源碼(JavaDoc)的后果啊.
在不使用SiteMesh的時候,為了使用WebWork,我們只需要在web.xml配置FilterDispatcher一個過濾器即可,閱讀一下FilterDispatcher的JavaDoc和源碼,我們可以看到它調用了:
finally { ActionContextCleanUp.cleanUp(req); } |
而且JavaDoc中也提到看ActionContextCleanUp的文檔,那我們就去看吧(我沒看所以...)
在ActionContextCleanUp中,有這樣的代碼:
req.setAttribute(CLEANUP_PRESENT, Boolean.TRUE); |
如果FilterDispatcher檢測到這個屬性,就不會清除ActionContext中的內容了,而由ActionContextCleanUp后續的代碼來清楚,保證了一系列的Filter訪問正確的ActionContext.
文檔中提到,如果用到SiteMesh的Filter或者其他類似Filter,那么設置順序是:
- ActionContextCleanUp filter
- SiteMesh filter
- FilterDispatcher
所以最后我們的web.xml應該類似這樣:
<filter> <filter-name>ActionContextCleanUp</filter-name> <filter-class>com.opensymphony.webwork.dispatcher.ActionContextCleanUp</filter-class> </filter>
<filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.webwork.sitemesh.FreeMarkerPageFilter</filter-class> </filter>
<filter> <filter-name>webwork</filter-name> <filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class> </filter>
<filter-mapping> <filter-name>ActionContextCleanUp</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<filter-mapping> <filter-name>webwork</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
發布程序,運行結果正常.在Action改變Locale,裝飾頁面也獲取了正確的Locale,OK,大功告成...嘿嘿
BTW:如果你使用jsp做裝飾頁面,使用SiteMesh自己的PageFilter即可,不過也要配置ActionContextCleanUp哦. :)
BTW 2:
其實,我還想寫一篇AJAX的,不過總是懶得寫那...
文件上傳的攔截器有些Bug,而且處理流程必須結合實際才行,所以也不想寫了...
除經特別注明外,本文章版權歸JScud Develop團隊或其原作者所有.
轉載請注明作者和來源. scud(飛云小俠) 歡迎訪問 JScud Develop