本文采用简单的servlet作为后台处理数据的工具,前台使用freemarker的ftl模板作为输出工具,简单说明怎样将封装有实体类对象的List集合注入到ftl模板中并且成功的在遍历显示出来,之前在网上找了很多这方面的资料,但是都没有解决这个问题,所以自己就从头认真的研读的一番freemarker的API文档,阅读了相关的类和接口的功能说明,终于找到了突破口,在这里写出来供和我有相同经历的孩纸(初学者)使用:
首先看我写的domain实体类:
package ;
public class Animals{
private String name;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
= price;
}
}
接着看我写AnimalsDao中查询所有Animals对象的并返回List<Animals>数据的信息:
package ;
import ;
import ;
import ;
public class AnimalsDao {
public List<Animals> getAnimals(){
List<Animals> list=new ArrayList<Animals>();
for(int i=0;i<10;i++){
Animals animals=new Animals();
("animals:"+i);
("price:"+i);
(animals);
}
return list;
}
}
下面是自己第一次写的ListServlet代码:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class ListServlet extends HttpServlet {
/**
* 测试freemarker模板的list 指令
*/
private static final long serialVersionUID = 1L;
// 负责管理FreeMarker模板的Configuration实例
private Configuration cfg = null;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
// 创建一个FreeMarker实例
cfg = new Configuration();
// 指定FreeMarker模板文件的位置
(getServletContext(),
"/WEB-INF/templates");
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
AnimalsDao dao=new AnimalsDao();
List<Animals> list=();
// 建立数据模型
Map root = new HashMap();
("animals", list);
// 获取模板文件
Template t = ("");
// 使用模板文件的Charset作为本页面的charset
// 使用text/html MIME-type
("text/html; charset=" + ());
Writer out = ();
// 合并数据模型和模板,并将结果输出到out中
try {
(root, out,ObjectWrapper.BEANS_WRAPPER); // 往模板里写数据
} catch (TemplateException e) {
();
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
(request, response);
}
}
下面是模板代码:
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<p>We have these animals:
<table border=1>
<tr><th>Name<th>Price
<#list animals as a>
<tr><td>${}<td>${} Euros
</#list>
</table>
</body>
</html>
但是运行时会出现下面的异常信息,着实让人很头疼:
: Don't know how to present an object of this type to a template:
at (:139)
at (:116)
at (:131)
at (:197)
at $(:163)
at (:316)
at (:94)
at (:180)
at (:78)
at (:180)
at (:91)
at (:180)
at (:166)
at (:238)
这里我声明一下,数据的封装和传递绝对没有问题,这个异常信息的关键是:
Don't know how to present an object of this type to a template:
无法引述动物类对象到模版,也就是说模板不认识list里面的数据是Animals对象,所以无法通过来访问属性对象
到这里我想大家明白我说的是什么意思了吧,通过查询API文档,可以容易看到()函数有两种形式,一个是含有两个参数的,而另一个则含有三个参数,他们分别为:
ObjectWrapper
that exposes the object methods and JavaBeans properties as hash elements, and has custom handling for Java
Map
-s,
ResourceBundle
-s, etc. It doesn't treat
org.
-s and Jython objects specially, however.
SimpleXXX
wrappers only. It behaves like the
DEFAULT_WRAPPER
, but for objects that it does not know how to wrap as a
SimpleXXX
it throws an exception. It makes no use of reflection-based exposure of anything, which may makes it a good candidate for security-restricted applications.
// 合并数据模型和模板,并将结果输出到out中
try {
(root, out,ObjectWrapper.BEANS_WRAPPER); // 往模板里写数据
} catch (TemplateException e) {
();
}
以下是改正后的运行效果:
首先看我写的domain实体类: