上篇文章http://www.cnblogs.com/wenjingu/p/3822989.html我们使用Gradle构建了一个简单的Spring MVC Web应用程序,

本篇将在上篇的基础上将jsp页面改为velocity模板,并集成到Spring MVC框架中。使用Velocity开发视图的好处是在团队开发中,将

java代码从web页面中分离出来,使得页面开发人员和业务逻辑开发人员的工作解耦,更有利于团队分工合作和web站点的长期维护。

1、修改gradle文件,增加依赖包,代码如下:

apply plugin: 'idea'
apply plugin: 'java' repositories {
mavenCentral()
maven { url "http://repo.spring.io/release" }
} dependencies {
compile(
"org.springframework:spring-context:4.0.5.RELEASE",
"org.springframework:spring-web:4.0.5.RELEASE",
"org.springframework:spring-webmvc:4.0.5.RELEASE",
"org.springframework:spring-context-support:4.0.5.RELEASE",
"org.apache.velocity:velocity:1.7",
"org.apache.velocity:velocity-tools:2.0",
"org.anarres.gradle:gradle-velocity-plugin:1.0.0"
)
testCompile("org.springframework:spring-test:4.0.5.RELEASE")
runtime("jstl:jstl:1.2")
} task copyJars(type: Copy) {
from configurations.runtime
into 'lib' // 目标位置
}

运行命令:gradle copyJars。

2、修改spring-web-servlet.xml配置文件,完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="controller"/> <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/vm"/>
<property name= "velocityProperties">
<props>
<prop key= "input.encoding">utf-8</prop>
<prop key= "output.encoding">utf-8</prop>
</props>
</property>
</bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<!--<property name="prefix" value="/WEB-INF/vm/"/>-->
<property name="suffix" value=".vm"/>
<property name="contentType" value="text/html;charset=utf-8"></property> <!-- 设置编码格式 -->
</bean> <!-- 扫描web包,应用Spring的注解 -->
<!--<context:component-scan base-package="controller"/>--> <!--<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--<property name="prefix" value="/WEB-INF/view/"/>-->
<!--<property name="suffix" value=".jsp"/>-->
<!--</bean>-->
</beans>

LoginController代码不变。

并在WEB-INF目录下新建目录vm,将所有的Velocity文件放在该目录下。

3、新建login.vm文件,代码如下:

<html>
<head>
<title>登录VelocityDemo</title>
</head>
<body>
<form action="/loginCheck.html" method="post">
用户名:
<input type="text" name="userName">
<br>
密 码:
<input type="password" name="password">
<br>
<input type="submit" value="登录"/>
</form>
#foreach ($error in $errors)
<br>$error
#end
</body>
</html>

上述页面的中文显示有乱码。

4、velocity中文乱码解决方案

在spring-web-servlet.xml配置文件中的添加如下代码:

<property name= "velocityProperties">
<props>
<prop key= "input.encoding">utf-8</prop>
<prop key= "output.encoding">utf-8</prop>
</props>
</property> <property name="contentType" value="text/html;charset=utf-8"></property> <!-- 设置编码格式 -->

5、测试结果

Demo源码下载:VelocityDemo2.0.zip ,lib中的jar包上传时删除了,运行前请先运行命令:gradle copyJars下载jar包。

Spring学习(三)——集成 Velocity的更多相关文章

  1. Spring学习(三)--高级装配

    一.Spring profile 在开发软件的时候,有一个很大的挑战就是将应用程序从一个环境迁 移到另外一个环境.开发阶段中,某些环境相关做法可能并不适合迁 移到生产环境中,甚至即便迁移过去也无法正常 ...

  2. spring学习(三) ———— spring事务操作

    前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...

  3. Spring学习三

    Spring注解来注入bean 在classpath中扫描组件 组件扫描,即componetscanning 利用注解来扫描的组件有  @Component  :基本注解,表示一个受Spring管理的 ...

  4. spring boot(三) 集成mybatis

    前言 还记得之前我们写接口也是基于SpringMVC+MyBatis环境下,项目入手就需要N个配置文件,N个步骤才能实现,不但繁琐,而且时间长了xml配置文件太多,难以维护.现在基于spring bo ...

  5. spring学习 三 框架的搭建

    1 导入jar包 spring启来最少要5个包,四个核心包和一个依赖的日志包 2 创建配置文件 在dynamic web project下的src目录下,创建一个spring的xml配置文件,名称可以 ...

  6. Spring学习(三)-----Spring自动装配Beans

    在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...

  7. Spring学习三----------注入方式

    © 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ...

  8. Spring学习(三)

    DI (Dependency Injection) 1.依赖注入,组件之间的依赖关系由容器在运行期间决定.Ioc容器注入应用程序某个对象,它所需要的外部资源(包括对象,资源,常量数据). birthd ...

  9. spring学习三:Spring Bean 生命周期

    Bean 的生命周期 理解 Spring bean 的生命周期很容易.当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态.同样,当 bean 不再需要,并且从容器中移除时,可能需 ...

  10. spring学习三:Spring的Aop、代理

    ref:https://mp.weixin.qq.com/s/J77asUvw8FcnF-6YlX6AAw AOP相关术语:    Joinpoint(连接点):类里面可以被增强的方法,这些方法称为连 ...

随机推荐

  1. Service和Thread的关系

    Service确实是运行在主线程里的,也就是说如果你在Service里编写了非常耗时的代码,程序必定有问题. Android的后台就是指,它的运行是完全不依赖UI的.即使Activity被销毁,或者程 ...

  2. HTTP之URL

    URI 和 URL 与 URI(统一资源标识符)相比,我们更熟悉 URL(Uniform Resource Locator,统一资源定位符).URL 正是使用 Web 浏览器等访问 Web 页面时需要 ...

  3. .net Core 2.0使用NLog

    最近研究了一下NLog的使用方式,简单的入了一下门. 实现的功能,对于不同的日志,进行不同的记录,分别有系统运行日志,和个人在程序中写的异常日志.发布之后放在了IIS上.进行查看日志的信息 参考了两篇 ...

  4. pid文件的作用

    pid文件的作用 一.pid文件的作用 1.pid文件的内容用cat命令查看,可以看到内容只有一行,记录了该进程的ID 2.pid文件的作用防止启动多个进程副本 3.pid文件的原理进程运行后会给.p ...

  5. Linux实战教学笔记48:openvpn架构实施方案(一)跨机房异地灾备

    第一章VPN介绍 1.1 VPN概述 VPN(全称Virtual Private Network)虚拟专用网络,是依靠ISP和其他的NSP,在公共网络中建立专用的数据通信网络的技术,可以为企业之间或者 ...

  6. UV mapping

    [UV mapping] UV mapping is the 3D modeling process of making a 2D image representation of a 3D model ...

  7. UIImage分类,设置边框

    #import "UIImage+image.h" @implementation UIImage (image) + (UIImage *)imageWithBorder:(CG ...

  8. 636. Exclusive Time of Functions 进程的执行时间

    [抄题]: Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU ...

  9. zookeeper 阿里滴滴 有点用 zookeeper主从选举方式

    面试也经常问kafka的原理,以及zookeeper与kafka原理的区别:kafka 数据一致性-leader,follower机制与zookeeper的区别: zookeeper是如何实现负载均衡 ...

  10. [C++ Mind Map] class and memory

    class and memory