如下配置,指定了三個視圖解析器,一個用於beetl頁面渲染,一個用於cms,采用了beetl技術,另外一個一些遺留的頁面采用jsp
<bean name="beetlConfig" class="org.beetl.ext.spring.BeetlGroupUtilConfiguration" init-method="init"> <property name="configFileResource" value="/WEB-INF/beetl.properties"/> </bean> <bean name="cmsbeetlConfig" class="org.beetl.ext.spring.BeetlGroupUtilConfiguration" init-method="init"> <property name="configFileResource" value="/WEB-INF/cms-beetl.properties"/> </bean> <!-- Beetl視圖解析器1 --> <bean name="beetlViewResolver" class="org.beetl.ext.spring.BeetlSpringViewResolver"> <!-- 多視圖解析器,需要設置viewNames和order --> <property name="viewNames"> <list> <value>/template/**</value> </list> </property> <property name="suffix" value=".btl"/> <property name="contentType" value="text/html;charset=UTF-8"/> <property name="order" value="0"/> <!-- 多GroupTemplate,需要指定使用的bean --> <property name="config" ref="beetlConfig"/> </bean> <!-- Beetl視圖解析器2 --> <bean name="cmsBeetlViewResolver" class="org.beetl.ext.spring.BeetlSpringViewResolver"> <!-- 多視圖解析器,需要設置viewNames和order --> <property name="viewNames"> <list> <value>/cmstemplate/**</value> </list> </property> <property name="contentType" value="text/html;charset=UTF-8"/> <property name="order" value="1"/> <!-- 多GroupTemplate,需要指定使用的bean --> <property name="config" ref="cmsbeetlConfig"/> </bean> <!-- JSP視圖解析器 --> <bean name="JSPViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 注意JSP的這個視圖解析器order必須在最后 --> <property name="order" value="256"/> <!-- beetl配置不支持前綴,這不同於jsp 和 freemaker --> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> <property name="contentType" value="text/html;charset=UTF-8"/> </bean>
Beetl視圖解析器屬性同spring自帶的視圖解析器一樣,支持contentType,order,prefix,suffix等屬性。
注意視圖解析器里屬性viewNames,這個用於判斷controller返回的path到底應該交給哪個視圖解析器來做。
-
以/template開頭的是beetlViewResolver來渲染。
-
以/cmstemplate是交給cmsBeetlViewResolver渲染。
-
如果都沒有匹配上,則是jsp渲染
如果你想更改此規則,你只能增加canHandle方法指定你的邏輯了。詳情參考org.springframework.web.servlet.view.UrlBasedViewResolver.canHandle
對於僅僅需要redirect和forward的那些請求,需要加上相應的前綴
-
以"redirect:"為前綴時:表示重定向,不產生BeetlView渲染模版,而直接通過Servlet的機制返回重定向響應.redirect:前綴后面的內容為重定向地址,可以采用相對地址(相對當前url),絕對地址(完整的url),如果采用/開頭的地址,會自動的在前面接上當前Web應用的contextPath,即contextPath為test的Web應用中使用redirect:/admin/login.html 實際重定向地址為 /test/admin/login.html
-
以"forward:"為前綴時:表示轉發,不產生BeetlView渲染模版。而是直接通過Servlet的機制轉發請求(關於轉發和重定向的區別,請自行查看Servlet API) forward:前綴后面的內容為轉發地址,一般都是以/開頭相對於當前Web應用的根目錄
其他集成需要注意的事項:
-
spring集成,請不要使用spring的 前綴配置,改用beetl的RESOURCE.ROOT 配置,否則include,layout會找不到模板
