您的位置:首页 > 编程语言 > Java开发

Spring MVC完成指南(五)Velocity

2009-03-27 09:36 295 查看
继上一篇完成buffalo的整合后,Spring MVC的显现层可以用Velocity来表现。Velocity是很简单实用的模板引擎。根据上一篇的信息,我会继续更新代码。

Spring中包含了velocity的包,但是需要另外两个包的支持才能正常调用。

spring-support.jar 和commons-collections-3.1.jar

如果没有这两个包,执行调用的时候一直会出错。我为此也找了2天的资料,后来才发现是缺少包.......

项目图:



主要修改的地方如下:

velocity.properties

resource.loader=class
class.resource.loader.class=org.apache.Velocity.runtime.resource.loader.ClasspathResourceLoader

file.resource.loader.cache=false

input.encoding = UTF-8
output.encoding = UTF-8


dispatcherServlet.xm

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!--
This bean sets up the Velocity environment for us based on a root path for templates.
Optionally, a properties file can be specified for more control over the Velocity
environment, but the defaults are pretty sane for file based template loading.
-->
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/" />
</bean>

<!--

View resolvers can also be configured with ResourceBundles or XML files. If you need
different view resolving based on Locale, you have to use the resource bundle resolver.

-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="contentType"><value>text/html;charset=UTF-8</value></property>
<property name="cache" value="true"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

<!-- 定义视图及路径
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
-->
<!-- JSP都放在WEB-INF/jsp目录下
<property name="prefix">
<value>/jsp/</value>
</property>
-->
<!-- JSP页面的后缀都四.JSP
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

-->

<!-- 定义映射 -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/action.do">action</prop>
</props>
</property>
</bean>

<!-- 定义控制器 -->

<bean id="action" class="cn.Action">
<property name="commandClass">
<value>cn.Person</value>
</property>

<!-- 指定验证类
<property name="validator">
<ref bean="userValidator"/>
</property>
-->
<!-- 指定失败要返回的页面 -->

<property name="success_View">
<value>test</value>
</property>
</bean>

<!-- 定义验证类
<bean id="userValidator" class="cn.UserValidator"></bean>
-->
</beans>


完成修改后,装载webapp就可以看到运行结果。

下一篇会是关于webflow的集成和使用,待续......
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: