first, from Velocityviewservlet to Velocitylayoutservlet
When you use velocity to develop a Web application, you need to configure a velocity-provided velocityviewservlet to accept the forward access to the velocity template (that is, the VM file) in XML. Velocityviewservlet is responsible for setting the attribute "read out" and the template file in the request to merge to form the final page, which is displayed to the response output on the user's computer.
Velocityviewservlet is a simple and easy to use. But as long as the Velocityviewservlet in the Web. XML is replaced with Velocitylayoutservlet, and configured on 2, 3 words, will have a simple layout of the page function. And this feature is actually very powerful.
second, Velocitylayoutservlet can ...
Velocitylayoutservlet can be used to simplify the development of page layouts under velocity.
With Velocitylayoutservlet, you can make the page appear as part of a page layout automatically when forward to a page. For example, access to User Data page, can automatically put the head of the site, tail and so on automatically output display processing.
third, velocitylayoutservlet use orderly
Several page layouts are set up in the system: Head (header), left menu area (sub), Middle right page Content section (main), Bottom (footer).
1.
Create the file [WebApp HOME]/VM/LAYOUT/LAYOUT.VM as follows:
Java code
- <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" >
- http://www.w3.org/1999/xhtml" xml:lang="en" >
- ... Omitted...
- <body>
- <div id= "header> #parse (' VM/LAYOUT/HEADER.VM ') </div>
- <div id="Content" >
- <div id="Sub" > #parse ($sub) </div>
- <div id="main" > $screen _content</div>
- </div>
- <div id="Footer" > #parse (' VM/LAYOUT/FOOTER.VM ') </div>
- </body>
$screen _content is equivalent to a placeholder, the content of the target page that is forward is substituted for that content.
#parse ($sub): Indicates that the sub position can be set dynamically through the $sub variable.
Also create ' VM/LAYOUT/FOOTER.VM ' ' VM/LAYOUT/HEADER.VM ' of these two files.
2.
Create WEB-INF/VM/USER/PROFILE.VM as follows: (assuming that the page is used to display the user's profile information)
Java code
- #set ($layout = "LAYOUT.VM")
- #set ($sub = "VM/USER/SUB.VM")
- A:what ' s your name?<br>
- B:my name is $user. loginname!
Note that this file is not the same as the normal my VM is in the first two words.
The first sentence sets which layout the page uses.
The second sentence sets the sub's value for the layout to include in the Vm/user/sub.vm file.
Also create a "VM/USER/SUB.VM" file
3. Configuring the Velocity.properites File
Using velocity is generally required to configure velocity.properites, at least input.encoding and output.encoding should be set. In this file, add the following code:
Java code
- # Directory for layout templates,
- # relative to Web application root directory
- Tools.view.servlet.layout.directory = vm/layout/
- # Filepath of the default layout template
- # relative to the layout directory
- # !!!!! Dude, pay attention to this line. Tip: Relative to the root directory of the WEBAPP!!!!
- Tools.view.servlet.layout. default.template = LAYOUT.VM
4. Make sure that the Web. XML configuration is similar to the following (mainly configuration Velocitylayoutservlet, not Velocityviewservlet):
Java code
- <servlet>
- <servlet-name>velocity</servlet-name>
- <servlet-class>
- Org.apache.velocity.tools.view.servlet.VelocityLayoutServlet
- </servlet-class>
- <init-param>
- <param-name>org.apache.velocity.toolbox</param-name>
- <param-value>/WEB-INF/toolbox.xml</param-value>
- </init-param>
- <init-param>
- <param-name>org.apache.velocity.properties</param-name>
- <param-value>/WEB-INF/velocity.properties</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>velocity</servlet-name>
- <url-pattern>*.vm</url-pattern>
- </servlet-mapping>
5, run the server bar,
A) Visit HTTP://WWW.XXX.COM/VM/USER/PROFILE.VM to see the effect.
b) Go back to the VM/USER/PROFILE.VM file and remove the #set ($layout = "LAYOUT.VM") to see the effect.
Effect comparison:
Add the Profile.vm file that $layout set, when browsing this page, will automatically turn the page into a part of the layout, the HEADER.VM,SUB.VM,FOOTER.VM also output, remove $layout settings after browsing, just output the page, The HEADER.VM,SUB.VM,FOOTER.VM is not output.
This feature is very handy when debugging and writing.
Each time you add a new page, you can easily have simple layout features by simply setting the $layout point to a layout template on the first line (note the path to the layout template, and the 3rd step has been prompted).
6, after the language:
have been thinking of implementing a similar function, and finally, after a closer look at the Velocity official website, we found that velocity was already there. I think it's good, it fits my needs.
So, if you use or are about to use velocity to develop systems, it is highly recommended to use the layout feature.
Velocity's layout function