本案例用到了
字符串模板资源加载器(StringTemplateResourceLoader)
Classpath资源模板加载器(ClasspathResourceLoader)
ServletGroupTemplate 跳到静态页面
代码下载地址:https://github.com/prettyRain/beetlDemo.git
pom配置 重点beetl坐标
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dianshang</groupId>
<artifactId>beetlDemo</artifactId>
<packaging>war</packaging>
<version>0.1-SNAPSHOT</version>
<name>beetl Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>ndct-central</id>
<name>ndct artifactory</name>
<url>http://maven.ct/artifactory/libs-release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>ndct-snapshot</id>
<name>ndct snapshot artifactory</name>
<url>http://maven.ct/artifactory/libs-snapshot/</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>
</dependencies>
<build>
<!--打包后的项目名称-->
<finalName>beetlDemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<fork>true</fork>
<compilerArguments>
<nowarn />
</compilerArguments>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>
</project>
adminServlet
/**
* Created by prettyRain
*/
public class AdminServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//Classpath资源模板加载器
//还有种常情况下,模板资源是打包到jar文件或者同Class放在一起,因此,可以使用ClasspathResourceLoader来加载模板实例
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("com/dianshang");
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template template = gt.getTemplate("/hello.txt");
template.binding("titlea","这是测试标题");
template.binding("name","这是测试名称adminServlet");
String render = template.render();
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.println(render);
}
}
indexServlet
/**
* Created by prettyRain
*/
public class IndexServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//new 一个字符串模板资源加载器
StringTemplateResourceLoader stringTemplateResourceLoader = new StringTemplateResourceLoader();
/* 使用Beetl默认的配置。
* Beetl可以使用配置文件的方式去配置,但由于此处是直接上手的例子,
* 我们不去管配置的问题,只需要基本的默认配置就可以了。
*/
Configuration configuration = Configuration.defaultConfiguration();
//Beetl的核心GroupTemplate
GroupTemplate groupTemplate = new GroupTemplate(stringTemplateResourceLoader,configuration);
String testTemplate="<html>\n" +
"<head>\n" +
"\t<title>${title}</title>\n" +
"</head>\n" +
"<body>\n" +
"\t<h1>${name}</h1>\n" +
"</body>\n" +
"</html>";
Template template = groupTemplate.getTemplate(testTemplate);
template.binding("title","这是测试标题");
template.binding("name","这是测试名称indexServlet");
String render = template.render();
//也可以使用map绑定
/*Map<String,String> map=new HashMap<String,String>()
map.put("name", "beetl");
map.put("title","This is a test template Email.");
template.binding(map);*/
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.println(render);
}
adminServlet
/**
* Created by prettyRain
*/
public class MainServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
req.setAttribute("title","这是测试title");
req.setAttribute("name","这是测试名称mainServlet");
String[] arrstr = {"1","2","3","4"};
req.setAttribute("arrstr",arrstr);
ServletGroupTemplate.instance().render("/view/beetlPage.html",req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
beetlPage.html
<%
var a = 2;
var b = 3;
var c = a+b;
%>
<html>
<body>
<h2>Hello World!</h2>
</body>
${name}
${c}
<ul>
<!--遍历-->
<%for(str in arrstr){%>
<li>${str}</li>
<%}%>
</ul>
</html>
网友评论