Velocity Template Engine Learning (2)-velocity Tools 2.0

Source: Internet
Author: User

With velocity, many of the original tags are unusable and must be done with velocity tools, the latest version of Velocity Tools is 2.0, and here are some considerations for velocity tools:

1. Integration issues with Spring MVC 3.x/4.x

Spring 3.x/4.x only supports 1.3.x velocity tools, and to use 2.0 you must extend the Velocitytoolboxview class yourself

1  Packageorg.springframework.web.servlet.view.velocity;2 3 ImportJava.util.Map;4 5 Importjavax.servlet.http.HttpServletRequest;6 ImportJavax.servlet.http.HttpServletResponse;7 8 ImportOrg.apache.velocity.context.Context;9 ImportOrg.apache.velocity.tools.Scope;Ten ImportOrg.apache.velocity.tools.ToolManager; One ImportOrg.apache.velocity.tools.view.ViewToolContext; A ImportOrg.springframework.web.servlet.view.velocity.VelocityToolboxView; -  -  Public classVelocitytoolbox2viewextendsVelocitytoolboxview { the @Override -     protectedContext Createvelocitycontext (map<string, object>model, - httpservletrequest request, httpservletresponse response) -             throwsException {//Create a +                                 //Chainedcontext -                                 //instance. + Viewtoolcontext CTX; A  atCTX =NewViewtoolcontext (Getvelocityengine (), request, response, - Getservletcontext ()); -  - Ctx.putall (model); -  -         if( This. gettoolboxconfiglocation ()! =NULL) { inToolmanager TM =NewToolmanager (); - Tm.setvelocityengine (Getvelocityengine ()); to tm.configure (Getservletcontext (). Getrealpath ( + gettoolboxconfiglocation ())); -             if(Tm.gettoolboxfactory (). Hastools (Scope.request)) { the Ctx.addtoolbox (Tm.gettoolboxfactory (). Createtoolbox ( * scope.request)); $             }Panax Notoginseng             if(Tm.gettoolboxfactory (). Hastools (scope.application)) { - Ctx.addtoolbox (Tm.gettoolboxfactory (). Createtoolbox ( the scope.application)); +             } A             if(Tm.gettoolboxfactory (). Hastools (scope.session)) { the Ctx.addtoolbox (Tm.gettoolboxfactory (). Createtoolbox ( + scope.session)); -             } $         } $         returnCTX; -     } -}
View Code

The following settings are then referenced in the servlet configuration file for spring MVC:

1 <Bean2                     class= "Org.springframework.web.servlet.view.velocity.VelocityViewResolver">3                     < Propertyname= "Order"value= "0" />4                     < Propertyname= "Cache"value= "true" />5                     < Propertyname= "prefix"value="" />6                     < Propertyname= "suffix"value= ". VM" />7                     < Propertyname= "Toolboxconfiglocation"value= "/web-inf/toolbox.xml" />                    8                     < Propertyname= "ContentType"value= "Text/html;charset=utf-8" />9                     < Propertyname= "Viewclass"value= "Org.springframework.web.servlet.view.velocity.VelocityToolbox2View"></ Property>Ten                 </Bean>
View Code

2. How to get the ContextPath of the current application

1     <Tool>2         <Key>Link</Key>3         <Scope>Request</Scope>4         <class>Org.apache.velocity.tools.view.LinkTool</class>5     </Tool>

With the LinkTool class of Velocity-tools, the current contextpath can be obtained directly with ${link.contextpath} in velocity.

3. How to get URL parameters

1     <Tool>2         <Key>Params</Key>3         <Scope>Request</Scope>4         <class>Org.apache.velocity.tools.view.ParameterTool</class>5     </Tool>

Then you can use similar $params.returnurl to get similar http://xxx.com/login? The ABC section in returnurl=abc

Velocity Template Engine Learning (2)-velocity Tools 2.0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.