Spring with velocity templates


















There's a short list of Velocity-related Spring Boot properties I can set in application. But there's a huge number of Velocity properties that I can't configure that way. I found this question, that seems to address what I need, but it isn't working for me. When I use breakpoints within Spring Boot during program start-up, I can see the "spring.

No matter what value I set them to, the Velocity run-time behavior uses the default anyway. You should put your breakpoint at VelocityAutoConfiguration to ensure that Velocity is used. Take a look at spring-boot-sample-velocity. Check the dependencies. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

Learn more. How to properly configure Velocity with Spring Boot? Ask Question. List methods on array references. So, if you have a reference to an array let's say this one is a String[] with three values , you can do:. Also new in Velocity 1.

A method like azpublic void setPlanets String As was mentioned earlier, properties often refer to methods of the parent object. Velocity is quite clever when figuring out which method corresponds to a requested property. It tries out different alternatives based on several established naming conventions.

The exact lookup sequence depends on whether or not the property name starts with an upper-case letter. The final value resulting from each and every reference whether variable, property, or method is converted to a String object when it is rendered into the final output. This form is synonymous with calling the get Object method on a given object i. Since this simply calls the get method all of the following are valid uses:. The bracketed syntax also works with Java arrays since Velocity wraps arrays in an access object that provides a get Integer method which returns the specified element.

The specified element is set with the given value. Velocity tries first the 'set' method on the element, then 'put' to make the assignment. Shorthand notation for references was used for the examples listed above, but there is also a formal notation for references, which is demonstrated below:. In almost all cases you will use the shorthand notation for references, but in some cases the formal notation is required for correct processing.

The goal is to allow someone to choose the base word and produce one of the two following results: "Jack is a pyromaniac.

Using the shorthand notation would be inadequate for this task. Consider the following example:. Using formal notation can resolve this problem. Formal notation is often useful when references are directly adjacent to text in a template. When Velocity encounters an undefined reference, its normal behavior is to output the image of the reference. For example, suppose the following reference appears as part of a VTL template.

So the above example would look like the following:. Velocity 1. The general intent of this setting is to make Velocity behave more strictly in cases that are undefined or ambiguous, similar to a programming language, which may be more appropriate for some uses of Velocity.

In such undefined or ambiguous cases Velocity will throw an exception. The following discussion outlines the cases in which strict behavior is different from traditional behavior.

With this setting references are required to be either placed explicitly into the context or defined with a set directive or Velocity will throw an exception.

References that are in the context with a value of null will not produce an exception. Additionally, if an attempt is made to call a method or a property on an object within a reference that does not define the specified method or property then Velocity will throw an exception.

This is also true if there is an attempt to call a method or property on a null value. Also, The following statements show examples in which Velocity will throw an exception when attempting to call methods or properties that do not exist.

In general strict reference behavior is true for all situations in which references are used except for a special case within the if directive. If a reference is used within a if or elseif directive without any methods or properties, and if it is not being compared to another value, then undefined references are allowed. This behavior provides an easy way to test if a reference is defined before using it in a template.

Also, the argument to foreach must be iterable this behavior can be modified with the property directive. Finally, undefined macro references will also throw an exception in strict mode. References that Velocity attempts to render but evaluate to null will cause an Exception. Keep in mind this is different from the reference not existing in the context which will always throw an exception when attempting to render it in strict mode.

Now that you are familiar with references, you can begin to apply them effectively in your templates. Velocity references take advantage of some Java principles that template designers will find easy to use. For example:. These examples illustrate alternative uses for the same references. Velocity takes advantage of Java's introspection and bean features to resolve the reference names to both objects in the Context as well as the objects methods.

It is possible to embed and evaluate references almost anywhere in your template. Velocity, which is modelled on the Bean specifications defined by Sun Microsystems, is case sensitive; however, its developers have strove to catch and correct user errors wherever possible.

Note: References to instance variables in a template are not resolved. Name does resolve to the class Foo's getName instance method, but not to a public Name instance variable of Foo. References allow template designers to generate dynamic content for web sites, while directives -- easy to use script elements that can be used to creatively manipulate the output of Java code -- permit web designers to truly take charge of the appearance and content of the web site.

Directives always begin with a. This is useful with directives that are immediately followed by text.

For example the following produces an error:. The set directive is used for setting the value of a reference. A value can be assigned to either a variable reference or a property reference, and this occurs in brackets, as demonstrated:. The left hand side LHS of the assignment must be a variable reference or a property reference. The right hand side RHS can be one of the following types:. Depending on how Velocity is configured, it is usually not possible to remove an existing reference from the context via this mechanism.

Note that this can be permitted by changing one of the Velocity configuration properties. This can be confusing for newcomers to Velocity.

This tends to confuse newcomers who construct foreach loops that attempt to set a reference via a property or method reference, then immediately test that reference with an if directive. The details of the if and foreach directives are covered later in this document.

Unlike some of the other Velocity directives, the set directive does not have an end statement. When using the set directive, string literals that are enclosed in double quote characters will be parsed and rendered, as shown:. By default, this feature of using single quotes to render unparsed text is available in Velocity. This default can be changed by editing velocity. Alternately, the [[ don't parse me!

This can be especially useful in place of escaping multiple directives or escaping sections which have content that would otherwise be invalid and thus unparseable VTL. The if directive in Velocity allows for text to be included when the web page is generated, on the conditional that the if statement is true. Remember that the Velocity context only contains Objects, so when we say 'boolean', it will be represented as a Boolean the class.

This is true even for methods that return boolean - the introspection infrastructure will return a Boolean of the same logical value. The content between the if and the end statements become the output if the evaluation is true.

An elseif or else element can be used with an if element. Note that the Velocity Templating Engine will stop at the first expression that is found to be true. Thanks Mailer.

StringWriter; import org. Template; import org. VelocityContext; import org. VelocityEngine; import org. MailSender; import org. Fork me on GitHub. Contents Scenario 1. Prerequisites 1. Spring setup 1. Library depedencies 1. Velocity setup 2. Email notification service 2.

MimeMessageHelper 2. VelocityEngine 2. Create velocity template 2.



0コメント

  • 1000 / 1000