コレは何?

テンプレート (Template.vm)

## 岩石型惑星 を列挙する ${Terrestrial}
#foreach ($entry in $list) 
#set ($type = "${entry.type}") 
#if ($type == "Terrestrial")
${entry.jp}(${entry.en})
#end
#end

## ガス惑星 を列挙する ${Jovian}
#foreach ($entry in $list)
#set ($type = "${entry.type}")
#if ($type == "Jovian")
${entry.jp}(${entry.en})
#end
#end

## 氷惑星 を列挙する ${Uranian}
#foreach ($entry in $list)
#set ($type = "${entry.type}")
#if ($type == "Uranian")
${entry.jp}(${entry.en})
#end
#end

出力プログラム

App.java

package com.mycompany.velocityexam;

import java.io.File;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

public class App {

    public static void main(String[] args) {
        Velocity.init(getPath("MyVelocity.properties"));
        VelocityContext ctx = new VelocityContext();
        ctx.put("Terrestrial", "地球型惑星");
        ctx.put("Jovian", "ガス惑星");
        ctx.put("Uranian", "氷惑星");
        List<Planet> planets = new ArrayList<Planet>();
        planets.add(new Planet("Terrestrial", "mercury", "水星"));
        planets.add(new Planet("Terrestrial", "venus", "金星"));
        planets.add(new Planet("Terrestrial", "earth", "地球"));
        planets.add(new Planet("Terrestrial", "mars", "火星"));
        planets.add(new Planet("Jovian", "jupiter", "木星"));
        planets.add(new Planet("Jovian", "saturn", "土星"));
        planets.add(new Planet("Uranian", "uranus", "天王星"));
        planets.add(new Planet("Uranian", "neptune", "海王星"));
        ctx.put("list", planets);

        Template t = Velocity.getTemplate("Template.vm");
        StringWriter sw = new StringWriter();
        t.merge(ctx, sw);
        System.out.println(sw);
    }

    public static String getPath(String classpath) {
        try {
            URL url = App.class.getClassLoader().getResource(classpath);
            File file = new File(url.toURI());
            return file.getAbsolutePath();
        } catch (URISyntaxException ex) {
            // Should not happen
            throw new RuntimeException(ex);
        }
    }

    public static class Planet {

        private String type;
        private String jp;
        private String en;

        public Planet(String type, String en, String jp) {
            this.type = type;
            this.jp = jp;
            this.en = en;
        }

        public String getType() {
            return type;
        }

        public String getJp() {
            return jp;
        }

        public String getEn() {
            return en;
        }
    }
}

properties

resource.loader=class
#resource.loader=file
#resource.loader=classpath,file

# classpath relative
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

# file relative (base dir is /opt/velocity/templates)
file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path=/opt/velocity/templates

input.encoding=UTF-8

# runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem
# runtime.log.logsystem.log4j.category=velocity

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany</groupId>
  <artifactId>VelocityExam</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>VelocityExam</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity</artifactId>
      <version>1.7</version>
    </dependency>
  </dependencies>
</project>

出力

------------------------------------------------------------------------
Building VelocityExam 1.0-SNAPSHOT
------------------------------------------------------------------------

--- exec-maven-plugin:1.2.1:exec (default-cli) @ VelocityExam ---
水星(mercury)
金星(venus)
地球(earth)
火星(mars)

木星(jupiter)
土星(saturn)

天王星(uranus)
海王星(neptune)

------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 0.842s
Finished at: Mon Mar 10 22:43:21 JST 2014
Final Memory: 7M/310M
------------------------------------------------------------------------

転記項目のフィルタリング


Java#Jakarta


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2014-06-23 (月) 23:16:09 (3816d)
Short-URL:
ISBN10
ISBN13
9784061426061