【发布时间】:2013-09-27 15:27:20
【问题描述】:
我对使用 Velocity 很陌生。我正在尝试使用它来生成 HTML 表单。我在 Eclipse 中工作。以下 jar 在我的类路径中:
velocity-dep-1.5.jar
commons-collections.jar
commons-lang.jar
log4j-1.2.8.jar
ant.jar
我正在运行一个 ant 构建文件来构建我的项目,但我没有看到正在生成的 HTML。为了让它真正生成 HTML 文件,我有什么遗漏吗?我正在关注的教程只有两个文件,我的基础是我的。它对作者有用,但也许还有其他一些我没有意识到使用速度的新手。我已经包含了我的代码和构建脚本,以便更容易查看我是否遗漏了什么。非常感谢!
我的表单模板代码在这里 (form.vm):
<html>
<head>
<title> My Form </title>
</head>
<body>
#if ($fieldErrors)
#foreach ($error in $fieldErrors)
$error<br>
#end
#end
#if ($actionErrors)
#foreach ($error in $actionErrors)
$error<br>
#end
#end
<form name="edit" action="edit.action" method="post">
<table>
<tr><td>Testing</td><td>123</td></tr>
#foreach($map in $radioList)
#formRowRadio("method" $method "true" $selected)<br/>
#end
</table>
<table>
#foreach($map in $textList)
#formRowText($label $label $value)
#end
<tr><td> </td><td><input type="submit" name="submit" value="submit"></td></tr>
</table>
</form>
</body>
</html>
这是我必须使用的 java 代码 (formDemo.java)
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.Template;
public class formDemo {
public static void main ( String[] args )
throws Exception {
Velocity.init();
ArrayList radioList = new ArrayList();
Map map = new HashMap();
map.put("method", "Yes");
map.put("selected", false);
radioList.add(map);
map = new HashMap();
map.put("method", "No");
map.put("selected", false);
radioList.add(map);
/*
* add the list to a VelocityContext
*/
VelocityContext context = new VelocityContext();
context.put("radios", radioList);
ArrayList textList = new ArrayList();
map = new HashMap();
map.put("label", "FirstName");
map.put("value", "");
textList.add(map);
map = new HashMap();
map.put("label", "LastName");
map.put("value", "");
textList.add(map);
context.put("textfields", textList);
Template template = Velocity.getTemplate("form.vm");
StringWriter writer = new StringWriter();
template.merge(context, writer);
}
}
构建脚本 (build.xml)
<?xml version='1.0' encoding='UTF-8'?>
<project name="velocityTemplate" default="jar" basedir=".">
<property name='cls' location='${basedir}/classes'/>
<property name='dat' location='${basedir}/data'/>
<property name='gen' location='${basedir}/gen'/>
<property name='lib' location='${basedir}/lib'/>
<property name='src' location='${basedir}/src'/>
<property name='tmp' location='${basedir}/templates'/>
<path id='project.classpath'>
<pathelement location='${cls}'/>
<fileset dir='${lib}' includes='*.jar'/>
</path> <target name='clean' description='Clean.'>
<delete dir='${cls}'/>
<delete dir='${gen}'/>
</target>
<target name='comp' description='Compile the source.'>
<mkdir dir='${cls}'/>
<javac srcdir='${src}' destdir='${cls}' classpathref='project.classpath' fork='true'/>
</target>
<target name='jar' depends='comp' description='JAR the application.'>
<jar destfile='${ant.project.name}.jar' update='false' filesonly='true' index='true'>
<fileset dir='${cls}'/>
<fileset dir='${src}'/>
</jar>
</target>
<target name='run' depends='jar' description='Run the application.'>
<path id='velocityTemplate.classpath'>
<pathelement location='${ant.project.name}.jar'/>
<fileset dir='${lib}' includes='*.jar'/>
</path>
<taskdef classpathref='velocityTemplate.classpath'/>
<mkdir dir='${gen}'/>
<enumerator outputPath='${gen}' inputPath='${dat}' templateFile='${tmp}/form.vm'/>
</target>
<target name='form' description='Creates form'>
<path id='velocityTemplate.classpath'>
<pathelement location='${basedir}/velocityTemplate.jar'/>
<pathelement location='${lib}/velocity-dep-1.5.jar'/>
</path>
<taskdef classpathref='velocityTemplate.classpath'/>
<velocityTemplate outputPath='${basedir}/src' templateFile='${basedir}/form.vm'/>
</target>
</project>
【问题讨论】:
-
更新: 我从构建脚本中删除了目标
<jar>,并将目标<run>修改为只有<java classname='my.classname.formDemo' classpathref='project.classpath fork='true'>以及我拥有velocityTemplate.classpath的所有地方,我改为project.classpath。此外,我在formDemo.java中修改了Velocity.getTemplate()以包含我的模板的完整路径名。一旦我进行了这些更改,它就起作用了。我的 HTML 搞砸了,但至少现在它生成了 HTML 文件。注意:我一直在为#formRowRadio 和#formRowText 使用宏,但我也把它去掉了。
标签: java html eclipse ant velocity