Followed Freemarker documentation
Controller
public ModelAndView home() {
ModelAndView mav = new ModelAndView();
mav.addObject("users", userList);
mav.addObject("user", "Big Joe");
mav.addObject("title", "Testing initial page, using FreeMarker!");
try {
mav.addObject("doc", freemarker.ext.dom.NodeModel.parse (new File("C:\\Users\\Himanshuy\\Desktop\\2.xml")));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return mav;
}
Error
FreeMarker template error: For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this evaluated to a sequence+hash (wrapper: f.e.dom.NodeListModel): ==> header [in template "index.ftl" at line 13, column 3
FTL
<#assign header=doc.title >
${header}
XML
<book>
<title>Test Book</title>
<chapter>
<title>Ch1</title>
<para>p1.1</para>
<para>p1.2</para>
<para>p1.3</para>
</chapter>
<chapter>
<title>Ch2</title>
<para>p2.1</para>
<para>p2.2</para>
</chapter>
</book>
What is wrong with my approach?
那应该是doc.book.title
(你当然不需要#assign
),因为doc
是XML文档,它与最顶层的元素不同。
我猜你不应该在你的ftl中使用$ {header},请查看此页面以获取有关如何利用xml文件的更多示例: http : //freemarker.org/docs/xgui_imperative_learn.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.