Objective
For the velocity and SPRINGMVC configuration please refer to the previous article, which is not covered here. The main purpose of velocity as a Java template engine is to allow anyone to reference objects defined in Java code using a simple and powerful template language. You can specify a template layout for this page in the velocity file, saving a lot of time to write a common template layout. You can define variables to interact with Java methods.
define a layout template In the previous article, we mentioned the configuration of the default template, and of course, you can not use the default template is to use the top of the page to write
#set ($layout = ' LAYOUT/YOURLAYOUT.VM ')
So how to define a layout, see the following example:
<! doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "HTTP// Www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> Then the location of the $screen_content is where the page you are opening will occupy.
Define a variable#set ($var = ' xxxx ') # #输出上面的变量直接使用 $var
Ring Array or List#foreach ($ele in $list) <span>output element: $ele </span> #end
Conditional judgment#if ($var = = ' xxxx ') <span>it is right</span> #elseif ($var = = ") <span>sorry</span> #else < Span>it is wrong</span> #end
Built-in objectsSimilar to jsp,velocity also has its own built-in objects, such as $request, $response, $ This session has allowed us to easily display Java object request and response and the contents of the session in a Web page.
Custom labels (directives)Velocity also supports custom tags, or directives, as well. If you look at Velocity's source code, you'll find a directive.properties.
Directive.1=org.apache.velocity.runtime.directive.foreachdirective.2= org.apache.velocity.runtime.directive.includedirective.3= org.apache.velocity.runtime.directive.parsedirective.4=org.apache.velocity.runtime.directive.macrodirective.5= org.apache.velocity.runtime.directive.literaldirective.6= org.apache.velocity.runtime.directive.evaluatedirective.7= org.apache.velocity.runtime.directive.breakdirective.8=org.apache.velocity.runtime.directive.definedirective.9 =org.apache.velocity.runtime.directive.stop
Here is the velocity currently provided by the 9 tags (instructions), below we add a new directive say
<body> #say ("Hello") </body>
Java Code Implementation
import java.io.ioexception;import java.io.serializable;import java.io.stringwriter;import java.io.writer;import java.util.hashmap;import java.util.map;import org.apache.velocity.velocitycontext;import org.apache.velocity.app.velocityengine;import org.apache.velocity.context.internalcontextadapter;import org.apache.velocity.exception.methodinvocationexception;import org.apache.velocity.exception.parseerrorexception;import org.apache.velocity.exception.resourcenotfoundexception;import org.apache.velocity.runtime.directive.directive;import org.apache.velocity.runtime.parser.node.node; import org.apache.velocity.runtime.parser.node.simplenode;import org.springframework.beans.factory.annotation.autowired;import com.alibaba.citrus.service.template.templateservice;import com.alibaba.click.util.hostutil;public class say extends directive{@AutowiredTemplateService &NBSP;TEMPLATESERVICE;PRIvate static final velocityengine velocityengine = new velocityengine (); @ Overridepublic string getname () {return "Say";} @Overridepublic int gettype () {return line;} @Overridepublic boolean render (Internalcontextadapter context, writer writer,node node) throws IOException, ResourceNotFoundException,ParseErrorException, methodinvocationexception {simplenode sn = (simplenode) node.jjtgetchild (0); writer.write (String) sn.value (context); return true;}}
Next, you can add a velocity.properties file.
Say=cn.buglife.demo.msv.directive.say
Use of the Java template engine velocity