`

Velocity学习(一)Hello World

阅读更多

Velocity模板引擎技术功能十分强大,在这里仅简单学习了一些入门知识,深入的知识需要在实际开发中运用中慢慢积累经验。这里讲解一下在web application环境中简单使用Velocity。

创建一个View来展示数据,这里以HTML文件作为Template,通过Velocity来填充动态数据。创建一个简单HTML文件 hello.html。

<body>
	<div style="margin:20px;"></div>
	<div style="width:80%;">
		<h3>hello world<span style="color:red;">$name</span></h3>
	</div>
</body>

 当客户端请求上面页面,服务端需要给上面的Template填充动态数据生产出HTML界面返回给客户端,这里没有使用Struts2和SpringMVC等第三方框架。创建一个Servlet对象通过继承VelocityViewServlet,可以简化开发。

public class MyVelocityViewServlet extends VelocityViewServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected Template handleRequest(HttpServletRequest request,
			HttpServletResponse response, Context ctx) {
		ctx.put("name", "Velocity");
		return getTemplate("hello.html");

	}

}

 完成了以上两个步骤,至少有一个问题没有解决那就是Template文件(hello.html)应该放到哪个路径下?

也就是VelocityViewServlet会从哪个路径下加载Template文件的问题?通过配置velocity.properties文件可以解决加载文件路径问题。

#模板的加载路径
webapp.resource.loader.path=/WEB-INF/vm/
## 设置模板文件加载器,webapp从应用根目录加载
resource.loader = webapp
webapp.resource.loader.class = org.apache.velocity.tools.view.WebappResourceLoader
## 设置编码
input.encoding = UTF-8
output.encoding = UTF-8

 创建了velocity.properties文件,引入了一个新的问题,这个配置文件又该放到哪儿?通过查询官方文档,Velocity会检索WEB-INF目录,来查找配置文件。将配置文件放到WEB-INF目录下即可。如果不放WEB-INF目录下,也可以自定义路径,但是也不是随便把配置文件放到一个随意的目录,Velocity会以WebRoot目录作为根目录查找velocity.properties配置文件。将配置文件放到WebRoot根目录下为例进行配置。


通过在web.xml文件中进行配置,让velocity来找到velocity.properties。

  	<!-- 相对于webroot路径 使用classpath写法无效  -->
  	<context-param>  
           <param-name>org.apache.velocity.properties</param-name>  
           <param-value>velocity.properties</param-value>  
  	</context-param> 
  	<context-param>  
            <param-name>org.apache.velocity.toolbox</param-name>
    		<param-value>/WEB-INF/tools.xml</param-value>
  	</context-param>
  	 
	<servlet>
		<servlet-name>MyVelocityServlet</servlet-name>
		<servlet-class>org.lian.servlet.MyVelocityViewServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>MyVelocityServlet</servlet-name>
		<url-pattern>/servlet/hello</url-pattern>
	</servlet-mapping>
	
	

 下面可以查看一下我们的结果

 


 

  • 大小: 12.4 KB
  • 大小: 39.8 KB
分享到:
评论

相关推荐

    Velocity之HelloWorld配置

    博文链接:https://dalezhu.iteye.com/blog/159771

    SpringMVC_HelloWorld:SpringMVC的一个hello world的demo,开启学习SpringMVC的大门

    SpringMVC_HelloWorld SpringMVC的一个hello world的demo,开启学习SpringMVC的大门。 SpringMVC 框架提供了一个DispatchServlet作为前端控制器...创建一个项目名称为SpringMVC_HelloWorld 目录结构如下: maven中的配置

    velocity示例

    这是一个简单入门的velocity HelloWorld入门示例,希望对初学者有帮助

    velocity例子

    java velocity例子

    Struts2 国际化字符串 拦截器

    &lt;result type="velocity"&gt;/HelloWorld.vm 例3 classes/struts.xml中VMHelloWorld Action的配置 新建HelloWorld.vm,内容如下所示: &lt;title&gt;Velocity ; charset=UTF-8"&gt; &lt;h2&gt;Message rendered in ...

    veltag-1.6.3.jar

    veltag可作为标记嵌入JSP页面。由于veltag项目未加入标准velocity项目中,所以作者制作了最新1.6.3版的veltag,当然,您还要自己下载velocity-1.6.3(h t t p://velocity....#set($a="hello world") $a &lt;/vel:velocity&gt;

    Vue中JS动画与Velocity.js的结合使用

    前面学习了用css实现动画效果,那 Vue 中能不能用js实现动画效果呢? 其实 Vue 提供了很多动画钩子 enter 入场动画钩子函数有before-enter、enter、after-... &lt;div v-if=show&gt;hello world &lt;button @click=han

    lightsword:使用SpringBoot,Scala开发的自动化测试平台

    3.1 Spring Boot CLI groovy版Hello World 3.2常规Java版Hello World 4.1 application.properties 4.2 Spring Boot属性配置和使用 4.2.1配置的优先级 4.2.2命令行参数配置 4.2.3 random。*属性配置

    Spring_Framework_ API_5.0.5 (CHM格式)

    request -&gt; Response.ok().body(fromObject("Hello World"))); 6. Kotlin支持 Kotlin 是一种静态类型的JVM语言,它让代码具有表现力,简洁性和可读性。 Spring5.0 对 Kotlin 有很好的支持。 7. 移除的特性 随着...

    实战每晚构建(中)

    主要叙述在设计构建平台时要考虑的一些开源或第三方技术,其中既有有类似于"Helloworld"的入门介绍,也有精髓内容解析,还有注意点提醒。 1、相关开源或第三方技术 在进行设计之前,我们有必要了解一些开源或第三...

    Java Web程序设计教程

    2.4项目实战——第一个javaee应用:helloworld 28 2.4.1开始创建第一个javaee应用 28 2.4.2熟悉helloworld框架结构 29 本章小结 32 课后练习 32 第3章jsp和servlet 33 3.1开发第一个jsp+servlet应用 33 3.1.1...

    Struts2 in action中文版

    2.2.2 探索HelloWorld应用程序 24 2.3 使用注解的HelloWorld 31 2.4 小结 33 第二部分 核心概念:动作、拦截器和类型转换 第3章 使用Struts 2动作 36 3.1 Struts 2动作简介 36 3.2 打包动作 39 3.2.1 Struts 2公文...

    SpringMVC模板 

    SpringMVC模板, SpringMVC与Velocity,Freemarker

    Spring-Reference_zh_CN(Spring中文参考手册)

    使用BeanPostProcessor的Hello World示例 3.7.1.2. RequiredAnnotationBeanPostProcessor示例 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.2.1. PropertyPlaceholderConfigurer示例 3.7.2.2. ...

    Spring.Boot.in.Action.2015.12.pdf

    Indeed, the fact that a simple Spring Boot Hello World application can fit into a tweet is a radical departure from what the same functionality required on the vm only a few short years ago. Out-of-...

Global site tag (gtag.js) - Google Analytics