Simple build velocity Web project in just 2 minutes

Source: Internet
Author: User

Velocity is a Java-based template engine. It allows anyone to refer to objects defined by Java code simply by using template language (language)

Velocity's syntax is very simple. Not much to introduce here.

Our usual Web project, the usual development process is to write a static page at the front end. The backend changes the static page to a JSP and receives the data passed by the action module in the appropriate place where the data needs to be replaced, using Jstl, El expressions, and so on. The action is typically used to process parameters, call the service layer to process the business logic, and the service layer invokes the DAO layer to fetch data from the database. We can see that the back end sends the data to the front end.

There is usually a big problem here. If a business change, such as a page would be to display a table, now needs to display 3 tables, and the other 2 tables are merged from the previous pages. The advantage of the MVC pattern is that, in addition to the page changes, do business logic Processing service layer and DAO layer, do not need to change, just need to change the corresponding action layer, call the required service module, the required data output.

If you use velocity, there is no need to change anything other than the page changes.

  The biggest difference between Velocity's Web projects and our usual projects is that the front end takes data from the backend. Take whatever you need. We can completely separate the front and back end through velocity.

Let's use a simple example to simulate the idea of velocity.

(because Velocity does not provide any web-related functionality itself, it simply generates a format document from a template.) However, when developing a Web application, a framework is needed to handle HTTP requests. Velocity has a very good sub-project Velocity-tools, and using its velocityviewservlet can be very convenient to implement this function. Following

1, only 2 dependent packages: The latest Velocity-1.6.2.jar and Velocity-tools-2.0.jar, website: 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. Then look at the simple page:

< HTML >  < Body > This is the     Word: $mytool. Gethello ()    </body>  </ HTML >

4. Next is the most important velocity-toolbox.xml class, where the tool class is the class that can be called directly on the page. 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>

Specifies the lifecycle of a tool class instance when a request arrives

5, OK, so, you can access, as follows:

  

As you can see, this feature of velocity makes it very convenient to develop a small website! Greatly improved the development efficiency!

If there is something wrong in the text, please correct me! Thank you!

Reprint please indicate source: http://www.cnblogs.com/zrtqsk/p/4010840.html Thank you!

Simple build velocity Web project in just 2 minutes

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.