Velocity notes--the problem of using velocity to get dynamic Web project names

Source: Internet
Author: User

Before using JSP development, you can easily get to the root project name through request, now change to use velocity rendering view, because it is not dependent on the environment of some classes such as Servlet,request, and the Web project's root project name is not written dead, need to get dynamically, At this time what to do, tried the online said a lot of ways, summed up the experience.

The first type: (failure)

Configure the following information in the Toolbox.xml:

<toolbox scope= "Request" >
<tool key= "link" class= "Org.apache.velocity.tools.view.LinkTool"/>
</toolbox>

Then directly reference $link.contextpath in the. vm

I follow this way to configure the completion, start the browser to open the project, the page can not be brushed out, according to the truth if it can not read LinkTool, it should be directly output $link.contextpath in the page, and then I removed the VM in the $ Link.contextpath, re-deployment can be brushed out of the page, I guess I might be reading the Toolbox.xml class when the problem, and then in Toolbox.xml to add a self-written tool class test:

<Tool>        <Key>Myutil</Key>        <class>Com.ximalaya.shop.api.util.MyUtil</class>        <Scope>Application</Scope>    </Tool>

The Myutil class has one of the simplest static methods GetMessage (), returns "test", and then I use $myutil.message in the VM to test. Browser page successfully returned test ... It seems not to read toolbox.xml problem, debug a bit can really go into the LinkTool method, but the value is always empty, tinker for a long time no solution, the first method, failure!

Second type: (Success)

Add a parameter where the spring configuration file is viewresolver:

 <!--Configuration Attempt Parser -    <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.velocity.VelocityViewResolver">        < Propertyname= "Cache"value= "false"/>        < Propertyname= "prefix"value=""/>        < Propertyname= "suffix"value= ". VM"/>         < property name= "Requestcontextattribute"  value= "RC"/>         < Propertyname= "ContentType"value= "Text/html;charset=utf-8"/>        < Propertyname= "Toolboxconfiglocation"value= "/web-inf/toolbox.xml"/>        < Propertyname= "Exposespringmacrohelpers"value= "true"/>        < Propertyname= "Exposerequestattributes"value= "true"/>        < Propertyname= "Exposesessionattributes"value= "true"/>        < Propertyname= "Allowsessionoverride"value= "true"/>        < Propertyname= "Allowrequestoverride"value= "true"/>    </Bean>

In the VM file, use $rc.contextpath directly to get the project name.

"Requestcontextattribute" exposes spring's RequestContext objects as variable RC. Use $rc.contextpath to get the application's ContextPath (that is,/myusers) and read user.name with ${rc.getmessage ("/web-inf/classes/")} Messages.properties localization information. This object is essential for view technologies that do not access Serlvet requests (that is, the velocity and freemarker templates).

Velocity notes--the problem of using velocity to get dynamic Web project names

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.