Spring MVC 4 + velocity with custom tools

Source: Internet
Author: User

Spring MVC version 4.1.6.RELAESE is used in recent projects

And Velocity-tools was upgraded to 2.0 by my 2B.

------------------------------

Where the MVC 3 version requires custom Viewclass to complete the velocity tools loading, or error

This step, online already someone share out method:


<beanID= "Velocityviewresolver"class= "Org.springframework.web.servlet.view.velocity.VelocityViewResolver">
   <!--<property name= "Viewclass" value= "Org.springframework.web.servlet.view.velocity.VelocityView"/> -
   <propertyname= "Viewclass"value= "Com.*.*.utils." Velocitytoolview "/>
<propertyname= "prefix"value="/"/>
<propertyname= "suffix"value= ". VM"/>
<propertyname= "ContentType"value= "Text/html;charset=utf-8"/>
<propertyname= "Toolboxconfiglocation"value= "/web-inf/toolbox.xml"/>
</bean>

The code is roughly as follows:

velocitytoolview velocitytoolboxview {    context  (Map<StringObject>  modelhttpservletrequest requesthttpservletresponse response)  Exception { Viewtoolcontext ctxctx = viewtoolcontext (Getvelocityengine () Requestresponsegetservletcontext ()) Ctx.putall (model) (. Gettoolboxconfiglocation ()  != )  {             toolmanager tm = toolmanager () Tm.setvelocityengine ( Getvelocityengine ()) Tm.configure (Getservletcontext (). Getrealpath (Gettoolboxconfiglocation ())) ( Tm.gettoolboxfactory (). Hastools (Scope.))  {                 Ctx.addtoolbox (Tm.gettoolboxfactory (). Createtoolbox (Scope.))}              (Tm.gettoolboxfactory (). Hastools (Scope .))  {                ctx.addtoolbox (Tm.gettoolboxfactory (). Createtoolbox (Scope.))}              (Tm.gettoolboxfactory (). Hastools (Scope .))  {                 Ctx.addtoolbox (Tm.gettoolboxfactory (). Createtoolbox (Scope.))} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CTX}}


And I have a need to add a custom toolbox,

But smarty Pants posted a copy from the Internet Toolbox.xml in the project found:

<toolbox> <!--[Datetool] @see Http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/ge Neric/datetool.html (ja) @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/Da        Tetool.html (en) @since velocitytools 1.0-<tool> <key>jsonUtils</key> <scope>application</scope> <class>org.*. Jsonutils</class> </tool></toolbox>

MVC jumps to the display in the VM, does not recognize the jsonutils,


Helpless, follow in

Toolmanager TM = Toolmanager ()

Code found that the original 2.0 toolbox configuration has been replaced by another form.

Refer to the Velocity-tools2.jar/org.apache.velocity.tools.generic.tools.xml inside the format, you will find. The original toolbox definition method has changed.

<?XML version= "1.0"encoding= "Iso-8859-1"?>

<!--
Licensed to the Apache software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with the additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); Except in compliance
With the License. Obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

unless required by applicable law or agreed to in writing,
software distributed under the License is distributed in an
' As is ' BASIS, without warranties OR CONDITIONS of any
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-

<tools>
<toolboxScope= "Application">
<toolclass= "Com.*.utils." Velocityjsonutils "/>
</toolbox>
</tools>

Redefine the configuration of tools and the functionality is normal.

and the name of Toolbox is annotated.

()

Solved the


Encounter problems on-line solutions may be more in the description of the previous version of how to solve the problem, for their own problems, need to calm down to see the other people's source of the realization, everything will be enlightened.


Spring MVC 4 + velocity with custom tools

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.