Just 2 minutes, simply build velocity web project

Velocity is a java template engine (template engine) based. It allows anyone to use just a simple template language (template language) to reference objects defined by the java code

  velocity syntax is very simple. Not much description here.

  We usually web projects, usually the front end of the development process is to write static pages. The rear end of the static page into jsp, where the corresponding data needs to be replaced, using the data jstl, EL expression or the like over the transmission module receiving action. Action is generally used for the processing parameters, calls the service to process the business logic layer, layer service layer calls dao fetch data from the database. We can see that, in general, is the backend to send data to the front end.

  Here generally have a big problem. If the business changes, such as a page would have to show a table, now we need to show three forms, the other two tables are from other pages before the merger. Benefits mvc pattern is, at this time, in addition to the change of the page, make the service business logic layer and a layer dao, need not change, only changes to the appropriate action layer, required to invoke the service module, the desired the data output.

  If using velocity, in addition to the changes to the page, the other does not need to be changed.

  velocity we usually do web projects and project the biggest difference is to take data from the back end of the front. What means what it needs. We can through the front and rear velocity completely separated.

  The idea of ​​simulation about velocity below using a simple example.

  (Since the velocity itself does not provide any web related functions, it just generates a document from a template format. However, the development of web applications, the need for a framework to handle HTTP requests .velocity have a very good sub velocity-tools, VelocityViewServlet can use it very easily achieve this function. below)

  1, only two dependencies: the latest velocity-1.6.2.jar and velocity-tools-2.0.jar, URL: http://velocity.apache.org/download.cgi

  2, first look at our web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
                         "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>   
    
    <servlet>  
        <servlet-name>velocity</servlet-name>  
        <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>  
        <init-param>
            <param-name>org.apache.velocity.toolbox</param-name>
            <param-value>/WEB-INF/conf/velocity-toolbox.xml</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>  
    <servlet-mapping>  
        <servlet-name>velocity</servlet-name>  
        <url-pattern>*.vm</url-pattern>  
    </servlet-mapping>  
    <welcome-file-list>
        <welcome-file>index.vm</welcome-file>
    </welcome-file-list>
</web-app>

 

  3, and then look at the simple page:

<html>  
<body>  
  
this is the word : $mytool.getHello()  
  
</body>  
</html> 

 

  4, the next most important velocity-toolbox.xml class, where the tools that can be called directly on the page of the class. as follows:

<?xml version="1.0" encoding="UTF-8"?>

<toolbox>

  <tool>
     <key>mytool</key>
     <scope>request</scope>
     <class>com.chandler.tool.MyTool</class>
  </tool>
    
</toolbox>

 

  指定了请求到来时的工具类实例的生命周期为request

  5、OK,就这样,可以访问了,如下:

  

  可以看到,velocity的这种特性,使得它开发小型的网站变得非常便捷!很大的提高了开发效率!

 

  如果文中有什么不对的地方,欢迎指正!谢谢!

   转载请注明出处:http://www.cnblogs.com/zrtqsk/p/4010840.html 谢谢!

 

转载于:https://www.cnblogs.com/zrtqsk/p/4010840.html

Guess you like

Origin blog.csdn.net/weixin_34092455/article/details/93248539