Java/Velocity/HTML
Содержание
Use Velocity to generate HTML based email
import java.io.StringWriter;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class EmailDemo
{
public static void main( String[] args )
throws Exception
{
/*
* first, get and initialize an engine
*/
VelocityEngine ve = new VelocityEngine();
ve.init();
/*
* organize our data
*/
ArrayList list = new ArrayList();
Map map = new HashMap();
map.put("name", "Cow");
map.put("price", "$100.00");
list.add( map );
map = new HashMap();
map.put("name", "Eagle");
map.put("price", "$59.99");
list.add( map );
map = new HashMap();
map.put("name", "Shark");
map.put("price", "$3.99");
list.add( map );
/*
* add that list to a VelocityContext
*/
VelocityContext context = new VelocityContext();
context.put("petList", list);
/*
* get the Template
*/
Template t = ve.getTemplate( "./src/email_html.vm" );
/*
* now render the template into a Writer, here
* a StringWriter
*/
StringWriter writer = new StringWriter();
t.merge( context, writer );
/*
* use the output in the body of your emails
*/
System.out.println( writer.toString() );
}
}
-------------------------------------------------------------------------------------
<HTML>
<HEAD>
<TITLE>Pet Store Sale!</TITLE>
</HEAD>
<BODY>
<CENTER>
<B>$petList.size() Pets on Sale!</B>
<BR/>
This is an email generated by velocity
<BR/>
This month only, choose from :
#set( $count = 1 )
<TABLE>
#foreach( $pet in $petList )
<TR>
<TD>$count)</TD>
<TD>$pet.name</TD>
<TD>$pet.price</TD>
</TR>
#set( $count = $count + 1 )
#end
</TABLE>
<I>Call Today!</I>
Bests <br>
www.jexp.ru
</CENTER>
</BODY>
</HTML>
Use Velocity to generate HTML document
-------------------------------------------------------------------------------------
import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.RenderTool;
public class VMDemo {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/VMDemo.vm");
VelocityContext ctx = new VelocityContext();
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
<html>
<head>
<title>Gimli"s Widgetarium</title>
</head>
<body>
<table>
#set ($rowCount = 1)
#set ($products = ["one", "two", "three"])
#foreach($product in $products)
#if ($rowCount % 2 == 0)
#set ($bgcolor = "#FFFFFF")
#else
#set ($bgcolor = "#CCCCCC")
#end
<tr>
<td bgcolor="$bgcolor">$product</td>
<td bgcolor="$bgcolor">$product</td>
</tr>
#set ($rowCount = $rowCount + 1)
#end
</table>
</body>
</html>
Velocity Generate Document: anakia task
Velocity works With HTML
import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.RenderTool;
public class VMDemo {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/VMDemo.vm");
VelocityContext ctx = new VelocityContext();
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
#set ($companyName = "Name")
<html>
<head>
<title>$companyName Homepage</title>
</head>
<body>
<h1>Welcome!!</h1>
#if ($userType == "VIP")
<h2>You are a VIP!</h2>
#else
<h2>You are not a VIP!</h2>
#end
</body>
</html>