SPRINGMVC to achieve velocity multiple resourceloader loading templates via files, databases, etc.

Source: Internet
Author: User

Spring implements the default file Resourceloader for velocity, and each time the files are read in a fixed position, it is difficult to implement this effect if we want to change the file at any time to make the page effective.

But there are a lot of examples in real-world scenarios, 1 marketing pages, 2 product configuration management, etc.

Implementations that can be implemented in a purely front-end way

Like what:

1. Put the page template in the database, and then write to die a page, through Ajax from the background to take a different template, and then according to different data to render the display

2. Pure back-end mode implementation, the transformation of the SPRINGMVC view of the render this layer, to ensure that each time the rendering, the original from the file system read template from the DB to read the template version.


This article mainly uses the second kind of scheme and everybody exchanges.


To analyze this problem, first understand SPRINGMVC's dispatcherservlet processing link

1. After obtaining the Modelandview returned by the controller in Dispatcherservlet, the corresponding view code is obtained by the Render method as follows:


2. In the Resolveviewname () method, Resolveviewname is performed through all configured Viewresolver. This link has been looked down, will go to the specific viewresolver and then build out view, here is not too much to repeat, see the code for details.

3. Once the view is acquired, the view is rendered using the render () method, which is rendered through Velocityview.dorender () during rendering, and then gets the template to be rendered by gettemplate ().

Velocityview.dorender ()----> gettemplate ()----> velocityengine.gettemplate ()---> runtimeinstance.gettemp Late ()---> Resourcemanager.getresource ()

Focus on Resourcemanager.getresource


This method is the template of the specific acquisition process, if the first access, then the other branch, with the cache of the If branch, go to the Else branch, will go through the loader to get template resources



You can see that the final judgment file needs to be updated by loader, velocityresourceloader to load the specific template


See here, we can understand that the control velocity of the template is obtained by loader. So if we want to implement ourselves to read the template from the DB, just implement your own loader (1. Get the template, 2 control when the template is updated)


But the problem comes!!! When does the loader spring inject in the initialization?


Keep looking down.

It is well known that the spring configuration velocity is implemented through Velocityconfigurer, Velocityconfigurer inherits from Velocityenginefactory, Through various parameters to achieve the velocity of the relevant parameters configuration, the main methods are:velocityenginefactory. Initvelocityresourceloader ()


Spring writes dead. The default loader name is called file

See here people should be more clear, implementation from the DB to read the VM file, we only need to rewrite two classes to

1.ResourceLoader (Implementation of the original from the file system in the loader to read from the DB or Distributed File system, and control the velocity of the cache management, to implement their own to manage this part of the template cache)

2.VelocityConfigurer (set a different classloader name when the velocity is initialized, and set your own newly written resourceloader in.)


The idea is basically the above idea, because the company code can not be leaked, so temporarily do not post rewrite class.

The follow-up will be implemented on a few blogs embedded in the jetty SPRINGMVC empty framework. Please look forward to it.


This article is also on and off by the evening squeeze out a little time to complete. Feel the work is busy more and more did not write the power of technical blog, but the memory is too poor, afraid of the next time to forget, but also to turn the source code, so record a thought it.


SPRINGMVC to achieve velocity multiple resourceloader loading templates via files, databases, etc.

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.