Java/Velocity/Velocity Event

Материал из Java эксперт
Перейти к: навигация, поиск

A simple demonstration of how the event handling features of the Velocity Servlet Engine are used

/*
 * Copyright 2001,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.runtime.log.LogSystem;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.app.event.EventCartridge;
import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
import org.apache.velocity.app.event.MethodExceptionEventHandler;
import org.apache.velocity.app.event.NullSetEventHandler;
import org.apache.velocity.context.Context;
/**
 *   This class is a simple demonstration of how the event handling
 *   features of the Velocity Servlet Engine are used.  It uses a
 *   custom logger as well to check the log message stream
 *   when testing the NullSetEventHandler
 *
 * @author 



== Event Handler: Exceptions  ==






   
  <!-- start source code -->
   
    <source lang="java">
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.app.event.EventCartridge;
import org.apache.velocity.app.event.MethodExceptionEventHandler;
import org.apache.velocity.app.event.NullSetEventHandler;
import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
import org.apache.velocity.context.Context;
public class EventHandler implements ReferenceInsertionEventHandler,
    NullSetEventHandler, MethodExceptionEventHandler {
  public EventHandler(Context ctx) {
    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(this);
    ec.attachToContext(ctx);
  }
  public Object referenceInsert(String reference, Object data) {
    System.out.println("referenceInsert: " + reference + " data: " + data);
    return data;
  }
  public boolean shouldLogOnNullSet(String lhs, String rhs) {
    System.out.println("shouldLogOnNullSet");
    System.out.println("lhs:" + lhs + " rhs:" + rhs);
      return true;
  }
  public Object methodException(Class cls, String method, Exception e)
      throws Exception {
    return "An " + e.getClass().getName() + " was thrown by the " + method
        + " method of the " + cls.getName() + " class [" + e.getMessage() + "]";
  }
  public static void main(String[] args) throws Exception {
    Velocity.init();
    Template t = Velocity.getTemplate("./src/eventHandler.vm");
    Context ctx = new VelocityContext();
    ctx.put("person", "Joe");
    ctx.put("exception", new ExceptionGenerator());
    EventHandler hdl = new EventHandler(ctx);
    Writer writer = new StringWriter();
    t.merge(ctx, writer);
    System.out.println(writer);
  }
}

import java.util.Random;
public class ExceptionGenerator {
  public String generate() throws Exception {
    Random rnd = new Random();
    int x = rnd.nextInt(5);
    if (x == 2) {
      throw new Exception("Unlucky!");
    } else {
      return "No Exception";
    }
  }
}
-------------------------------------------------------------------------------------
#set($myNull1 = $validNull)
#set($myNull2 = $invalidNull)
This is $person 
$exception.generate()
$exception.generate()
$exception.generate()
$exception.generate()