spring mvc集成velocity使用
目前流行的三大页面视图神器是:老牌大哥jsp、后起之秀freemarker和velocity。这里不详细比较这三者的优劣,总体来说,jsp是标配,但后面两个更严格的执行了视图与业务的分离,页面里是不允许出现java代码的。velocity是模板语言,更强调复用,性能也更好一些,velocity就是速度的意思。freemarker的集成看spring mvc集成freemarker使用。
目前流行的web框架是spring mvc,作为整合老手spring可以无缝接洽上面三个视图解析器。这里着重看下velocity怎么在spring mvc里用。
首先定义spring的web配置文件:
spring-mvc.xm
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base-package="com.wulinfeng.test.testpilling" />
<mvc:annotation-driven /> <!-- velocity配置 -->
<bean id="velocityConfiger" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/" />
<property name="configLocation" value="classpath:velocity.properties" />
</bean> <!-- 配置视图的显示 -->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="/" /><!-- 视图文件的前缀,即存放的路径 -->
<property name="suffix" value=".html" /><!-- 视图文件的后缀名 -->
<property name="dateToolAttribute" value="date" /><!--日期函数名称-->
<property name="numberToolAttribute" value="number" /><!--数字函数名称-->
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持-->
<property name="exposeRequestAttributes" value="true" /><!--是否开放request属性-->
<property name="requestContextAttribute" value="rc"/><!--request属性引用名称-->
</bean>
</beans>
velocity.properties
#encoding
input.encoding=UTF-8
output.encoding=UTF-8
contentType=text/html;charset=UTF-8 #autoreload when vm changed
file.resource.loader.cache=false
file.resource.loader.modificationCheckInterval=1
velocimacro.library.autoreload=false
配置好了视图解析器后,我们来看下怎么在页面中用,分别列出controller和页面:
package com.wulinfeng.test.testpilling.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import com.wulinfeng.test.testpilling.service.TestPillingService;
import com.wulinfeng.test.testpilling.util.PropertiesConfigUtil; @Controller
public class TestPillingController
{
/** 主页 */
private static final String HOME_PAGE = PropertiesConfigUtil.getProperty("homepage"); @Autowired
private TestPillingService testPillingService; /**
* 加载主页
*
* @author wulinfeng
* @param model
* @return
*/
@RequestMapping(value = "/home.html", method = RequestMethod.GET)
public String getMethodContent(Model model)
{
testPillingService.initialize(model);
return HOME_PAGE;
} /**
* 获取单个测试桩接口内容
*
* @author wulinfeng
* @param method
* @return
*/
@RequestMapping(value = "/getMethod/{method}", method = RequestMethod.GET)
public @ResponseBody String getMethodContent(@PathVariable("method") String method)
{
return testPillingService.getMethodContent(method);
} /**
* 新增测试桩
*
* @author wulinfeng
* @param method
* @param requestBody
* @return
*/
@RequestMapping(value = "/editMethod/{method}", method = RequestMethod.POST)
public @ResponseBody void editMethodContent(@PathVariable("method") String method, @RequestBody String requestBody)
{
testPillingService.editMethodContent(method, requestBody);
} /**
* 删除测试桩
*
* @author wulinfeng
* @param method
*/
@RequestMapping(value = "/deleteMethod/{method}", method = RequestMethod.GET)
public @ResponseBody void deleteMethodContent(@PathVariable("method") String method)
{
testPillingService.deleteMethodContent(method);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试桩配置页面</title>
</head>
<body>
<form id="interfaceForm">
<h2 align="center">
<font color="#FF0000">测试桩配置</font>
</h2>
<table
style="width: 1200px; height: 600px; margin-left: 5%; margin-top: 30px;"
border="2px" bordercolor="gray" cellspacing="0px" cellpadding="5px">
<tr height="10%">
<td rowspan="3" width="32%" valign="top" align="center"><font
size="4pt" color="black">接口名称:</font><br /> <br />
<div name="interfaceNames" id="interfaceNames"
style="border: solid 2px pink; width: 350px; height: 520px; overflow: auto;">
<table>
#foreach($method in $methodKeys)
<tr valign="top" style="height: 25px;">
<td align="center" width="150px"><a
onclick="getMethodContent('$method');">$method</a></td>
<td width="100px" align="left"><a
style="text-decoration: none;"
onclick="deleteInterfaceEntity('$method');"> 删除</a></td>
</tr>
#end
</table>
</div></td>
<td>接口名: <input type="text" name="interfaceName"
id="interfaceName" style="width: 400px" /> <input
type="button" value="新增/修改" onclick="generateInterfaceEntity();" /></td>
</tr>
<tr>
<td><font size="4pt" color="black"> 接口报文:</font><br /> <br />
<textarea name="interfaceBody" id="interfaceBody"
style="width: 700px; height: 450px; margin-left: 50px;">
</textarea></td>
</tr>
</table>
</form>
</body> <script type="text/javascript">
var xmlHttp; //创建一个xmlHttpRequest对象
window.onload = function createxmlHttp() {
try {
//尝试创建 xmlHttpRequest 对象,除 IE 外的浏览器都支持这个方法。
xmlHttp = new XMLHttpRequest();
} catch (e) {
try {
//使用较新版本的 IE 创建 IE 兼容的对象(Msxml2.xmlHttp)。
xmlHttp = ActiveXObject("Msxml12.XMLHTTP");
} catch (e1) {
try {
//使用较老版本的 IE 创建 IE 兼容的对象(Microsoft.xmlHttp)。
xmlHttp = ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
flag = false;
}
}
} //判断是否成功的例子:
if (!xmlHttp) {
alert("creat XMLHttpRequest Object failed.");
}
} //调用http接口获取接口内容
function getMethodContent(method) {
url = "/getMethod/" + method;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = showContent;
document.getElementById("interfaceName").value = method; //将接口名放入html指定div中
xmlHttp.send();
} //回调函数,显示http响应结果
function showContent() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var text = xmlHttp.responseText; //这里获得服务器返回的数据
document.getElementById("interfaceBody").innerHTML = text; //将数据放入html指定div中
} else {
alert("response error code:" + xmlHttp.status);
}
}
} //删除接口
function deleteInterfaceEntity(method) {
url = "/deleteMethod/" + method;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = showActionResult;
xmlHttp.send();
} //新增、编辑接口
function generateInterfaceEntity() {
url = "/editMethod/" + document.getElementById("interfaceName").value;
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type",
"application/json;charset=UTF-8");
xmlHttp.onreadystatechange = showActionResult;
xmlHttp.send(document.getElementById("interfaceBody").innerHTML);
} //回调函数,显示action操作结果,刷新页面
function showActionResult() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
alert("operation success!");
window.location.reload();
} else {
alert("operation failed! response error code:" + xmlHttp.status);
}
}
}
</script>
</html>
下面是service类设置数据的方法
/**
* 加载测试桩接口列表
*
* @author wulinfeng
* @param model
*/
public void initialize(Model model)
{
model.addAttribute("methodKeys", methodMap.keySet());
}
这里页面虽然是html,实际上就是velocity,注意看上面的配置<property name="suffix" value=".html" />,最后maven的pom文件里注意jar引入:
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
spring mvc集成velocity使用的更多相关文章
- spring mvc集成freemarker使用
freemarker作为视图技术出现的比velocity早,想当年struts风靡一时,freemarker作为视图层也风光了一把.但现在velocity作为后起之秀的轻量级模板引擎,更容易得到青睐. ...
- Spring MVC集成slf4j-logback
转自: Spring MVC集成slf4j-logback 1. Spring MVC集成slf4j-log4j 关于slf4j和log4j的相关介绍和用法,网上有很多文章可供参考,但是关于logb ...
- spring mvc 集成freemarker模板
主要使用到的jar 文件:spring mvc +freemarker.jar 第一步:spring mvc 集成 freemarker <!-- 定义跳转的文件的前后缀 ,视图模式配置--&g ...
- Spring MVC集成Swagger
什么是Swagger? 大部分 Web 应用程序都支持 RESTful API,但不同于 SOAP API——REST API 依赖于 HTTP 方法,缺少与 Web 服务描述语言(Web Servi ...
- Spring MVC集成Log4j
以下示例显示如何使用Spring Web MVC框架集成LOG4J.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: 创建一 ...
- Spring MVC集成Spring Data Reids和Spring Session实现Session共享
说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入. ...
- Spring MVC 结合Velocity视图出现中文乱码的解决方案
编码问题一直是个很令人头疼的事,这几天搭了一个Spring MVC+VTL的web框架,发现中文乱码了,这里记录一种解决乱码的方案. 开发环境为eclipse,首先,检查Window->pref ...
- Spring Boot与Spring MVC集成启动过程源码分析
开源项目推荐 Pepper Metrics是我与同事开发的一个开源工具(https://github.com/zrbcool/pepper-metrics),其通过收集jedis/mybatis/ht ...
- Spring MVC集成Swagger2.0
在集成Swagger之前,得先说说什么是Swagger,它是用来做什么的,然后再讲讲怎么集成,怎么使用,当然,在这之前,需要了解一下OpenAPI. OpenAPI OpenAPI 3.0规范定义了一 ...
随机推荐
- BI项目中的ETL设计详解(数据抽取、清洗与转换 )(转载)
原文:http://www.cnblogs.com/reportmis/p/5939732.html ETL是BI项目最重要的一个环节,通常情况下ETL会花掉整个项目的1/3的时间,ETL设计的好坏直 ...
- NO.3 Android SDK 高效更新
一.修改协议 SDK Manager下Tools->Options,选中 “Force https://… sources to be fetched using http://…” 既 ...
- UVALive 4998 Simple Encryption
题目描述: 输入正整数K1(K1<=5000),找一个12位正整数K2使得K1K2=K2(mod 1012). 解题思路: 压缩映射原理:设X是一个完备的度量空间,映射ƒ:Χ→Χ 把每两点的距离 ...
- Juniper
Juniper Networks[编辑] Juniper Networks 公司类型 上市(NYSE:JNPR) 成立 1996年2月 代表人物 执行长:Shaygan Kheradpir技术 ...
- poj1274
题解: 二分图匹配 裸题匈牙利匹配 代码: #include<cstdio> #include<cstring> #include<cmath> #include& ...
- android 属性动画和布局动画p165-p171
一.属性动画 ObjectAnimator ObjectAnimator是属性动画框架中最重要的实行类,创建一个ObjectAnimator只需通过他的静态工厂类直接返回一个ObjectAnimato ...
- 内存保护机制及绕过方案——利用未启用SafeSEH模块绕过SafeSEH
前言:之前关于safeSEH保护机制的原理等信息,可在之前的博文(内存保护机制及绕过方案中查看). 利用未启用SafeSEH模块绕过SafeSEH ⑴. 原理分析: 一个不是仅包含中间语言(1L)且 ...
- 一次完整的HTTP请求
HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1:建立TCP连接,TCP的三次握手 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服 ...
- Node.js/Python爬取网上漫画
某个周日晚上偶然发现了<火星异种>这部漫画,便在网上在线看了起来.在看的过程中图片加载很慢,而且有时候还不小心点到广告,大大延缓了我看的进度.后来想到能不能把先把漫画全部抓取到本地再去看. ...
- LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...