Spring学习(三)——集成 Velocity
上篇文章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的更多相关文章
- Spring学习(三)--高级装配
一.Spring profile 在开发软件的时候,有一个很大的挑战就是将应用程序从一个环境迁 移到另外一个环境.开发阶段中,某些环境相关做法可能并不适合迁 移到生产环境中,甚至即便迁移过去也无法正常 ...
- spring学习(三) ———— spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- Spring学习三
Spring注解来注入bean 在classpath中扫描组件 组件扫描,即componetscanning 利用注解来扫描的组件有 @Component :基本注解,表示一个受Spring管理的 ...
- spring boot(三) 集成mybatis
前言 还记得之前我们写接口也是基于SpringMVC+MyBatis环境下,项目入手就需要N个配置文件,N个步骤才能实现,不但繁琐,而且时间长了xml配置文件太多,难以维护.现在基于spring bo ...
- spring学习 三 框架的搭建
1 导入jar包 spring启来最少要5个包,四个核心包和一个依赖的日志包 2 创建配置文件 在dynamic web project下的src目录下,创建一个spring的xml配置文件,名称可以 ...
- Spring学习(三)-----Spring自动装配Beans
在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...
- Spring学习三----------注入方式
© 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ...
- Spring学习(三)
DI (Dependency Injection) 1.依赖注入,组件之间的依赖关系由容器在运行期间决定.Ioc容器注入应用程序某个对象,它所需要的外部资源(包括对象,资源,常量数据). birthd ...
- spring学习三:Spring Bean 生命周期
Bean 的生命周期 理解 Spring bean 的生命周期很容易.当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态.同样,当 bean 不再需要,并且从容器中移除时,可能需 ...
- spring学习三:Spring的Aop、代理
ref:https://mp.weixin.qq.com/s/J77asUvw8FcnF-6YlX6AAw AOP相关术语: Joinpoint(连接点):类里面可以被增强的方法,这些方法称为连 ...
随机推荐
- Maven使用Nexus私服的配置
工作记录 —————————————————————————————— 配置文件 apache-maven-3.3.3\conf\settings.xml 在mirrors(镜像)之间配置. url为 ...
- shell编程——变量的数值计算
在shell脚本中,有时候会需要对数值类型的变量进行计算,通常我们用的是(()) [root@localhost collect]# ((a=1+2)) [root@localhost collect ...
- ubuntu 安装 rtpengine
摘要 RtpEngine推荐使用Debian系统,可以看出Debian系统的安装是最简单的.我是基于ubuntu18.04安装的.需要注意的是如果你的Ubuntu系统版本太低,安装时会遇到各种的版本太 ...
- 超强敏感词过滤算法第二版 可以忽略大小写、全半角、简繁体、特殊符号、HTML标签干扰
上一篇 发一个高性能的敏感词过滤算法 可以忽略大小写.全半角.简繁体.特殊符号干扰 改进主要有几点: 用BitArray取代Dictionary用空间换时间 性能进一步提升 大概会增加词库的 6k* ...
- OpenGL位图函数
[OpenGL位图函数] 1.OpenGL中glBitmap用于绘制一个二值阵列. When drawn, the bitmap is positioned relative to the curre ...
- Level Of Detail
[Level Of Detail] LOD0离摄像机最近,LODN离摄像机最远.LOD Group组件的用法是,将此组件挂在根节点上,然后每一个LOD挂同样多的Renderer(通常是子对象). At ...
- 【UVA10079 训练指南】收集者的难题【最大流】
题意: Bob和他的朋友从糖果包装里手机贴纸.这些朋友每人手里都有一些(可能有重复的)贴纸,并且只跟别人交换他所没有的贴纸,贴纸总是一对一交换. Bob比这些朋友更聪明,因为他意识到只跟别人交换自己没 ...
- 刷题向》一道逆向思维题(BZOJ1046)(NORMAL)
这道题对于一类题都有一个通用思路:反向递减序列即为正向字典序. 对于逆向思维的题还要多做才能培养这种对于逆向思维的感觉. 想到这种方法之后,就很简单了. 因为n×m不会炸,所以反向LIS叠一个贪心就能 ...
- Spring @AliasFor
原文地址:https://blog.csdn.net/wolfcode_cn/article/details/80654730 在Spring的众多注解中,经常会发现很多注解的不同属性起着相同的作用, ...
- 关于fastjson的一些知识
今天被问到了一些有关fastjson的知识,问了fastjson内部的实现机制,笔者只是用过fastjson这个包,还真没了解过它的机制等. 下去后搜索了一些有关fastjson的知识,希望能对自己和 ...