🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 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> ~~~