目前流行的三大页面视图神器是:老牌大哥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');">&nbsp;删除</a></td>
</tr>
#end
</table>
</div></td>
<td>接口名: <input type="text" name="interfaceName"
id="interfaceName" style="width: 400px" /> &nbsp;&nbsp;&nbsp; <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使用的更多相关文章

  1. spring mvc集成freemarker使用

    freemarker作为视图技术出现的比velocity早,想当年struts风靡一时,freemarker作为视图层也风光了一把.但现在velocity作为后起之秀的轻量级模板引擎,更容易得到青睐. ...

  2. Spring MVC集成slf4j-logback

    转自: Spring MVC集成slf4j-logback 1.  Spring MVC集成slf4j-log4j 关于slf4j和log4j的相关介绍和用法,网上有很多文章可供参考,但是关于logb ...

  3. spring mvc 集成freemarker模板

    主要使用到的jar 文件:spring mvc +freemarker.jar 第一步:spring mvc 集成 freemarker <!-- 定义跳转的文件的前后缀 ,视图模式配置--&g ...

  4. Spring MVC集成Swagger

    什么是Swagger? 大部分 Web 应用程序都支持 RESTful API,但不同于 SOAP API——REST API 依赖于 HTTP 方法,缺少与 Web 服务描述语言(Web Servi ...

  5. Spring MVC集成Log4j

    以下示例显示如何使用Spring Web MVC框架集成LOG4J.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: 创建一 ...

  6. Spring MVC集成Spring Data Reids和Spring Session实现Session共享

    说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入. ...

  7. Spring MVC 结合Velocity视图出现中文乱码的解决方案

    编码问题一直是个很令人头疼的事,这几天搭了一个Spring MVC+VTL的web框架,发现中文乱码了,这里记录一种解决乱码的方案. 开发环境为eclipse,首先,检查Window->pref ...

  8. Spring Boot与Spring MVC集成启动过程源码分析

    开源项目推荐 Pepper Metrics是我与同事开发的一个开源工具(https://github.com/zrbcool/pepper-metrics),其通过收集jedis/mybatis/ht ...

  9. Spring MVC集成Swagger2.0

    在集成Swagger之前,得先说说什么是Swagger,它是用来做什么的,然后再讲讲怎么集成,怎么使用,当然,在这之前,需要了解一下OpenAPI. OpenAPI OpenAPI 3.0规范定义了一 ...

随机推荐

  1. fabric文件上传打包与校验

  2. 利用正则提取discuz的正文内容

    源正文: [p=24, null, left][color=#000][font=宋体]近日,香港著名漫画家马荣成在香港举办的"[color=#ff660][url=http://cul.c ...

  3. 通过wifi连接android设备的方法

    [转自]http://blog.csdn.net/kuanxu/article/details/7444874 最近由于要在另外一台android设备上调试代码,在本机PC上查看其log.两台机器离的 ...

  4. tyvj 1059 过河 dp

    P1059 过河 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2005 提高组 第二道 描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳 ...

  5. lucene学习-2 - 一个示例

    接下来我会写一个lucene的实例.实际上在搜索引擎上随便搜索下都能找到这样的东西.不过还是写一下吧,这也是我学习的经历. package com.zhyea.doggie; import java. ...

  6. 入门教程:.NET开源OpenID Connect 和OAuth解决方案IdentityServer v3 创建简单的OAuth2.0服务器,客户端和API(三)

    本教程的目的在于创造尽可能简单的identityserver安装作为一个oauth2授权服务器.这应该能够让你了解一些基本功能和配置选项(完整的源代码可以发现在这里).在后面的文档中会介绍更多的高级功 ...

  7. python Tkinter图形用户编程简单学习(一)

    Events(事件) Events are given as strings, using a special event syntax:事件以字符串的方式给出,使用特殊的事件语法:<modif ...

  8. 为什么font-size推荐使用具体数值?

    1.font-size的单位 font-size通常用的单位是px/em/rem,px就不说了,em/rem 主要用在移动端,原因的根据根元素大小进行适配,简而言之,em相对于父级定义基础字号,rem ...

  9. python 爬虫002-http与urllib2

    urllib2 GET https://www.oschina.net/home/login #!/usr/bin/env python # -*- coding: utf-8 -*- import ...

  10. C#和Java接口对比

    C#和java的接口有很多类似之处,对于编程约束和设计模式的实现有重要作用.这里记录几个知识点. 1. C#的接口中不能有字段,但Java的接口中允许有static final修饰的字段/域(fiel ...