Basic use of velocity and basic use of velocity
Basic usage requirements of velocity: Master jstl Technology of jsp, because the usage of velocity is very similar to that of jstl. There is little difference in syntax, but the velocity example is obviously less than jstl, and there are few explanations. Therefore, you must be familiar with jstl Technology of jsp when using velocity. Otherwise, velocity is not so easy to use.
Before learning velocity, you must learn jstl.
Basic use of velocity:
Directory:
The jar package in lib to be introduced is Baidu, which is not described here.
The specific configuration is as follows:
First, write the web. xml file, which is required for java web projects and is indispensable:
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xsi: schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id = "WebApp_ID" version = "3.1"> <display-name> VoTest </display-name> <welcome-file-list> <welcome-file> hello.html </welcome-file> </welcome-file-list> <! -- Velocity configuration file --> <servlet-name> Hello </servlet-name> <! -- The servlet-name must be the same as the servlet-name below, but not the same as the class --> <servlet-class> com. dodou. tool. hello </servlet-class> <! -- If you have a package named com. dodou. tool. hello --> <init-param> <param-name> org. apache. velocity. properties </param-name> <param-value>/WEB-INF/velocity. properties </param-value> <! -- Velocity configuration file --> </init-param> </servlet> <servlet-mapping> <servlet-name> Hello </servlet-name> <url-pattern>/hello.html </url-pattern> <! -- The Hello. java class is run when you export hello.html, so you can change it to another file. --> </servlet-mapping> </web-app>
Then the file velocity. properties only prevents garbled characters.
input.encoding=utf-8 output.encoding=utf-8default.contenttype=text/html;charset\=utf-8
Compile a basic demo class file named Hello. java for convenience. The file content is as follows:
Package com. dodou. tool; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. velocity. template; import org. apache. velocity. context. context; import org. apache. velocity. tools. view. velocityViewServlet; public class Hello extends VelocityViewServlet {private static final long serialVersionUID = 1L; protected Template handleRequest (HttpServletRequest request, HttpServletResponse response, Context ctx) {ctx. put ("var", "Wang Zheng"); // assign $ var variable "Wang Zheng" ctx. put ("var2", "tell you a joke"); // map operations, Map <String, String> paramValues = new HashMap <String, string> (); paramValues. put ("1", "Wang zheng01"); paramValues. put ("2", "Wang Zheng 02"); paramValues. put ("3", "Wang zheng03"); paramValues. put ("4", "Wang Zheng 04"); paramValues. put ("5", "Wang zheng05"); request. setAttribute ("pv", paramValues); // list operation, the foreground retrieves List = new ArrayList (); list. add ("Wang zheng01"); list. add ("Wang Zheng 02"); list. add ("Wang zheng03"); ctx. put ("list", list); return getTemplate ("hello.html"); // Template Name }}
The last one is the hello.html file. The suffix of this file is not necessarily the same for html to use vm, but must be changed in the configuration class. There is no reason.
Sample Code:
<! DOCTYPE html>
When using velocity, you can learn or preview the jstl tag syntax. This is basically the same.
The demo is as follows: (it contains the lib jar package, all of which are up-to-date)
Click to download
Https://share.weiyun.com/55594809b57c67075175c74aa4a9b5a2