1. 程式人生 > >springmvc整合 velocity,實現多檢視整合(jsp,velocity)

springmvc整合 velocity,實現多檢視整合(jsp,velocity)

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