springmvc集成 velocity,实现多视图整合(jsp,velocity)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yyyCHyzzzz/article/details/56679732

maven依赖

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-tools</artifactId>
    <version>2.0</version>
</dependency>

注意一点: 一定还需要引入这个包,要不然会疯狂报错

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.1.3.RELEASE</version>
</dependency>

mvc配置文件

<!--jsp视图解析器-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="order" value="1"/>
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/page/"/>
    <property name="suffix" value=".jsp"/>
</bean>
<!-- velocity环境配置 -->
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <!-- velocity配置文件路径  或者直接用velocityProperties属性 -->
    <property name="configLocation" value="classpath:velocity.properties"/>
    <!-- velocity模板路径 -->
    <property name="resourceLoaderPath" value="/WEB-INF/templates/"/>
</bean>
<!-- velocity视图解析器 -->
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
    <property name="order" value="0"/>
    <property name="contentType" value="text/html;charset=UTF-8"/>
    <property name="cache" value="true"/>
    <property name="suffix" value=".vm"/>
    <property name="layoutUrl" value="layout/layout.vm"/>
    <property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持-->
    <property name="exposeSessionAttributes" value="true" /><!--是否开放request属性-->
    <property name="requestContextAttribute" value="request"/><!--request属性引用名称-->
    <property name="dateToolAttribute" value="dateTool"/>
    <property name="numberToolAttribute" value="numberTool"/>
</bean>
 

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
 

项目目录结构

image

layout.vm

<html>
<head>
    <title>$!page_title</title>
    #parse("default/header.vm")
</head>
<body>
 
<div>
 
    $screen_content
 
</div>
 
</body>
</html>

 

header.vm

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta http-equiv="Cache-Control" content="no-store"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="3600"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
 

test.vm

<html>
<head>
    <title>Spring MVC and Velocity</title>
</head>
<body>
<h1>Spring MVC and Velocity</h1>
 
    ${hello}
 
<hr />
Copyright &copy 2014 lm
</body>
</html>

 

TestController

/**
 * Created by Stay on 2017/2/23  17:43.
 */
@Controller
@RequestMapping(value = "/test")
public class TestController {
    @RequestMapping(value = "/velocity", method = RequestMethod.GET)
    public String getTest(Model model) {
        model.addAttribute("hello", "test velocity");
        return "test";
    }
}
 

测试

image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值