Velocity's understanding and learning

Source: Internet
Author: User

When the company was working on the project, I saw that the background page used in the previous version used the Velocity Magic board and was curious about what technology it was, so I found some information to use SPINGMVC to configure the velocity template in the MAVEN project. Now let's start with the technology.

Velocity Initial Knowledge:

velocity is a Java template engine technology is actually a part of the front-end interface that interacts with the backend business, and you can use the jsp alternative (or both can achieve the foreground display function). Velocity is used to configure the velocity parser, which is roughly the same as the view parser method in Springmvc, except that it is technically different.

The first contact with this thing, I was attracted to the "template" This name, previously learned C # technology, do the site feel the use of templates is really good. As a system of Web sites, basically requires the overall style consistent, then it may be a lot of pages of the head and the bottom of the page is the same. If you don't use a template, you'll be repeating a lot of code (you can also use the page directive in JSP, of course). So the first feeling should feel good to use. Then we looked up some information. It seems that the technology is really good. In my opinion, it has several advantages:

(1) easy to learn, as long as there are HTML and CSS, and so on the basis. Easier to start with.

(2) The ability to really split Java code from HTML pages

(3) when interacting with background data, the usage is simple and concise (compared to the jstl tag, it is not necessary to use import to introduce the libraries needed for the page, and there is no use of a prefix, which is complex, as jstl each use. And the two functions are roughly the same)

also, performance optimization and the like, these do not have much experience, we can check the information .

So let's try how velocity blends into the SPRINGMVC project:

If you've configured Springmvc, it's really easy. Just check some information, put internalresourceviewresolver This view parser can be changed. Of course also need to import the corresponding package and configuration file: velocity-tools.xml and velocity.properties

If you have not configured Springmvc So good to learn, then you can refer to these web site information:

http://blog.csdn.net/javaloverkehui/article/details/38494609

Http://www.blogjava.net/liuxiaosan/archive/2011/11/24/364737.html

Configure the Controller so that its page displays properly when configured.

The next step is to learn the grammar of velocity.

Please refer to the article:

http://www.open-open.com/doc/view/aa893717a65647f6a020c5e05b07e748

http://blog.csdn.net/initphp/article/details/8513326

Special note is how to use the template: Here is a small example:

DEFAULT.VM

 

#* @vtlvariable name= "screen_content" type= "java.lang.String" *#

<! DOCTYPE HTML PUBLIC "-//wapforum//dtd XHTML Mobile 1.0//en" "Http://www.wapforum.org/DTD/xhtml-mobile10.dtd" >

<body>

$screen _content # #一定要注意 $screen _content, which represents the content that needs to be populated </body>

next look at how to use this template:

You can use this template if you use it directly on another page. The template can also have a part of the design, but it must be marked with $screen_content in the section where the content needs to be populated.

#set ($layout = "LAYOUT/DEFAULT.VM")

<div class= "Blue" >

<div class= "x" >

<div class= "Z logo" ></div>

<div class= "z d_h" ></div>

<div class= "z d_v" ></div>

</div>

</div>

then we'll see how the data interacts. use velocity to get controller data (usage is a bit similar to JSTL)

BOOKLIST.VM

#set ($layout = "LAYOUT/DEFAULT.VM")

<center>

<table>

#foreach ($t in ${books})

<tr>

<td>${t.bookName}</td>

<td>${t.studentId}</td>

</tr>

#end

</table>

</center>

 

The corresponding controller:

@RequestMapping ("/getbooklist.html")

Public String Getbookinfo (httpservletrequest request, httpservletresponse response) {

    

list<book> books=new arraylist<book> ();

books=bookservice.findbooksbystudent ((long) 1);

Request.setattribute ("Books", books);

return "Book/booklist";

}

as long as the data using Request.setattribute ("books", books) for key-value storage, you can directly use the El language on the requested page to obtain, using the #foreach ($t in ${books}) obtains the array directly and parses the data.

Other uses also refer to the specific syntax for learning.


Velocity's understanding and learning

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.