`
精神分裂
  • 浏览: 28462 次
  • 性别: Icon_minigender_1
  • 来自: 二次元世界
社区版块
存档分类
最新评论

Velocity在Struts 2框架下的应用

阅读更多
Struts 2更是提供了对Velocity和FreeMarker模板引擎的支持。通过以下几个步骤,实现在Struts 2中使用velocity模板。
创建一个Web Project,除了Struts2所必备的包外,还需要引入如下包:velocity-1.4.jar、velocity-dep-1.4.jar、velocity-tools-1.1.jar

在web.xml中不需要配置关于Velocity的Servlet,只配置Struts 2的filter即可。
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>


编写Action代码如下:
public class Velocity_II {
	private String str;
	private List<String> names;
	private List<User> users;
	
	public String execute() {
		this.str = "Use Velocity";
		this.names = getStringData();
		this.users = getObjectData();
		return "success";
	}
	public List<String> getStringData() {
		List<String> list = new ArrayList<String>();
		String name = null;
		for(int i=0;i<20;i++){
			name = "Hoffman "+i+" Name";
			list.add(name);
		}
		return list;
	}
	public List<User> getObjectData() {
		List<User> lusers = new ArrayList<User>();
		for(int i=0;i<5;i++) {
			User user = new User();
			user.setName("中文测试 "+i);
			user.setPhone("1395487"+i);
			user.setAddress("Shop Street "+i);
			lusers.add(user);
		}
		return lusers;
	}
	……省略Geter and Settr Method……
}


在webroot下建立list.vm,内容如下:
<html>
	<head> <title>List Velocity</title> </head>
	<body>
		<b>Use Valocity to Show String:</b> $str &nbsp; ${str}<hr>
		<b>Use Valocity to Show List:</b></br>
		#set($num = 0)
		#set($bool = true)
		#foreach($name in $names)
			#if($bool)
				$name &nbsp;&nbsp;
				#if($num == 4) </br> #end
				#if($num == 9) #set($bool = false) #end
				#set($num = $num + 1)
			#end
		#end <hr>
		<b>Use Valocity to Show List:</b></br>
		#foreach($user in $users)
			$user.name - $user.phone - $user.address </br>
		#end <hr>
	</body>
</html>


在struts.xml配置文件中,需要注意配置result的type,如下:
<struts>
	<constant name="struts.i18n.encoding" value="GBK"/>
	<package name="struts" extends="struts-default">
	    <action name="listVelocity" class="com.mixele.velocityII.Velocity_II">
	        <result name="success" type="velocity">list.vm</result>
	    </action>
    </package>
</struts>

分享到:
评论

相关推荐

    完整的struts2框架应用实例.docx

    Struts2 框架应用实例详解 Struts2 框架是一种基于 Java 语言的 Web 应用程序框架,它提供了一个灵活的和可扩展的架构,以帮助开发者快速构建基于 Web 的应用程序。下面是 Struts2 框架应用实例的详细介绍: 一、...

    Velocity+Struts 配置

    Velocity 是一个基于Java的模版引擎。它允许web 页面设计者引用...这个项目完成后,Velocity将为Turbine web 应用程序框架提供模板服务。Velocity+Turbine 方案提供的模板服务将允许web 应用按真正的mvc模式进行开发。

    Struts2与Velocity模板

    Struts2 与 Velocity 模板 Velocity 是一种基于 JAVA 的模板引擎,开发人员使用...Velocity 模板引擎是一个功能强大且灵活的模板引擎,广泛应用于 Web 开发中,可以与 Struts2 框架集成,实现显示层与程序代码的分离。

    Struts2\constant应用

    该属性的默认值为struts-default.xml,struts-plugin.xml,struts.xml,看到该属性值,读者应该明白为什么Struts 2框架默认加载struts.xml文件了。 struts.objectFactory 该属性指定Struts 2中的action由哪个容器...

    Struts2属性文件详解

    Struts2属性文件详解 struts.configuration 该属性指定加载Struts 2...该属性的默认值为struts- default.xml,struts-plugin.xml,struts.xml,看到该属性值,所以应该明白为什么Struts 2框架默认加载struts.xml文件了.

    Struts2 in action中文版

    第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 为什么使用框架 8 1.3 Struts 2框架...

    深入浅出struts2

    │深入浅出STRUTS 2 Struts Ti却发现了二者在技术与开发人员这两个层面上的共同之处,不久之后,两个项目就在WebWork的技术基础上进行了合并2。 当我们说起WebWork的时候,我们实际上说的是两个项目——XWork和...

    实战STRUTS 电子书

    书中还介绍了如何同时使用JSP标签和Velocity模板,并仔细介绍了Struts体系结构以及Struts控制流,同时也说明了如何扩展Struts框架结构。 书中还明确指出了Struts 1.0和1.1版本间的区别,同时还通过实例说明了Struts ...

    第五章struts2的标签库.pdf

    $ 符号用于在国际化资源文件中引用 OGNL 表达式和在 Struts 2 框架的配置文件中引用 OGNL 表达式。 Struts 2 的标签库可以分为三类:UI 标签、数据标签和逻辑标签。UI 标签主要用于生成 HTML 页面,数据标签用于...

    java 三大主流框架

    Struts2是一款基于MVC模式的Web应用框架,主要用于开发Web应用程序。Struts2的出现是为了取代Struts1,Struts2相比Struts1有很多改进和优化,例如支持AJAX、JSON、XML等技术,提高了开发效率和应用性能。 Struts2的...

    struts2讲义_吴峻申

    4.1 拦截器在Struts2中的缺省应用 47 4.2 拦截器原理实现 50 4.3 在Struts2中配置自定义的拦截器 53 4.3.1 扩展拦截器接口的自定义拦截器配置 54 4.3.2 继承抽象拦截器的自定义拦截器配置 56 4.3.3 继承方法拦截器的...

    struts2.0.jar

    Struts 2标签现在可与FreeMarker、Velocity及类似模板引擎配合使用。 · 引入拦截器: Struts 2为拦截器(interceptor)提供了全面支持。拦截器可在Action类执行前后加以执行。拦截器经配置后,可以把工作流程或者...

    深入浅出Struts2(附源码)

    作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,如数据类型转换、文件上传和下载、提高Struts 2应用的安全性、调试与性能分析、FreeMarker、Velocity、Ajax,等等。跟随作者一道深入Struts 2,聆听...

    struts 入门ppt

    struts2 入门 经过七年多的发展,Struts1已经成为了一个高度成熟的框架,不管是稳定性还是可靠性都 得到了广泛的证明。但是随着时间的流逝,技术的进步,Struts1的局限性也越来越多地暴 露出来,并且制约了Struts1的...

    Struts in Action[文字版][中文]

    本书的目的是帮助Web应用开发者能够最好的使用 Struts web 应用框架。 Struts是一个开源软件,有助于开发者更加快速和容易地建立Web应用程序。Struts依靠绝大多数开发者已熟知的标准技术—比如 JavaBeans, Java ...

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    Struts 2 是Java Web 应用首选的MVC 框架。《深入浅出Struts2》对Struts 2 的工作机理进行了透彻的阐述。书中介绍了如何利用Struts 2 来解决Web 应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写...

    简易Web框架EasyJWeb源码

    应用于WEB应用程序快速开发的MVC框架,框架设计构思来源于国内众多项目实践,框架充分借签了当前主要流行的开源Web框架(Struts、JSF、Tapestry 、Webwork),吸取了其优点及精华,利用Velocity作为模板页面引擎,是...

Global site tag (gtag.js) - Google Analytics