Velocity中的ComparisonDateTool、MathTool、NumberTool

Turn:

 

You can find it from the example that comes with the Apache official website, located at \velocity-tools-1.4-src\examples\showcase.

The examples that come with the Apache official website are as follows:

When using Velocity Tools, it must be specified in toolbox.xml, and then configured in the application's web.xml.

Here, regarding the reference to the jar package, a Velocity runtime exception is likely to occur, such as:

信息: Velocity    [warn] org.apache.velocity.runtime.exception.ReferenceException: reference : template = /templates/context.vm [line 12,column 37] : $context is not a valid reference.
2008-4-23 9:41:45 org.apache.catalina.core.ApplicationContext log
信息: Velocity    [warn] org.apache.velocity.runtime.exception.ReferenceException: reference : template = /templates/context.vm [line 16,column 37] : $context.keys is not a valid reference.
2008-4-23 9:41:45 org.apache.catalina.core.ApplicationContext log
信息: Velocity    [warn] org.apache.velocity.runtime.exception.ReferenceException: reference : template = /templates/context.vm [line 20,column 37] : $context.toolbox is not a valid reference.
2008-4-23 9:41:45 org.apache.catalina.core.ApplicationContext log
信息: Velocity    [warn] org.apache.velocity.runtime.exception.ReferenceException: reference : template = /templates/context.vm [line 24,column 37] : $context.values is not a valid reference.

The key is that there is a problem with the selection of the jar package, or it may be the jar package that does not add the specified tool.

Choose from several tools:

org.apache.velocity.tools.generic.ComparisonDateTool

The configuration in toolbox.xml looks like this:

<tool>
     <key>date</key>
     <scope>application</scope>
     <class>org.apache.velocity.tools.generic.ComparisonDateTool</class>
     <parameter name=”format” value=”yyyy-MM-dd”/>
     <parameter name=”depth” value=”2″/>
     <parameter name=”skip” value=”month”/>
</tool>

date is like a built-in object that you can always use in Velocity templates in .vm.

Regarding the date, it looks like this:

Example function to display the result
$date 2008-04-22         
$date.long April 22, 2008               
10:58:54 pm $date.medium_time 22:58:54                   
$date.full_date Tuesday, April 22, 2008                    
$date .yyyy 2008                 
$date.get('default','short') 2008-4-22 10:58 PM             
$date.get('yyyy-Md H:m:s') 2008-4-22 22:58: 54              
$date.whenIs('2008-04-20′) 2 days 22 hours ago              
$date.whenIs('2008-04-20′).full 2 ​​days 22 hours 58 minutes 54 seconds 484 milliseconds ago           
$date.whenIs( '2008-04-20').days -2            
$date.whenIs(’2008-04-20′)            22 hours 58 minutes ago        
$date.whenIs(’2008-04-20′,’2008-04-20′)            same time           
$date.difference(’2008-04-20′, ’2008-04-20′)            2 days                
$date.whenIs(’2008-04-20′).days            -2           

org.apache.velocity.tools.generic.MathTool

The configuration in toolbox.xml looks like this:

<tool>
     <key>math</key>
     <scope>application</scope>
     <class>org.apache.velocity.tools.generic.MathTool</class>
</tool>

Regarding math, it looks like this:

函数举例显示结果$math.add(“10″,”4″)            14                 
$math.sub(“100″,”20″)            80                  
$math.mul(“11″,”7″)            77                     
$math.div(“100″,”5″)            20                   
$math.idiv(“100″,”5″)            20                       
$math.pow(“2″,”5″)            32                   
$math.mod(“13″,”5″)            3              
$math.max(“99″,”16″)            99          
$math.min(“99″,”16″)            16           
$math.abs(“-99″)            99                 
$math.cell(“99.26″)        
$math.cell(“99.26″)            
$math.floor(“99.26″)            99        
$math.random            0.7663665545444911      
$math.random(“1″,”10″)            2        
$math.roundTo(“2″,”5″)            5.0              
$math.toInteger(“15″)            15               
$math.toDouble(“15″)            15.0              
$math.toNumber(“15″)            15           

org.apache.velocity.tools.generic.NumberTool

The configuration in toolbox.xml looks like this:

<tool>
     <key>number</key>
     <scope>application</scope>
     <class>org.apache.velocity.tools.generic.NumberTool</class>
     <parameter name=”format” value=”#0.0″/>
</tool>

Regarding number, it looks like this:

Example of function display result $number org.apache.velocity.tools.generic.NumberTool@32c41c                
$number.currency(“35″) ¥35.00               
$number.format #0.0                  
$number.locale zh_CN             
$number.format(“35″) 35.0            
$number.integer(“35.47″) 35             
$number.number(“35.47″) 35.47           
$number.percent(“0.3547″) 35%          
$number.toNumber(“35.47″) 35.47           

org.apache.velocity.tools.generic.SortTool

The configuration in toolbox.xml looks like this:

<tool>
     <key>sorter</key>
     <scope>application</scope>
     <class>org.apache.velocity.tools.generic.SortTool</class>
</tool>

Regarding the sorter, it looks like this:

Example of function display result
$sorter.sort(['d','a','b','c']) [a, b, c, d]               
$sorter.sort([5,1,12,3] ) [1, 3, 5, 12]           

The sorter tool can sort the specified collection, such as List, Array, Map and so on.

org.apache.velocity.tools.generic.EscapeTool

The configuration in toolbox.xml looks like this:

<tool>
     <key>esc</key>
     <scope>application</scope>
     <class>org.apache.velocity.tools.generic.EscapeTool</class>
</tool>

Regarding esc, it looks like this:

Example of function display result $esc org.apache.velocity.tools.generic.EscapeTool@e09a07                 
$esc.b \             
$esc.backslash \            
$esc.d $                 
$esc.dollar $                
$esc.e !         
$esc.exclamation !           
$ esc.h #                  
$esc.hash #                 
$esc.q "            
$esc.quote "          
\esc.s esc.s              
$esc.singleQuote '          
$esc.html("$esc.backslash") \          
$esc.url(“shirdrn.org/users/index.vm?id=1&name=异域王者”)     shirdrn.org%2Fusers%2Findex.vm%3Fid%3D1%26name%3D%E5%BC%82%E5%9F%9F%E7%8E%8B%E8%80%85          
$esc.sql(“SELECT * FROM PERSON WHERE ID=’2008042319831′”)            SELECT * FROM PERSON WHERE ID=”2008042319831”                  
\$esc.java()            [说明]Escapes the characters in a String using Java String rules.              
\$esc.javascript()            [说明]Escapes the characters in a String using JavaScript String rules.             
\$esc.xml()            [说明]Escapes the characters in a String using XML entities.                  
\$esc.propertyKey()            [说明]Escapes the characters in a String using java.util.Properties rules for escaping keys.                     
\$esc.propertyValue()            [说明]Escapes the characters in a String using java.util.Properties rules for escaping values.    

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326631003&siteId=291194637