多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 4.2 Spring 相关函数 Spring函数并没有内置,需要注册,如下 ```xml <bean name="beetlGroupUtilConfiguration" class="org.beetl.ext.spring.BeetlGroupUtilConfiguration" init-method="init"> <property name="functions"> <map> <!-- 定义SpEL方法 --> <entry key="spel"> <bean class="org.beetl.ext.spring.SpELFunction"/> </entry> </map> </property> <property name="functionPackages"> <map> <entry key="sputil"> <bean class="org.beetl.ext.spring.UtilsFunctionPackage"/> </entry> </map> </property> </bean> ``` spel(spelString, rootObject) SpEL方法传入一个Spring SpEL表达式以获取表达式结果,方法建议以函数的方式定义在BeetlGroupUtilConfiguration的functions中 spelString: SpEL表达式字符串,必传(否则返回null) rootObject: 作为spel的根对象(对应#root),可以是一个Map或Bean对象,默认取空Map。由于Beetl运行上下文无法直接获取模版局部变量的变量名,建议局部变量采用自定义Map的方式传入 - 列表筛选(以自定义Map为根对象传入局部变量) ```javascript <% var intArray = [12, 1, 2, 3]; %> ${spel('#root.intArray.?[#this>10]', {intArray: intArray})} ``` - 以Bean对象为根对象 ```javascript <% var now = date(); %> ${spel('#root.year + 1900', now)} ``` - 直接new对象 ```javascript ${spel('(new java.util.Date()).year + 1900')} ``` - 直接引用Spring Bean ```javascript ${spel('@testBean')} ``` - 默认变量 - \#root 表示SpEL的根对象, 由spel函数第二参数传入,默认是一个空map - \#context 表示Beetl执行上下文 - \#global 表示Beetl的共享变量Map,由于Beetl上下文无法获取临时变量名,临时变量建议使用根对象的方式传入 - \#ctxPath 表示Servlet Context Path(由Beetl WebRender提供) - \#servlet 可以从中获取到Servlet request,response,session原生实例(由Beetl WebRender提供) - \#parameter 表示请求参数Map(由Beetl WebRender提供) - \#request 表示请求对象(由Beetl WebRender提供) - \#session 表示会话域属性Map(由Beetl WebRender提供) sputil 提供了spring内置的一些功能,如 ```java // 测试source中是否包含了candidates的某个成员(相当于交集非空) sputil.containsAny(Collection<?> source, Collection<?> candidates) // 返回在source集合总第一个也属于candidates集的元素 sputil.findFirstMatch(Collection<?> source, Collection<?> candidates) // 测试指定文本是否匹配指定的Ant表达式(\*表达式), 多个表达式只要一个匹配即可 sputil.antMatch(String input, String... patterns) // 返回指定路径表示的文件的扩展名(不带点.) sputil.fileExtension(String path) // 忽略大小写的endsWith sputil.endsWithIgnoreCase(String input, String suffix) // 忽略大小写的startsWith sputil.startsWithIgnoreCase(String input, String prefix) // 测试输入值是否为空白, null视为空白, 无视字符串中的空白字符 sputil.isBlank(String input) // 首字母大写转换 sputil.capitalize(String input) // 首字母小写转换 sputil.uncapitalize(String input) // 在集合或数组元素之间拼接指定分隔符返回字符串 // null表示空集, 其他类型表示单元素集合 sputil.join(Object collection, String delim) // 同上, 只是会在最后结果前后加上前缀和后缀 // 注意这个函数名叫做joinEx sputil.joinEx(Object collection, String delim, String prefix, String suffix) // 对文本进行html转义 sputil.html(String input) // 对文本进行javascript转义 sputil.javaScript(String input) ```