上篇文章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. js给kindeditor添加值

    需求:在点击回复按钮时,在kindeditor中添加被回复的用户昵称 html:<textarea name="content" id="mycontent&quo ...

  2. Redis实战——安装

    借鉴来源:https://www.cnblogs.com/codersay/p/4301677.html redis官网地址:http://www.redis.io/ 最新版本: redis-4.0. ...

  3. RouterOS DNS劫持(转)

    什么是DNS劫持 DNS劫持就是通过技术手段,来控制用户解析域名的IP地址.举个例子,正常解析域名www.awolf.net时应该返回IP:64.64.30.60:但现在通过DNS劫持,使域名www. ...

  4. atom 配置备忘

    插件 vim-mode-plus vim-mode-plus-ex-mode plateformio-ide-terminal    'cmd窗口 docblockr 帮助你快速的生成注释 linte ...

  5. elasticsearch中的mapping简介

    默认mapping elasticsearch(以下简称ES)是没有模式(schema)的,当我们执行以下命令: curl -d '{"name":"zach" ...

  6. Spring PropertyPlaceholderConfigurer占位符用法

    1.PropertyPlaceholderConfigurer是一个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口的一个实现.PropertyPlacehol ...

  7. Kafka核心概念(转)

    转自:https://blog.csdn.net/liyiming2017/article/details/82805479 1.Kafka集群结构 实际上kafka的结构图是有些区别的,现在我们看下 ...

  8. 10 华电内部文档搜索系统 search04

    上一节我们着重讲解了权限过滤器的使用.这一节讲解一下补充一下Struts 2下面对应的Checkbox的使用.对索引下面的数据进行维护操作,一次可以删除多个.在Struts 2下面使用对应的Check ...

  9. 优化mysql slave的同步速度

    测试环境:Red Hat Enterprise Linux Server release 6.3 (Santiago)Server version: 5.6.22-log MySQL Communit ...

  10. c/c++中关于String类型的思考

    首先说明:String并不是一种内置类型,因此任何通过String声明出来的实例都不是一个变量,不同于内置类型因此String仅仅能称之为一种特殊的型别,没错String是一个类类型. 一般来说c语言 ...