Velocity usage

Velocity

Velocity is a java-based template engine (template engine), which allows anyone to use just a simple template language (template language) to reference objects defined by the java code. As a relatively perfect template engine, Velocity function is more powerful, but powerful at the same time also increases the complexity of the application. 

"#" Is used to identify the Velocity script statements, including the # set, # if, # else, # end, # foreach, # end

 

#if($info.imgs)
xx
#else
xxx
#end

 

"$" Is used to identify an object (or understood as a variable);

$msg

"{}" Variable is used to unambiguously identify Velocity

"!" Is used to force the variable does not exist be blank

$!msg

 

#set ($i=0)
#foreach($info in $list)
Serial number: $ i
#set($i=$i+1)
#end
Single row ## XXX
Multi-line # * xxx
Paragraph comment # * # * --- 
Double quotes and quotes
#set ($var="helo")
Equal force string "$ var" = "helo"
After the first definition of the type of use
#set($xx= "9")
#set($Integer = 0)
$Integer.parseInt($xx)
条件语句
#if( $foo )
#end
#if($foo)
#elseif()
#else
#end
常用实例

          
#foreach($course in $courses)
<option  value="$course.id" #if("$!query.query_course_id"=="$!course.id") selected #end>$!course.name $!course.class_name </option>		
#end   
</select>

	#set($Integer = 0)
<select name="query.query_course_times" >
 #set($nums=[1 .. $Integer.parseInt($max_times)])
#foreach($key in $nums)
<option value="$!key">$!key</option>		
#end   
</select>

#foreach( $element in $records )
<td align="left">
 $!element.s_name &nbsp;
</td>
#end
 



 

Reproduced in: https: //my.oschina.net/dminter/blog/205031

Guess you like

Origin blog.csdn.net/weixin_34127717/article/details/91888843