Velocity is a template language, its role, benefits and so can find a lot of information, below we divided two times to do simple practice, the first part is Hello World. Look, I finished this experiment, you know, initial start is actually a simple thing:
1, the establishment of template documents HELLO.VM, simple, the full text as follows:
Hello, $name
2. Create class file:
Import Java.io.BufferedWriter;
Import Java.io.OutputStreamWriter;
Import Org.apache.velocity.Template;
Import Org.apache.velocity.VelocityContext;
Import org.apache.velocity.app.Velocity;
public class Hellovelocity {
public static void Main (string[] args) throws Exception {
Velocity.init ();
Velocitycontext context = new Velocitycontext ();
Context.put ("name", "yy");
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (System.out));
Template Template = velocity.gettemplate ("HELLO.VM");
Template.merge (context, writer);
Writer.flush ();
Writer.close ();
}
}
Let the class file run, pay attention to the VM file in the working directory, see, the output is not hello,yy J
The procedure is this: Initialize – Get the Context–context output to the value of the image-get the template file – Write template file – Close the resource
Come here half understand it, it doesn't matter, we let it run after all, followed by the second experiment.
(keywords: Java, JSP, Servlet, template, templates, Apache, Jakarta, Velocity)
In this experiment we let velocity be combined with struts in Web applications to turn struts's forward to *.VM files.
We must go down to the Velocity-tools tool, the following address:http://jakarta.apache.org/velocity/tools/
To combine with struts, we have to do 4 steps:
1, velocitystruts and Velocityview These two tools must be added to the project, which is to put your velocity-tools-1.1.jar and Velocity-1.4.jar in the project's Lib
2, add Velocityviewservlet in Web.xml, it is the engine of VM file
<servlet-name>velocity</servlet-name> <servlet-class> Org.apache.velocity.tools.view.servlet.VelocityViewServlet
</servlet-class>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
That's two parts of the above.
3, add velocity.properties (this step is not necessary)
4, add toolbox.xml (this step is not necessary)
If you add the following two steps, you should add the Web.xml file
<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>
<load-on-startup>10</load-on-startup>
OK, let's start with our experiment, build a most basic struts project, submit title and content, which needless to say, note that forward points to. vm Files
The action file is as follows:
Infoform infoform = (infoform) Form;
HttpSession session = Request.getsession ();
Session.setattribute ("title", Infoform.gettitle ());
Session.setattribute ("Content", infoform.getcontent ());
Return (Mapping.findforward ("show"));
The forward is described in Struts-config.xml as follows:
<forward name= "Show" path= "/SHOW.VM"/>
The full text of SHOW.VM document reads as follows:
$title
<br>
$content
<br>
Lets Count: #foreach ($i in [1..5]) $i #end
In addition to displaying the variables in the session, and putting a loop on the number of writes, you can see a simple foreach
Run up, see the effect? is not a bit like using a label, of course, its function is still more, more powerful, with two hours enough to start the bar, detailed use of the time, and then look up the manual, it is not difficult.