Velocity+Java较全教程
来源:互联网 发布:全面战争战锤优化 编辑:程序博客网 时间:2024/05/21 06:58
Velocity+Java较全教程
一.安装myEclipse
二.安装velocity的eclipse插件:
http://www.oschina.net/p/veloeclipse(介绍)
方法1(现在基本上非常慢)http://propsorter.sourceforge.net/veloeclipse/(在eclipse的安装插件那里输入这个网址,然后去掉“group items……”那个勾,就会出现一个velocity,安装它)
方法2(本地安装)http://blog.csdn.net/dannor2010/article/details/8457188
三.把velocity-1.7.jar放在项目的\WebRoot\WEB-INF\lib\下
去http://velocity.apache.org/download.cgi下载velocity-tools,然后把里头所有jar放到刚刚的lib下
http://www.cnblogs.com/dodohua/archive/2011/01/28/1946867.html
四.测试工程(重点!)
1.创建Web Project(不是Web Service Project),名为HelloWebProject
2.在WebRoot目录下新建hello.vm,内容如下:
<html><body><p>Hello, $var !</p></body></html>
3.在src目录下新建Hello.java(这个.java名字和.vm名字不需要相同),内容如下:
package com.turtle.homework;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 { protected Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) { ctx.put("var","Turtle");//给$var变量赋值为"Turtle" return getTemplate("hello.vm");//模板名称 }}
4.编辑WebRoot/WEB-INF/web.xml,改成(或新建)如下:
<?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>Hello</display-name> <welcome-file-list> <welcome-file>hello.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>HelloName</servlet-name> <!-- 这个servlet-name要和下面那个servlet-name相同,但不一定和class相同 --> <servlet-class>Hello</servlet-class> <!-- 如果你有包名就是com.a.Hello --> <init-param> <param-name>org.apache.velocity.properties</param-name> <param-value>/WEB-INF/velocity.properties</param-value> <!-- velocity配置文件 --> </init-param> </servlet> <servlet-mapping> <servlet-name>HelloName</servlet-name> <url-pattern>/hello.html</url-pattern> <!-- 当你访问hello.html时就运行Hello.java这个类,所以你可以改成其它文件即可 --> </servlet-mapping></web-app>
(重点在于<servlet>到</servlet-mapping>一段
5.编辑WebRoot/WEB-INF/velocity.properties文件(这段代码是为了将编码改成UTF-8)
input.encoding=utf-8 output.encoding=utf-8default.contenttype=text/html;charset\=utf-8
6.如果出现乱码,请看这篇文章:http://www.cnblogs.com/turtlegood/articles/3884170.html
7.运行工程,访问http://localhost:8080/HelloWebProject/hello.html,即可看到效果
(如果有任何错误的地方欢迎指出!)
- Velocity+Java较全教程
- OSG 网上学习教程 较全
- BAT 批处理脚本 教程(较全)
- java 修饰符 (较全)
- Velocity教程
- Velocity教程
- velocity教程
- Velocity教程
- Velocity教程
- Velocity教程
- Velocity教程
- Velocity教程
- Velocity教程
- Velocity教程
- Velocity教程
- velocity教程
- Velocity教程
- Velocity教程
- http://www.itellyou.cn/
- AndroidStudio代码格式工具
- iOS开发 常用第三方开源框架 网站收藏
- 使用FTP上传下载文件
- 欢迎使用CSDN-markdown编辑器
- Velocity+Java较全教程
- hdu 3306 Another kind of Fibonacci(矩阵快速幂)
- Struts2与Struts1的比较
- iOS 获取当前最上层ViewController
- Storm入门和基础性能测试
- STL的内存管理
- 盒子模型的理解
- c-free编译时头文件找不到
- 第二篇:netty的实例