## 3.4 SpringMVC集成
適用于Spring 6+JDK17
```xml
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-spring</artifactId>
<version>3.14.1.RELEASE</version>
</dependency>
```
適用于Spring 5以下
```xml
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-spring-classic</artifactId>
<version>3.14.1.RELEASE</version>
</dependency>
```
### 3.4.1 普通集成
Srping5 需要做如下配置即可包名是org.beetl.ext.spring6)
```xml
<bean id="beetlConfig" class="org.beetl.ext.spring6.BeetlGroupUtilConfiguration" init-method="init"/>
<bean id="viewResolver" class="org.beetl.ext.spring6.BeetlSpringViewResolver">
<property name="contentType" value="text/html;charset=UTF-8"/>
</bean>
```
或者Spring5以下(包名是org.beetl.ext.spring)
```xml
<bean id="beetlConfig" class="org.beetl.ext.spring.BeetlGroupUtilConfiguration" init-method="init"/>
<bean id="viewResolver" class="org.beetl.ext.spring.BeetlSpringViewResolver">
<property name="contentType" value="text/html;charset=UTF-8"/>
</bean>
```
同其他集成方式一樣,模板的配置將放在beetl.properties中。
如果想獲取GroupTemplate,可以調用如下代碼
```java
BeetlGroupUtilConfiguration config = (BeetlGroupUtilConfiguration) this.getApplicationContext().getBean("beetlConfig");
GroupTemplate group = config.getGroupTemplate();
```
Controller代碼如下:
```java
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index(HttpServletRequest req) {
ModelAndView view = new ModelAndView("/index");
//total 是模板的全局變量,可以直接訪問
view.addObject("total",service.getCount());
return view;
}
```
[http://git.oschina.net/xiandafu/springbeetlsql](http://git.oschina.net/xiandafu/springbeetlsql)?有完整例子
通常可以把模板放到WEB-INF目錄下,除了可以配置beetl.propertis 外,還可以使用Spring配置
```xml
<bean id="beetlConfig" class="org.beetl.ext.spring." init-method="init">
<property name="root" value="/WEB-INF/templates"/>
</bean>
```
### 3.5 高級集成
spring集成還允許注冊被spring容器管理的Function,Tag等,也允許配置多個視圖解析器等功能
```xml
<bean name="beetlConfig" class="org.beetl.ext.spring.BeetlGroupUtilConfiguration" init-method="init">
<property name="configFileResource" value="/WEB-INF/beetl.properties"/>
<property name="functions">
<map>
<entry key="testFunction" value-ref="testFunction"/>
</map>
</property>
<property name="functionPackages">
<map>
<entry key="fp" value-ref="testFunctionPackage"/>
</map>
</property>
<property name="tagFactorys">
<map>
<entry key="html.output" value-ref="testTagFactory"/>
<entry key="html.output2" value-ref="testTagFactory2"/>
</map>
</property>
</bean>
<bean name="testTagFactory" class="org.beetl.ext.spring.SpringBeanTagFactory">
<property name="name" value="testTag"/>
</bean>
<bean name="testTagFactory2" class="org.beetl.ext.spring.SpringBeanTagFactory">
<property name="name" value="testTag2"/>
</bean>
<bean name="beetlViewResolver" class="org.beetl.ext.spring.BeetlSpringViewResolver">
<property name="config" ref="beetlConfig"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
</bean>
```
如上圖所示,BeetlGroupUtilConfiguration有很多屬性,列舉如下
- configFileResource 屬性指定了配置文件所在路徑,如果不指定,則默認在classpath下
- functions 指定了被spring容器管理的function,key為注冊的方法名,value-ref 指定的bean的名稱
- functionPackages,指定了被spring容器管理的functionPackage,key為注冊的方法包名,value-ref 指定的bean的名稱
- tagFactorys ,注冊tag類,key是tag類的名稱,value-ref指向一個org.beetl.ext.spring.SpringBeanTagFactory實例,該子類是一個Spring管理的Bean。屬性name對應的bean就是tag類。需要注意,由于Tag是有狀態的,因此,必須申明Scope為 "prototype"。如代碼:
```java
@Service
@Scope("prototype")
public class TestTag extends Tag {
}
```
- typeFormats: 同functions,參數是 Map<Class<?>, Format>,其中key為類型Class
- formats:同functions,參數是 Map<String, Format>,其中key為格式化函數名
- virtualClassAttributes 同functions,參數Map<Class<?>, VirtualClassAttribute>,其中key為類型Class
- virtualAttributeEvals ,類型為List<VirtualAttributeEval>
- resourceLoader,資源加載器 ,值是 實現ResourceLoader的一個Bean
- errorHandler ,錯誤處理,值是實現ErrorHandler的一個Bean
- sharedVars,同functions,類型是Map<String, Object>,可以在此設置共享變量
- configProperties,類型是Properties,可以覆蓋配置文件的某些屬性
如下配置,指定了三個視圖解析器,一個用于beetl頁面渲染,一個用于cms,采用了beetl技術,另外一個是一些遺留的頁面采用jsp
```xml
<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渲染
你也可以通過擴展名來幫助Spring決定采用哪種視圖解析器,比如
~~~xml
<property name="viewNames">
<list>
<value>/**/*.btl</value>
</list>
</property>
~~~
如果你想更改此規則,你只能增加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會找不到模板
- 如果根目錄不是默認目錄,可以通過添加root屬性
~~~xml
<bean name="cmsbeetlConfig" class="org.beetl.ext.spring.BeetlGroupUtilConfiguration" init-method="init">
<property name="root" value="/WEB-INF/views"/>
</bean>
~~~
- Beetl 3 中文文檔
- 第一部分 基礎用法
- 1.1 安裝
- 1.2 快速開始
- 1.3 模板基礎配置
- 1.4 模板加載器
- 1.5 定界符與占位符
- 1.6 注釋
- 1.7 變量定義
- 1.8 屬性
- 1.9 數學表達式
- 1.10 循環語句
- 1.11 條件語句
- 1.12 異常捕獲
- 1.13 虛擬屬性
- 1.14 函數調用
- 1.15 安全輸出(重要)
- 1.16 輸出格式化
- 1.17 標簽
- 1.18 調用Java方法與屬性
- 1.19 嚴格MVC控制
- 1.20 指令
- 1.21 錯誤處理
- 1.22 Beetl小工具
- 1.23 Escape
- 第二部分 高級用法
- 2.1 配置GroupTemplate
- 2.2 自定義方法
- 2.3 自定義格式化函數
- 2.4 自定義標簽
- 2.5 自定義虛擬屬性
- 2.6 使用額外的資源加載器
- 2.7 自定義資源加載器
- 2.8 使用CompositeResourceLoader
- 2.9 自定義錯誤處理器
- 2.10 自定義安全管理器
- 2.11 注冊全局共享變量
- 2.12 自定義布局
- 2.13 性能優化
- 2.14 定制輸出
- 2.15 定制模板引擎
- 2.16 直接運行Beetl腳本
- 2.17 模板校驗
- 第三部分 Web 集成
- 3.1 Web提供的全局變量
- 3.2 集成技術開發指南
- 3.3 Servlet集成
- 3.4 SpringMVC集成
- 3.5 Spring Boot集成
- 3.6 Jodd集成
- 3.7 JFinal4 集成方案
- 3.8 Nutz集成
- 3.9 Struts2集成
- 3.10 整合ajax的局部渲染技術
- 3.11 在頁面輸出錯誤提示信息
- 附錄
- 4.1 內置方法
- 4.2 Spring相關函數
- 4.3 Spring security
- 4.4 shiro
- 4.5 內置格式化方法
- 4.6 內置標簽函數
- 4.7 內置html標簽
- 4.8 性能優化
- 4.9 Eclipse 插件
- 4.10 性能測試對比