Java 模板引擎 jetbrick-template在springmvc中配置使用
左丘照
2023-12-01
jetbrick-template 是一个新一代 Java 模板引擎,具有高性能和高扩展性。 适合于动态 HTML 页面输出或者代码生成,可替代 JSP 页面或者 Velocity 等模板。 指令和 Velocity 相似,表达式和 Java 保持一致,易学易用。
文章最后附简单springmvc集成jetbrick.template模版项目一个。
1.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- 加载jetbrick-template配置 -->
<context-param>
<param-name>jetbrick-template-config-location</param-name>
<param-value>/WEB-INF/jetbrick-template.properties</param-value>
</context-param>
<listener>
<listener-class>
jetbrick.template.web.JetWebEngineLoader
</listener-class>
</listener>
<!-- Spring MVC 核心控制器 -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
二、spring-mvc.xml配置
<context:component-scan
base-package="jetbrick.template.samples.springmvc.controller"/>
<mvc:annotation-driven />
<bean class="jetbrick.template.web.springmvc.JetTemplateViewResolver">
<property name="suffix" value=".jetx" />
<property name="contentType" value="text/html; charset=UTF-8" />
<property name="order" value="9999" />
</bean>
三、jetbrick-template.properties配置
import.packages = jetbrick.template.samples.dao, jetbrick.template.samples.model
import.autoscan = true
import.autoscan.packages = jetbrick.template.samples
template.reloadable = true
compile.debug = true
input.encoding=UTF-8
output.encoding=UTF-8
template.path=/WEB-INF/jetx/
template.loader=jetbrick.template.resource.loader.FileSystemResourceLoader
template.suffix=.jetx
四、使用
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>jetbrick-template samples</title>
</head>
<body>
#define(UserInfo author)
<div>Welcome, You are ${author.name}!</div>
<br/>
<table border="1" width="600">
<tr>
<td>ID</td>
<td>书名</td>
<td>作者</td>
<td>价格</td>
<td>出版时间</td>
</tr>
#for(BookInfo book: author.getBooks())
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.getAuthorUser().name}</td>
<td>${book.price.format()}</td>
<td>${book.publicationDate.format("yyyy-MM-dd")}</td>
</tr>
#else
<tr>
<td colspan="5" height="100">Sorry! 还没出版过任何数据哦!</td>
</tr>
#end
</table>
<br/>
[url=users]点击这里返回用户列表[/url]
</br/>
#include("includes/footer.jetx")
</body>
</html>