Template engine Velocity: dynamically generating pages

Source: Internet
Author: User

Recently, to achieve some business to use the dynamic generation page, it is easy to study the lower velocity open source template engine, found that it is very powerful.

Open Source Address: http://www.oschina.net/p/velocity

Presumably many Java enthusiasts use Eclipse to write programs that load the velocity plug-in in Eclipse,

The update address for Velocity's eclipse plug-in is: Http://propsorter.sourceforge.net/veloeclipse

This allows for a more friendly hint when writing template files.

Velocity of the Javadoc:http://www.oschina.net/uploads/doc/velocity-1.5/index.html

Velocity is a Java-based template engine (template engine). It allows anyone to simply use the template language (template language) to refer to objects defined by Java code. When velocity is applied to web development, the interface designer can develop a Web site that follows the MVC architecture synchronously with the Java program developer, that is, the page designer can focus on the display of the page only, while the Java program developer focuses on the business logic code. Velocity separates Java code from Web pages, which facilitates long-term maintenance of web sites, as well as providing an alternative to JSP and PHP. Velocity is far more than web site development, for example, it can generate SQL and PostScript, XML from Templates (template), or it can be used as a stand-alone tool to generate source code and reports, or as an integrated component of other systems. Velocity can also provide template services (template service) for the turbine web development architecture. The way Velocity+turbine provides a template service allows a Web application to be developed with a true MVC model.

The template language is the end of the. VM, as shown in the following example:

<th style= "WIDTH:8%;HEIGHT:28PX;" > #if ($isNull = = "0") &nbsp;<fontcolor= "Red" >*</font> #end $description </th>

<tdstyle= "width:22%;" >

#foreach ($entry in $entrys. Keyset ())

#if ($defaultValue = = $entry)

<inputtype= "Radio" id= "$name" name= "$name" value= "$entry" checked> $entrys. Get ($entry)

#else

<inputtype= "Radio" id= "$name" name= "$name" value= "$entry" > $entrys. Get ($entry)

#end

#end

</td>

If you want to learn more about the writing syntax of template files, you can search the Web for relevant information, there will be a lot of velocity grammar tutorials.

Using the template engine in our program, we need to include the corresponding velocity configuration in the configuration file, which we configured in spring,

<bean id= "Velocityengine" class= "Com.jh.jcs.framework.template.velocity.VelocityTemplateEngine" True ">

<property name= "ScriptName" >

<value>velocity</value>

</property>

<property name= "P" >

<props>

<prop key= "Resource.loader" >file</prop>

<prop

key= "File.resource.loader.description" >

Velocity File Resource Loader

</prop>

<prop key= "File.resource.loader.class" >

Org.apache.velocity.runtime.resource.loader.FileResourceLoader

</prop>

<prop key= "File.resource.loader.cache" >

False

</prop>

<prop key= "Input.encoding" >

UTF-8

</prop>

<prop key= "Output.encoding" >

UTF-8

</prop>

</props>

</property>

<property name= "Fileextname" >

<set>

<value>vm</value>

</set>

</property>

</bean>

<bean id= "Textareatemplateparser" class= "Com.jh.jcs.framework.template.velocity.InputArgTemplateParser" >

<property name= "Vmfile" >

<value>templatetest/textarea.vm</value>

</property>

</bean>

..... .......

There are too many configuration file content to move, specific detailed configuration, can refer to the information on the Internet.

Do all the configuration, the introduction of the source code, but also according to different business to do the corresponding encapsulation, such as my

Map result = Businessresourceservice.getbusinessresourcetemplatebydataorder (

false, list);

You can get the map information in the order in which you define the rules, including the page information.

This time we take out the page information

List<string> contlist = (list<string>) result.get ("HTML");

So we generated our dynamic page based on velocity, according to our field settings,

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.