上篇文章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. mongodb(二)

    数据准备: var persons = [{ name:"jim", age:25, email:"75431457@qq.com", c:89,m:96,e: ...

  2. leetcode558

    """ # Definition for a QuadTree node. class Node(object): def __init__(self, val, isL ...

  3. 查看win激活状态的命令

    查看win激活状态的命令  1.键盘按下win+R 运行输入如下命令即可. 2.Win+R===>输入 slmgr.vbs -dlv   显示:最为详尽的激活信息,包括:激活ID.安装ID.激活 ...

  4. 浅谈AVL树,红黑树,B树,B+树原理及应用(转)

    出自:https://blog.csdn.net/whoamiyang/article/details/51926985 背景:这几天在看<高性能Mysql>,在看到创建高性能的索引,书上 ...

  5. Hibernate的HQL中in参数设置

    平时经常用Hibernate,由于习惯表间不建立关联,所以HQL查询时候经常要用in语句. 我最常用的情况有2种: 1.in后是个子查询,如 FROM A WHERE A.ID IN (SELECT ...

  6. linux 下的mysql 连接报错

    报错: Fri Jul 28 16:28:52 CST 2017 WARN: Establishing SSL connection without server’s identity verific ...

  7. javaScript字符串操作

    JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...

  8. sqlserver里常用的语法

    bb 为nvarchar(50)CAST(bb AS int) select MAX(CAST(bb AS int)) from AAA

  9. MySQL篇之Navicat可视化工具

    主要内容: Navicat工具的介绍和使用 1.介绍和下载安装 <1>介绍 Navicat是一款针对MySQL数据库开发的可视化管理工具,以图形界面的形式操作MySQL数据库. 但在生产环 ...

  10. CocoaPods安装/更新报错While executing gem ... (OpenSSL::SSL::SSLError)解决方案

    今天给新买的MacBook Pro更新CocoaPods,结果上来就报错,出师不利. HeinocdeMacBook-Pro:~ Heinoc$ sudo gem update --system Pa ...