Note that there are some explanatory texts on larger screens.

plurals
  1. POBuilding and Running Velocity Generating HTML
    primarykey
    data
    text
    <p>I'm very new to using Velocity. I am trying to use it to generate an HTML form. I am working in Eclipse. The following jars are in my classpath:</p> <pre><code>velocity-dep-1.5.jar commons-collections.jar commons-lang.jar log4j-1.2.8.jar ant.jar </code></pre> <p>I am running an ant build file to build my project, but I don't see the HTML being generated. Is there something I'm missing in order to get it to actually generate the HTML file? 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!</p> <p>I have the template code for my form here (<code>form.vm</code>):</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt; My Form &lt;/title&gt; &lt;/head&gt; &lt;body&gt; #if ($fieldErrors) #foreach ($error in $fieldErrors) $error&lt;br&gt; #end #end #if ($actionErrors) #foreach ($error in $actionErrors) $error&lt;br&gt; #end #end &lt;form name="edit" action="edit.action" method="post"&gt; &lt;table&gt; &lt;tr&gt;&lt;td&gt;Testing&lt;/td&gt;&lt;td&gt;123&lt;/td&gt;&lt;/tr&gt; #foreach($map in $radioList) #formRowRadio("method" $method "true" $selected)&lt;br/&gt; #end &lt;/table&gt; &lt;table&gt; #foreach($map in $textList) #formRowText($label $label $value) #end &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="submit" name="submit" value="submit"&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here is the java code I have to go along with that (<code>formDemo.java</code>)</p> <pre><code>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); } } </code></pre> <p>Build script (<code>build.xml</code>)</p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt; &lt;project name="velocityTemplate" default="jar" basedir="."&gt; &lt;property name='cls' location='${basedir}/classes'/&gt; &lt;property name='dat' location='${basedir}/data'/&gt; &lt;property name='gen' location='${basedir}/gen'/&gt; &lt;property name='lib' location='${basedir}/lib'/&gt; &lt;property name='src' location='${basedir}/src'/&gt; &lt;property name='tmp' location='${basedir}/templates'/&gt; &lt;path id='project.classpath'&gt; &lt;pathelement location='${cls}'/&gt; &lt;fileset dir='${lib}' includes='*.jar'/&gt; &lt;/path&gt; &lt;target name='clean' description='Clean.'&gt; &lt;delete dir='${cls}'/&gt; &lt;delete dir='${gen}'/&gt; &lt;/target&gt; &lt;target name='comp' description='Compile the source.'&gt; &lt;mkdir dir='${cls}'/&gt; &lt;javac srcdir='${src}' destdir='${cls}' classpathref='project.classpath' fork='true'/&gt; &lt;/target&gt; &lt;target name='jar' depends='comp' description='JAR the application.'&gt; &lt;jar destfile='${ant.project.name}.jar' update='false' filesonly='true' index='true'&gt; &lt;fileset dir='${cls}'/&gt; &lt;fileset dir='${src}'/&gt; &lt;/jar&gt; &lt;/target&gt; &lt;target name='run' depends='jar' description='Run the application.'&gt; &lt;path id='velocityTemplate.classpath'&gt; &lt;pathelement location='${ant.project.name}.jar'/&gt; &lt;fileset dir='${lib}' includes='*.jar'/&gt; &lt;/path&gt; &lt;taskdef classpathref='velocityTemplate.classpath'/&gt; &lt;mkdir dir='${gen}'/&gt; &lt;enumerator outputPath='${gen}' inputPath='${dat}' templateFile='${tmp}/form.vm'/&gt; &lt;/target&gt; &lt;target name='form' description='Creates form'&gt; &lt;path id='velocityTemplate.classpath'&gt; &lt;pathelement location='${basedir}/velocityTemplate.jar'/&gt; &lt;pathelement location='${lib}/velocity-dep-1.5.jar'/&gt; &lt;/path&gt; &lt;taskdef classpathref='velocityTemplate.classpath'/&gt; &lt;velocityTemplate outputPath='${basedir}/src' templateFile='${basedir}/form.vm'/&gt; &lt;/target&gt; &lt;/project&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload