[英]Building and Running Velocity Generating HTML
I'm very new to using Velocity. 我对使用Velocity非常陌生。 I am trying to use it to generate an HTML form.
我正在尝试使用它来生成HTML表单。 I am working in Eclipse.
我正在Eclipse中工作。 The following jars are in my classpath:
以下jar在我的类路径中:
velocity-dep-1.5.jar
commons-collections.jar
commons-lang.jar
log4j-1.2.8.jar
ant.jar
I am running an ant build file to build my project, but I don't see the HTML being generated. 我正在运行一个蚂蚁构建文件来构建我的项目,但是我看不到正在生成HTML。 Is there something I'm missing in order to get it to actually generate the HTML file?
为了使它真正生成HTML文件,我缺少什么吗? The tutorial I was following only has two files that I based mine off of.
我遵循的教程只有两个基于我的文件。 It worked for the author, but perhaps there is something else that I don't realize being new to using velocity.
它对作者有用,但也许还有其他我不了解使用速度的东西。 I have included my code and build script to make it easier to see if I'm missing something.
我已经包括了我的代码和构建脚本,以使其更容易查看是否缺少某些内容。 Thank you very much!
非常感谢你!
I have the template code for my form here ( form.vm
): 我在这里有我的表单的模板代码(
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>
Here is the java code I have to go along with that ( formDemo.java
) 这是我必须遵循的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 script ( build.xml
) 生成脚本(
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>
If you just want to see the html being generated, you'll have to print out the value of the StringWriter or write it to a file. 如果只想查看正在生成的html,则必须打印出StringWriter的值或将其写入文件。
Currently, unless there's code missing, you're just filling up the buffer. 当前,除非缺少代码,否则您只是在填充缓冲区。
Template template = Velocity.getTemplate("form.vm");
StringWriter writer = new StringWriter();
template.merge(context, writer);
template.merge(context, writer) just renders the template to the StringWriter object. template.merge(context,writer)只是将模板呈现给StringWriter对象。
I think your template should be as the following: 我认为您的模板应如下所示:
<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 $radios)
#formRowRadio("method" $map.method "true" $map.selected)<br/>
#end
</table>
<table>
#foreach($map in $textfields)
#formRowText($map.label $map.label $map.value)
#end
<tr><td> </td><td><input type="submit" name="submit" value="submit"></td></tr>
</table>
</form>
</body>
</html>
And you also need to effectively print the StringWriter to a File.html. 而且,您还需要有效地将StringWriter打印到File.html。
In the code you posted: there 2 ways to run velocity: 在您发布的代码中:有两种运行速度的方法:
ant form
but since you don't fill any velocity context: velocity won't be able to replace the variable in your template (ie the generated html won't contains dynamic data) ant form
但是由于您没有填写任何速度上下文:Velocity将无法替换模板中的变量(即,生成的html将不包含动态数据) I'm not sure what you need: 我不确定您需要什么:
Is it using velocity at build time ? 在构建时是否使用速度? In this case you have to run
ant form
and then ant run
(or add a depends="form" in comp target). 在这种情况下,您必须先运行
ant form
,然后再ant run
(或在comp目标中添加depends =“ form”)。 But you also have to provide a velocity context to velocity in the build.xml 但是您还必须在build.xml中提供一个速度上下文给速度
If you need to run velocity at runtime: just use a file writer to perfom the velocity merge. 如果需要在运行时运行速度:只需使用文件编写器执行速度合并。
It looks like your build script is not actually executing the Java code. 看起来您的构建脚本实际上并未执行Java代码。
Try using the java ant task, see here: 尝试使用Java ant任务,请参见此处:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.