`

Struts集成velocity

阅读更多
需要lib
velocity-1.4.jar,
velocity-tools-1.2.jar
copy to /WEB-INF/lib目录下
修改web.xml
<servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
</servlet>
<servlet>
    <servlet-name>velocity</servlet-name>
    <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
    <init-param>
      <param-name>org.apache.velocity.properties</param-name>
      <param-value>/WEB-INF/properties/velocity.properties</param-value>
    </init-param>
    <init-param>
      <param-name>org.apache.velocity.toolbox</param-name>
      <param-value>/WEB-INF/properties/toolbox.xml</param-value>     
    </init-param>
</servlet>

<!-- Action Servlet Mapping -->
<servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>velocity</servlet-name>
    <url-pattern>*.vm</url-pattern>
</servlet-mapping>

加了红色部分。
新建velocity属性文件(velocity.properties),内容如下
#  the following are specified relative to the
#  root of the webapp because the 
#  ControllerServlet will setup these
#  properties with the right real path
#  prepended
#
file.resource.loader.path=/vm
runtime.log=${bluetop.root}/logs/velocity.log
#encodeing
input.encoding = GBK
output.encoding = GBK
写一test class文件
public class VelocityTest extends BaseAction {

	/* (non-Javadoc)
	 * @see com.bluetop.web.action.BaseAction#executeAction(org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public String executeAction(ActionForm actionFrom,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<String> list = new ArrayList<String>();

        list.add("velocity测试");
        list.add("velocity测试 element 2");
        list.add("velocity测试 element 3");
        list.add("velocity测试 element 4");
        request.setAttribute("list", list);
		return "example";
	}
}

配置struts-config.xml
<action path="/vtest" scope="request" type="com.bluetop.demo.VelocityTest">
      <forward name="example" path="/vm/example.vm"/>
  </action>

新建example.vm
## This is an example velocity template

#set( $this = "Velocity")

$this is great!

#foreach( $name in $list )
    $name is great!
#end

#set( $condition = true)

#if ($condition)
    The condition is true!
#else
    The condition is false!
#end    

执行/vtest.do输出内容如下:
Velocity is great! velocity测试 is great! velocity测试 element 2 is great! velocity测试 element 3 is great! velocity测试 element 4 is great! The condition is true!

Velocity工作正常,over


一个典型的velocity.properties
#============================================================
# LITERAL SCRIPT
# for Velocity 1.5
#============================================================
stringliterals.interpolate = false
directive.set.null.allowed = true
directive.foreach.maxloops = -1

#============================================================
# RUNTIME LOG
#============================================================
runtime.log.logsystem.class = org.apache.velocity.tools.generic.log.CommonsLogLogSystem
runtime.log.logsystem.commons.logging.name = org.apache.velocity


#============================================================
# RUNTIME PROPERTIES
#============================================================
parser.pool.size = 20

#============================================================
# TEMPLATE ENCODING
#============================================================
input.encoding = UTF-8
output.encoding = UTF-8


#============================================================
# FOREACH PROPERTIES
#============================================================
directive.foreach.counter.name = velocityCount
directive.foreach.counter.initial.value = 1


#============================================================
# RESOURCE MANAGEMENT
#============================================================
#resource.manager.logwhenfound = true
resource.manager.class = org.apache.velocity.runtime.resource.ResourceManagerImpl
resource.manager.cache.class = org.apache.velocity.runtime.resource.ResourceCacheImpl


#============================================================
# RESOURCE LOADER PROPERTIES
#============================================================
#resource.loader = file
#file.resource.loader.description = Velocity File Resource Loader
#file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
#file.resource.loader.path = .

# for Development,
# when release modified => file.resource.loader.cache = true
#file.resource.loader.cache = false
#file.resource.loader.modificationCheckInterval = 2


#============================================================
# INCLUDE PROPERTIES
#============================================================
directive.include.output.errormsg.start = <!-- include error :
directive.include.output.errormsg.end = see error log -->


#============================================================
# PARSE PROPERTIES
#============================================================
directive.parse.max.depth = 10


#============================================================
# VELOCIMACRO PROPERTIES
#============================================================
# for Development
webapp.resource.loader.cache = false
webapp.resource.loader.modificationCheckInterval = 1
velocimacro.library.autoreload = true

velocimacro.library = /WEB-INF/conf/vm/VM_global_library.vm,/anyplace/vmmacro/anyplaceMacro.vm
velocimacro.permissions.allow.inline = true
velocimacro.permissions.allow.inline.to.replace.global = true
velocimacro.permissions.allow.inline.local.scope = false
velocimacro.context.localscope = false


#============================================================
# INTERPOLATION
#============================================================
runtime.interpolate.string.literals = true


#============================================================
# PLUGGABLE INTERROSPECTOR
#============================================================
#runtime.introspector.uberspect = org.apache.velocity.util.introspection.UberspectImpl

分享到:
评论

相关推荐

    struts2与velocity集成 实例

    在Struts2与Velocity集成的过程中,首先需要在Struts2的配置文件中指定Velocity作为视图解析器。这通常是在struts.xml文件中添加一个`&lt;result&gt;`标签,并设置类型为`velocity`,如下所示: ```xml &lt;result type="...

    struts2 velocity

    通过研究这个压缩包,开发者可以深入理解如何在Struts2中集成Velocity,以及如何利用Velocity的特性来创建动态、高效的Web应用。同时,这也是一种了解MVC架构如何在实践中运作的好方式。为了更好地利用这些资源,...

    Struts 与 Velocity 的集成(http://www-128.ibm.com/deve)

    在集成Struts和Velocity时,主要目标是利用Velocity的强大模板功能来增强Struts应用程序的视图表现。以下是一些关键的知识点: 1. **Struts MVC架构**: - Struts框架的核心是Action类,它处理来自客户端的请求,...

    struts2Velocity.zip_velocity

    在Struts2中集成Velocity,可以使得视图层的开发更加高效和灵活。 在Struts2中,Velocity模板被用来作为视图部分,用于呈现由控制器处理后的数据。Velocity模板语言(VTL)是Velocity的核心,它允许开发者在HTML、...

    使用了Struts结构和Velocity模板技术的BLOG

    Struts和Velocity是两...通过这个项目,开发者可以深入理解Struts和Velocity的集成,学习如何利用MVC模式来组织代码,以及如何利用Velocity模板优雅地呈现数据。这对于提升Java Web开发技能和构建实际项目非常有帮助。

    mongo集成spring struts2 json velocity

    在"mongo集成spring struts2 json velocity"这个项目中,我们将看到如何将这些技术整合到一起,创建一个功能丰富的Web应用程序。 首先,MongoDB的集成意味着项目会利用其NoSQL特性和文档存储的优势。Spring Data ...

    Struts2集成FreeMarker和Velocity,写成了工具类,快速实现页面静态化

    Struts2集成FreeMarker和Velocity,写成了工具类,快速实现页面静态化,以后直接调用即可,无需修改任何源代码,改压缩文件包括 1、工具类; 2、源码(含jar包); 3、mysql数据库可执行文件; 4、struts2集成...

    Struts2+velocity jar

    3. **插件支持**:Struts2有一个丰富的插件生态系统,可以方便地集成其他功能,如Ajax、JSON、文件上传等。 4. **类型转换(Type Conversion)**:Struts2自动处理HTTP请求参数到Action属性的转换,减少了开发者的...

    Struts2与Velocity模板

    在 Struts2 框架中,Velocity 模板引擎可以与 Struts2 集成,实现了显示层与程序代码的分离。 Velocity 模板引擎的优点在于,它可以使得开发人员快速开发显示层,同时也可以与 Struts2 框架集成,以实现显示层与...

    struts2+spring+velocity扩展实例V1版本

    在这个"struts2+spring+velocity扩展实例V1版本"中,我们可以看到这三个框架的集成与应用。 首先,Struts2是一个基于MVC(Model-View-Controller)设计模式的Java Web框架,它的主要职责是处理用户的请求,并将其...

    Velocity+Struts 配置

    他也可用于一个独立的程序以产生源代码和报告,或者作为其它系统的一个集成组件。这个项目完成后,Velocity将为Turbine web 应用程序框架提供模板服务。Velocity+Turbine 方案提供的模板服务将允许web 应用按真正的...

    Struts2&&Velocity

    在"Velocity003"这个压缩包中,可能包含了一些关于如何在Struts2项目中集成和使用Velocity模板的示例代码或教程。例如,可能有Action类的实现,展示了如何设置和返回结果对象;还有Velocity模板文件,演示了如何在...

    Struts2+Spring+Velocity项目

    Struts2、Spring和Velocity是Java Web开发中的三个重要框架,它们共同构建了一个高效、灵活且可扩展的应用程序架构。让我们深入探讨这三个组件以及如何在项目中整合它们。 **Struts2** 是一个基于MVC(Model-View-...

    Struts2 与 Velocity 实例

    Struts2 和 Velocity 是两种广泛应用于Java Web 开发中...在提供的压缩包文件 "Velocity" 中,可能包含了使用Velocity模板的示例或模板文件,这可以帮助开发者更好地理解和学习如何在Struts2项目中集成和使用Velocity。

    struts+spring+velocity

    Struts、Spring 和 Velocity 是Java开发中常用的三个框架,它们在构建企业级Web应用程序时起着关键作用。这里我们将深入探讨这些技术的核心概念、如何协同工作以及它们在实际项目中的应用。 **Struts** Struts 是一...

    velocity+struts2 demo

    在Struts2框架中集成Velocity,可以充分利用两者的优势。首先,开发者需要在Struts2的配置文件(struts.xml)中定义Action,指定使用Velocity作为视图解析器。然后,创建Velocity模板文件(通常以`.vm`为扩展名),...

    struts2.2+velocity+tiles+spring3+mybatis3.05整合

    总之,"struts2.2+velocity+tiles+spring3+mybatis3.05整合"实例展示了Java Web开发中的典型技术栈集成,为开发者提供了高效、稳定的开发环境,有助于提升项目开发的速度和质量。通过学习和实践这个实例,开发者可以...

    struts2与shiro集成(实例)

    此外,Struts2支持多种结果类型,如JSP、FreeMarker、Velocity等,方便视图的展示。 接下来是Shiro。Shiro提供了一种简单的方式来处理用户的登录、权限控制和会话管理。它的核心组件包括Subject(代表当前用户)、...

    struts_hibernate_velocity_mysql 开发实例

    这个"struts_hibernate_velocity_mysql 开发实例"很可能是提供了一个实际应用的案例,帮助开发者理解如何将这些技术集成到一个完整的项目中。下面我们将分别介绍这四个技术以及它们在Web开发中的作用。 **Struts** ...

    Velocity学习Web项目

    通过这个项目,你可以深入理解如何在Struts 1.x环境中集成Velocity,利用Velocity的模板语言来生成动态内容,同时还能掌握Struts MVC架构的基本工作原理。这不仅有助于提高代码的可读性和可维护性,还能提升Web应用...

Global site tag (gtag.js) - Google Analytics