java - What is Apache Velocity? -


Can anyone please explain, what is Apache velocity?

Thank you in advance.

Apache velocity is one, it means that you can add variables in a context, load the template In which the variables are referenced and present a text from this template where the reference of the variable is replaced by the actual value of the variable.

Its purpose is to separate the design and static content from the code, for example, take a website. You do not want to make HTML inside your java code, right? Every time you change a bit of design, you may have to compile your app again and you will control your code with unnecessary design disorder. You want to get your variable, either by calculation or database or whatever and a designer create an HTML template in which your variables are used.

Some pseudo-codes to clean it:

  / * The user's name is "Foo" and he is "admin" * / user user = getUserFromDatabase ("Foo" ) Is the type of; / * You will not add strict coded content in the real world. * It's just to show how the template engine works * / string message = "hello"; Velocity.init (); / * Velocity engine * / VelocityContext ctx = new VerlocityContext () starts; / * User object template * / ctx.put ("user", user) will be available under "user"; / * Message as "welcome" * / ctx.put ("welcome", message); Stringworld author = new stringwriter (); Speed Merge template ("myTemplate.vm", CTX, author); Println (author);   

A file named myTemplate.vm is now given

  $ {welcome} $ {user.name}! You are $ {user.type}   

The output will be:

  Hello, Foo! You are an administrator   

Now suppose that flat text should be instead of HTML. Designer myTemplate.vm

  & lt; Html & gt; Will change in & Lt; Body & gt; & Lt; H1 & gt; $ {Welcome} $ {user.name} & lt; / H1> & Lt; P & gt; You are a $ {user.type} & lt; / P & gt; & Lt; / Body & gt; & Lt; / Html & gt;   

Then the output will be an HTML page without a single change in the Java code.

So the use of a template engine like velocity (there others, for example or), designers work as a designer and programmers work as a programmer with minimal intervention for each other.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -