Velocity template production tool use experience (key recommendation template path designation)

xiaoxiao2021-03-06  64

Velocity use help

Velocity is a good tool for making a template, especially suitable for website to generate a large number of static HTML pages, which can reduce resource consumption caused for database access.

There is no description of Velocity, there are many information on the Internet, just want to make some records according to the problems encountered in your project, hoping to provide you with help:

The template is required in Velocity. The template's storage path is set by the configuration file * .properties; we can use the absolute path here, you can use a relative path, such as the JSP page in my project D: / Tomcat / Under WebApps / Bjlife, the template is placed in the D: / VM directory so that the template path of the Properties file can be written:

file.resource.loader.description = Velocity File Resource Loader file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader file.resource.loader.path = d: / vm file.resource.loader .cache = false file.resource.loader.modificationCheckinterval = 2 In the corresponding program you can specify the template file path: Complete the JSP code as follows:

<% @ page import = "java.io. *"%> <% @ page import = "java.io.StringWriter"%> <% @ page import = "org.apache.velocity.app.VelocityEngine"%> < % @ page import = "org.apache.velocity.App.Velocity"%> <% @ page import = "org.apache.velocity.template"%> <% @ page import = "org.apache.velocity.VelocityContext" %> <% @ Page import = "com.bjlife.db. *"%> <% @ Page Import = "com.bjlife.util. *"%> <% @ Page import = "java.sql. *"% > <% @ Page Import = "java.net. *"%> <% @ Page import = "java.util.properties"%>

<% @ Database connection Connection conTmp = null; Statement stmtTmp = null; ResultSet rstTmp = null; String filepath = "d: //"; String vmpath =; FileReader fr = null; FileWriter fw = null; DBFactory dbFactory "." = new DBFactory (); StringUtils sutils = new StringUtils (); Properties p = new Properties (); // set Template template = null configuration information by Properties object; try {p.setProperty ( "file.resource.loader.path" , "D: / VM"); // Sets Velocity's input Output encoding conversion P.SetProperty, "GB2312"); P.SetProperty ("Input.Encoding", "ISO-8859-1" ); // Enter the encoded velocity.init (p); // get the template file template = velocity.getTemplate ("test.vm");} catch (exception e) {system.out.println (e.tostring () }

try {conTmp = dbFactory.getConnection (); stmtTmp = conTmp.createStatement (); String Menusql = "select * from test"; rstTmp = stmtTmp.executeQuery (Menusql); while (rstTmp.next ()) {VelocityContext context = new VelocityContext (); String menutype = rstTmp.getString ( "menutype"); context.put ( "title", menutype); String menuname = rstTmp.getString ( "menuname"); menuname = new String (menuname.getBytes ( "GBK ")," ISO-8859-1 "); Context.Put (" Menuname ", Menuname); string menicon = RSTTMP.GETSTRING (" Menuicon "); Context.Put (" Menuicon ", Menuicon; stringWriter Writer = New StringWriter (); template.merge (defext, write); fw = new filewriter ("MENU_DM") ". Html"); // Establish a FileWriter object, and instantiate fw fw.write (Writer .tostring ()); fw.close ();}} catch (sqlexception extmp) {extmp.printstacktrace (); system.out.println ("Data Insert Database Error!");} finally { Try {stmttmp.close ();} catch (exception e) {E.PrintStackTrace (); system.out.println ("Data Insert Database Errors! ");} Try {contmp.close ();} catch (exception e) {E.PrintStackTrace (); system.out.println (" Data Insert Database Errors! ");}}%>

转载请注明原文地址:https://www.9cbs.com/read-82181.html

New Post(0)