Third script with Velocity (VM)

Hi guys, I’ve been busy this week, for this reason I am posting today not Wednesday as I am used to post. Ok, So I will finish the examples with vm scripts in this post.

I’ve updated the last script and the template with another stuff that you can do with vm template. So, as you can see, you can write Java code inside the template. Like, if you want a template which it will share some parts of another template, you can put a condition and then you will be reducing the number of the templates.

– You can keep the getters and setters from Customer.Class

– You will need modify CostumerSearch: Add the petlist (New value which we are sending) and create the hashmap with different values

package test;
import org.apache.velocity.Template;
import java.io.StringWriter;
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import test.Customer;

public class CustomerSearch  {
    
    public static void main(String[] args) throws Exception { 

     CustomerSearch cs = new CustomerSearch(); 

     StringWriter writer = cs.getTemplate(); 
     System.out.println(writer.toString());
    }
    public StringWriter getTemplate()  {

        Template template = null;
        StringWriter writer = new StringWriter();
        Customer customer = getCustomer();
        
        ArrayList<Map> list = new ArrayList<Map>(); 
        Map<String, String> map = new HashMap<String, String>(); 
        
        map.put("category", "dog"); 
        map.put("name", "Whisky"); 
        list.add( map ); 

        map = new HashMap<String, String>(); 
        map.put("category", "cat"); 
        map.put("name", "Madonna"); 
        list.add( map ); 

        map = new HashMap<String, String>(); 
        map.put("category", "fish"); 
        map.put("name", "Levels"); 
        list.add( map );
        VelocityContext context = new VelocityContext();
        try  {
            context.put("petList", list);
            context.put("customer", customer);
            template = Velocity.getTemplate("src/customer.vm");
            template.merge(context, writer); 
        }  catch( Exception e )  {
          System.err.println("Exception caught: " + e.getMessage());
        }

        return writer;
    }

    private Customer getCustomer()  {
        Customer customer = new Customer();
        customer.setCustomerNumber("ABC123");
        customer.setName("Joe JavaRanch");
        customer.setAddress("123 Rancho Javo");
        customer.setCity("Bueno Ranch");
        customer.setState("CO");
        customer.setZip("63121");
        customer.setPhone("303-555-1212");
        return customer;
    }
}

– Now you need to modify the template. In this example, I created a for with the hashmap and added a condition to write pets unless fish.

<html> 
  <head>
   <title>Customer Search Results - $customer.customerNumber</title>
  </head>
  <body bgcolor="#faf7f1"> 
  <h1>Customer information for customer number $customer.customerNumber</h1>
  <b>Name:</b> $customer.name<br>
  <b>Address:</b> $customer.address<br>
  <b>City:</b> $customer.city<br>
  <b>State:</b> $customer.state<br>
  <b>Zip:</b> $customer.zip<br>
  <b>Phone:</b> $customer.phone<br>
  
  #foreach( $pet in $petList ) 
   #if ($pet.category != "fish") 
    <b>$pet.category </b>- Name: $pet.name<br> 
   #end 
  #end 

  </body>
</html>

I mixed a lot of things here (Hashmap,singleton, etc.), but now you can write the way you need. I hope something here helps you 🙂

Cheers guys !

Source:

http://www.javaworld.com/article/2075966/core-java/start-up-the-velocity-template-engine.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.