0

我正在尝试在 Freemarker 模板中实现 JSTL。为此,我获得了一个示例 freemarker 模板示例,并尝试合并 JSTL 命名空间并尝试执行它。这是我的代码现在的样子。

<html>
<head><title>ViralPatel.net - FreeMarker Spring MVC Hello World</title>
<#assign c=JspTaglibs["http://java.sun.com/jstl/core"]/>
<style>
body, input {
font-family: Calibri, Arial;
margin: 0px;
padding: 0px;
}
#header h2 {
color: white;
background-color: #3275A8;
height: 50px;
padding: 5px 0 0 5px;
font-size: 20px;
}

.datatable {margin-bottom:5px;border:1px solid #eee;border-      collapse:collapse;width:400px;max-width:100%;font-family:Calibri}
.datatable th {padding:3px;border:1px solid #888;height:30px;background-color:#B2D487;text-align:center;vertical-align:middle;color:#444444}
.datatable tr {border:1px solid #888}
.datatable tr.odd {background-color:#eee}
.datatable td {padding:2px;border:1px solid #888}
#content { padding 5px; margin: 5px; text-align: center}

fieldset { width: 300px; padding: 5px; margin-bottom: 0px; }
legend { font-weight: bold; }
</style>

<body>
<div id="header">
<H2>
<a href="http://viralpatel.net"><img height="37" width="236" border="0px" src="http://viralpatel.net/blogs/wp-content/themes/vp/images/logo.png" align="left"/></a>
FreeMarker Spring MVC Hello World
</H2>
 </div>

<div id="content">
<c:set var="Salary" value="4000/>
<c:out value="${Salary}/>

 <fieldset>
<legend>Add User</legend>
 <form name="user" action="add.html" method="post">
Firstname: <input type="text" name="firstname" />   <br/>
Lastname: <input type="text" name="lastname" /> <br/>


<input type="submit" value="   Save   " />
  </form>
  </fieldset>
  <br/>
  <table class="datatable">
<tr>
    <th>Firstname</th>  <th>Lastname</th>
</tr>
    <#list model["userList"] as user>
<tr>
    <td>${user.firstname}</td> <td>${user.lastname}</td>
</tr>
    </#list>

  </table>

</div>  
 </body>
</html>  

我知道问题出在我添加的新代码上,即薪水,我看到以下错误。

freemarker.core.InvalidReferenceException: Expression Salary is undefined on line 62, column 5 in index.ftl.
at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)

我究竟做错了什么?

谢谢尼基尔

4

1 回答 1

1

您做错的事情是不了解 Freemarker 的工作原理,并认为它在某种程度上完全无法与 JSP 一起使用。据我所知,当 Freemarker 已经这样做时,您已经导入了 JSTL 以便设置和输出变量值。

替换这个:

<c:set var="Salary" value="4000/>
<c:out value="${Salary}/>

...有了这个:

<#assign Salary = 4000>
${Salary}

此外,摆脱 JSTL 标签库,学习如何使用 Freemarker。你会快乐很多。

于 2013-04-09T19:36:00.613 回答