【问题标题】:Building and Running Velocity Generating HTML构建和运行 Velocity 生成 HTML
【发布时间】: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>&nbsp;</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>

【问题讨论】:

  • 更新: 我从构建脚本中删除了目标 &lt;jar&gt;,并将目标 &lt;run&gt; 修改为只有 &lt;java classname='my.classname.formDemo' classpathref='project.classpath fork='true'&gt; 以及我拥有 velocityTemplate.classpath 的所有地方,我改为project.classpath。此外,我在formDemo.java 中修改了Velocity.getTemplate() 以包含我的模板的完整路径名。一旦我进行了这些更改,它就起作用了。我的 HTML 搞砸了,但至少现在它生成了 HTML 文件。注意:我一直在为#formRowRadio 和#formRowText 使用宏,但我也把它去掉了。

标签: java html eclipse ant velocity


【解决方案1】:

如果您只想查看生成的 html,则必须打印出 StringWriter 的值或将其写入文件。

目前,除非缺少代码,否则您只是在填充缓冲区。

    Template template = Velocity.getTemplate("form.vm");
    StringWriter writer = new StringWriter();
    template.merge(context, writer);

template.merge(context, writer) 只是将模板呈现给 StringWriter 对象。

【讨论】:

  • 好的,有道理。我将 formDemo.java 的最后一位更改为这个,但是在我构建之后,我在我的任何目录中都看不到 .html 文件。 context.put("textfields", textList);Template template = Velocity.getTemplate("form.vm");FileWriter fwriter = new FileWriter("form.html");StringWriter writer = new StringWriter();template.merge(context, writer);fwriter.write(writer.toString());fwriter.close();
  • 实际上,我认为这是我的构建脚本的问题,而不是写入文件部分。
  • 能否在java代码中加入一些日志或sysout语句来确认ant脚本确实在运行类?
【解决方案2】:

我认为你的模板应该如下:

<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>&nbsp;</td><td><input type="submit" name="submit" value="submit"></td></tr>
    </table>

</form>

</body>
</html>

您还需要有效地将 StringWriter 打印到 File.html。

【讨论】:

    【解决方案3】:

    在您发布的代码中:有两种运行速度的方法:

    • 运行主程序(但这不是您在构建项目时所做的)
    • 正在运行 ant form,但由于您没有填写任何速度上下文:速度将无法替换模板中的变量(即生成的 html 不会包含动态数据)

    我不确定你需要什么:

    它是否在构建时使用速度?在这种情况下,您必须运行ant form,然后运行ant run(或在comp 目标中添加depends="form")。但是您还必须在 build.xml 中为速度提供速度上下文

    如果您需要在运行时运行速度:只需使用文件编写器来执行速度合并。

    【讨论】:

      【解决方案4】:

      看起来您的构建脚本实际上并未执行 Java 代码。

      尝试使用 java ant 任务,看这里:

      Ant Java task

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        • 2014-06-14
        • 2013-11-17
        • 2019-08-31
        • 1970-01-01
        • 1970-01-01
        • 2014-02-15
        相关资源
        最近更新 更多