Examples of velocity templates accessing java methods




















Java Code Examples for org. MethodInvocationException The following examples show how to use org. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar. Can be overridden to customize the behavior. Source Project: bazel Source File: Page. JspUtils executeTag org. InternalContextAdapter, org. Node, javax.

PageContext, javax. SimpleNode value org. Source Project: velocity-engine Source File: Velocity. Active 6 years, 5 months ago. Viewed times. Add a comment. Active Oldest Votes. To make it work I made the following changes: Make the s property protected instead of private: protected String s; Make the the getter and setter abstract in the base class A: public abstract void setS String s ; public abstract String getS ; Override the abstract methods in the sub class B.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Instances of the VelocityContext class can be created by using the default constructor with no arguments. Alternatively, constructors exist for using an existing collection object that implements the Map interface or an existing context object or both. Using an existing context object is called context chaining.

Use context chaining when you want to associate a context from code that you didn't write; it can be easier than extending a class and modifying the internals of its methods. Context chaining can also be useful for creating a thread-safe context. For this, create a new VelocityContext object by passing the shared context into the constructor. Don't share VelocityContext objects between threads because the context may get modified by the Velocity engine as it executes macros or loops.

The standard file extension for Velocity templates is. Numerous add-ons exist for editing Velocity templates inside text editors. The Template object represents a Velocity template.

Use the getTemplate method in the Velocity class to get an instance of the Template class:. There is another signature of getTemplate that sets the character encoding of the template to one of the standard Java character sets. This works just like any other use of Java encodings:. Any public method on an object in the reference which is in the context can be called.

Any objects returned by those methods can have their public methods accessed. Velocity will render any objects to strings to display them. For example, consider the following Java code:. If the method returns a primitive instead of an object, it is wrapped in the corresponding object by the Velocity engine. Calling the size method in a Vector will return an int. Velocity doesn't know how to use primitives, so it's wrapped in an Integer object. Velocity uses shortcuts for accessing properties in objects in the context.

JavaBeans use getter and setter methods for reading and modifying internal fields in the object. These fields usually have either private or protected scope and are inaccessible to outside objects without using the provided methods. Velocity provides a quick syntax for accessing these fields if the available getter method doesn't take any arguments.

In Velocity, the following two lines are equivalent to each other:. Velocity also supports the use of names that are not case-sensitive for properties. The get method is called with the property name as the key. Both of the following lines are equivalent, if there are no getEmail or getemail methods:. One important point to remember is that the Velocity engine is smart enough to figure out that if a key doesn't exist in the object, nothing will be rendered, and the statement will be rendered as text.

An example of this would be to create a HashMap object that contains a single key-value pair and then place it into the context. Here is a snippet of Java code that creates a HashMap :. The last method to be tried by Velocity, when it sees a property, is the isXXX method. Velocity treats the property as a boolean.

A simple example of this is an empty Vector class and the isEmpty method:. Velocity can also use a shortcut to set the value of a property in an object or the value of a key in a collection; this is very similar to how Velocity handles getter methods. We'll demonstrate this using a HashMap :. As we've already seen, Velocity uses its own syntax in templates so that the template content is completely separated from any additional code.

The Velocity Templating Language has very simple rules for variables. Velocity will replace a variable reference in a template with the String representation of an object. If the object referenced by a variable is not a String object, Velocity calls its toString method to render output. This is the default behavior; however, you can override the toString method in the way that you want to show the output. Variable naming is similar to other programming languages. To assign a value to a variable, we need to use one of Velocity's directives.

Every directive is prefixed with the hash symbol. Several directives are defined inside Velocity and others can be defined using macros, as we'll see in the section called Velocity Macros. The directive that we're going to use is set. The set directive works in the same way as a Java variable assignment. Normally, Velocity will display a variable reference in the results, even if the variable doesn't exist in the context.

This is because the reference could just be included in the original text. Here is a quick demonstration of this:. If we use Velocity's quiet reference notation, the template engine will not display the reference if it doesn't exist. Add an exclamation point! Velocity can be easily used to add two or more strings to create a new string in a Velocity variable.

The references to the variables should just be placed inside the quotes on the right-hand side of the equals sign, as shown in the following example:. Velocity can use a slightly more complicated notation, known as the formal reference.

This is useful for avoiding ambiguity in variable names. We'll have to use a somewhat unlikely example to demonstrate this:. One of the most common mistakes made by new Velocity users, is to use Java-style string concatenation inside a Velocity template, such as this:. Here is a quick example demonstrating their use:.

For variable references, this is needed only when the variable is already defined. Keep in mind that Velocity escaping behaves differently when variables are defined and when they aren't. One interesting quirk is involved in printing the comment tag in a template. Velocity appears incapable of using escapes to print out this combination, so the author came up with a solution:. In each case, Velocity uses an instance of Iterator internally with the foreach directive discussed in the section called foreach… end.

For collections and maps, Velocity gets the iterator directly from the object. For arrays and enumerations, Velocity internally wraps the array or enumeration in an object that implements the Iterator interface.

If an object that implements the Iterator or Enumeration interfaces is placed in the context directly, it is used and is not capable of being reset to the first element when it reaches the end.

Velocity will only be able to use that object once per template with the foreach directive. This problem does not arise with Collection, Map , or arrays. Velocity will just retrieve a new iterator if needed.

Velocity will place a warning into the log file when it finds an Iterator or Enumeration used in a foreach statement. It's the best practice to use a Collection, Map , or array instead, if possible. Velocity contains only two flow directives: if… else… elseif… end and foreach… end. This makes Velocity a very simple programming language.

The if directive is similar to the if statement in any other language. The if directive may be followed with elseif or else to create a list of conditions that could be true.

The end directive must be used to close the if directive. The if directives can be nested inside other if directives. To evaluate the conditions on an if or elseif directive, Velocity uses three rules:.

Here is a more advanced example, to demonstrate the use of if… else to determine if an object is in the context. It does exist, so we will see its value in the output.

We also evaluate the word true as a condition:. Here is an example demonstrating the logical operators. The Velocity foreach directive is used for looping the contents of a Collection, Iterator, Array , or Enumeration. It's used for grabbing each object in the list and doing some changes to it. If you need to know where you are in the list, Velocity provides a built-in variable that gives the loop counter.

It's really recommended not to change this because other people who use your template will not know what your counter variable is. The other configurable option is to determine whether Velocity's loop counter starts at zero or one. The counter defaults to one. It's also very easy to loop the contents of a Hashtable or HashMap. Get the set of keys from the hash table's keyset method, loop it, and output each value as it appears by calling the get method in the Hashtable with the key.

Velocity's macros are known as Velocimacros and they are essential for both maintainability and good UI design. Pieces of the UI can be broken out into reusable chunks that can take parameters. These macros can then be stored in a global library template file. This allows UI elements to be shared between templates, across the application.

Here's an example of a UI element macro, inline in a Velocity template:. Suppose that you had used this macro to build buttons all over your templates and then your web designers, your customers, or your business analysts, came back to you and told you that all images had to be served up from another server in another department, or all links needed to have JavaScript messages in the status bar.

It's easy to change something like this by using macros. We defined the macro by using the macro directive, which takes at least one argument. The first argument is the name of the macro, which is button in this case. The following arguments become the parameters to the Velocimacro. You can have zero parameters to a Velocimacro. When you call a Velocimacro, you have to use all of its parameters; there can't be any optional parameters. Arrays, Boolean s, boolean literals true or false , String s, and Object s can all be passed in as arguments.

For more on the boolean arguments, refer the section called if… end. We could also move our button macro out to a library, where it could be used across templates. Velocity's macro template libraries are defined in the Velocity properties file:. We could either add our button macro to that file, or we could create our own macro template file.

The GlobalMacros. The velocimacro. There are several other macro properties that can be set and they are defined in the excellent Velocity user guide that is available on the web site and also with the distribution.

They concern scope, allowing inline macros, allowing inline macros to have the same name as global macros, and whether or not to reload the macro library when it changes. One catch of the global macro library is that if the property velocimacro.

You may need to have different values for this setting for production servers and developer's machines. Velocity templates can load external templates or files, just like most page-oriented languages you may already be familiar with.

The files or templates are found using the Velocity resource loader. To use them, there are two directives:. The parse directive takes a single argument, which is the template to be loaded. Like other Velocity directives, this can be a string literal or a Velocity variable:. The include directive is useful for loading pieces of HTML or text into another template.

These are relative to the resource loader paths specified in the Velocity configuration file. If you aren't using a configuration file, it's relative to the working directory for the application. The directive also takes either a string literal or a variable.



0コメント

  • 1000 / 1000