Spring使用Velocity模版

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.wodwl</groupId>
	<artifactId>hello</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.1.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>com.ibatis</groupId>
			<artifactId>ibatis-sqlmap</artifactId>
			<version>2.1.0.565</version>
		</dependency>
		<dependency>
			<groupId>jetty</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5-6.0.2</version>
		</dependency>
		<dependency>
			<groupId>velocity</groupId>
			<artifactId>velocity</artifactId>
			<version>1.5</version>
		</dependency>
	</dependencies>

</project>



spring-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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<context:annotation-config />
	<!-- 把标记了@Controller注解的类转换为bean -->
	<context:component-scan base-package="com.wodwl.controller" />

	<bean id="velocityConfigurer"
		class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
		<property name="configLocation">
			<value>classpath:velocity.properties</value>
		</property>
	</bean>

	<bean id="velocityResolver"
		class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
		<property name="order" value="1" />
		<property name="contentType">
			<value>text/html;charset=utf-8</value>
		</property>
		<property name="suffix">
			<value>.vm</value>
		</property>
	</bean>

</beans> 

控制器类

package com.wodwl.controller;

import java.util.HashMap; 
import java.util.Map; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; 
@Controller
public class UserController { 
    private String add_view="/templates/index"; 
    private String display_view="user/display"; 
    private String list_view; 
    private String update_view; 
    

    public String getAdd_view() {
		return add_view;
	}


	public void setAdd_view(String add_view) {
		this.add_view = add_view;
	}


	public String getDisplay_view() {
		return display_view;
	}



	public void setDisplay_view(String display_view) {
		this.display_view = display_view;
	}

	public String getList_view() {
		return list_view;
	}


	public void setList_view(String list_view) {
		this.list_view = list_view;
	}


	public String getUpdate_view() {
		return update_view;
	}


	public void setUpdate_view(String update_view) {
		this.update_view = update_view;
	}

    @RequestMapping(value="/spring/ok.do")
    public ModelAndView handleRequest(HttpServletRequest request, 
            HttpServletResponse response) throws Exception {  
        Map model = new HashMap(); 
        model.put("msg", "hello nice to 带加号"); 
        return new ModelAndView(getAdd_view(), model); 
    } 
    @RequestMapping(value="/spring/hello.do")
    public ModelAndView get(){
    	Map modelMap=new HashMap();
    	modelMap.put("msg","hello world");
    	return new ModelAndView(getDisplay_view(),modelMap);
    }
	
	
}

src/main/resources/velocity.properties文件

#----------------------------------------------------------------------------
# These are the default properties for the
# Velocity Runtime. These values are used when
# Runtime.init() is called, and when Runtime.init(properties)
# fails to find the specificed properties file.
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# R U N T I M E  L O G
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
#  default LogSystem to use: default: AvalonLogSystem
#----------------------------------------------------------------------------

#runtime.log.logsystem.class = org.apache.velocity.runtime.log.AvalonLogSystem,org.apache.velocity.runtime.log.SimpleLog4JLogSystem

runtime.log.logsystem.class = org.apache.velocity.runtime.log.Log4JLogSystem

#---------------------------------------------------------------------------
# This is the location of the Velocity Runtime log.
#----------------------------------------------------------------------------

#runtime.log = velocity.log

#----------------------------------------------------------------------------
# This controls if Runtime.error(), info() and warn() messages include the
# whole stack trace. The last property controls whether invalid references
# are logged.
#----------------------------------------------------------------------------

runtime.log.error.stacktrace = false
runtime.log.warn.stacktrace = false
runtime.log.info.stacktrace = false
runtime.log.invalid.reference = true

#----------------------------------------------------------------------------
# Configuration for the Log4JLogSystem.
# You must define the runtime.log.logsystem.class property to be:
#   org.apache.velocity.runtime.log.Log4JLogSystem
#
# You must also include Log4J's .jar files into your classpath. They are
# included with the Velocity distribution in the build/lib directory.
#
# There are several different options that you can configure.
# Uncomment the ones that you want and also define their settings.
#----------------------------------------------------------------------------
#runtime.log.logsystem.log4j.pattern=%d - %m%n
#runtime.log.logsystem.log4j.file.size=100000
#runtime.log.logsystem.log4j.file.backups=1
#runtime.log.logsystem.log4j.syslogd.host=my.syslog.server.com
#runtime.log.logsystem.log4j.syslogd.facility=LOG_DAEMON
#runtime.log.logsystem.log4j.remote.host=my.remote.server.com
#runtime.log.logsystem.log4j.remote.port=1099
#runtime.log.logsystem.log4j.email.server=localhost
#runtime.log.logsystem.log4j.email.from=root@localhost
#runtime.log.logsystem.log4j.email.to=root@localhost
#runtime.log.logsystem.log4j.email.subject=Velocity Error Report
#runtime.log.logsystem.log4j.email.buffer.size=512

#----------------------------------------------------------------------------
# T E M P L A T E  E N C O D I N G
#----------------------------------------------------------------------------

input.encoding=UTF-8
output.encoding=UTF-8

#----------------------------------------------------------------------------
# F O R E A C H  P R O P E R T I E S
#----------------------------------------------------------------------------
# These properties control how the counter is accessed in the #foreach
# directive. By default the reference $velocityCount will be available
# in the body of the #foreach directive. The default starting value
# for this reference is 1.
#----------------------------------------------------------------------------

directive.foreach.counter.name = velocityCount
directive.foreach.counter.initial.value = 1

#----------------------------------------------------------------------------
# I N C L U D E  P R O P E R T I E S
#----------------------------------------------------------------------------
# These are the properties that governed the way #include'd content
# is governed.
#----------------------------------------------------------------------------

directive.include.output.errormsg.start = <!-- include error :
directive.include.output.errormsg.end   =  see error log -->

#----------------------------------------------------------------------------
# P A R S E  P R O P E R T I E S
#----------------------------------------------------------------------------

directive.parse.max.depth = 10

#----------------------------------------------------------------------------
# Resource  loaders
#----------------------------------------------------------------------------

resource.loader =class
#for the loader we call 'class', use the ClasspathResourceLoader
class.resource.loader.description = Velocity Classpath Resource Loader 
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
class.resource.loader.cache = false
#cache forever
class.resource.loader.modificationCheckInterval = 0




#----------------------------------------------------------------------------
# VELOCIMACRO PROPERTIES
#----------------------------------------------------------------------------
# global : name of default global library.  It is expected to be in the regular
# template path.  You may remove it (either the file or this property) if
# you wish with no harm.
#----------------------------------------------------------------------------
#velocimacro.library = VM_global_library.vm

velocimacro.permissions.allow.inline = true
velocimacro.permissions.allow.inline.to.replace.global = false
velocimacro.permissions.allow.inline.local.scope = false

velocimacro.context.localscope = false

#----------------------------------------------------------------------------
# INTERPOLATION
#----------------------------------------------------------------------------
# turn off and on interpolation of references and directives in string
# literals.  ON by default :)
#----------------------------------------------------------------------------
runtime.interpolate.string.literals = true


#----------------------------------------------------------------------------
# RESOURCE MANAGEMENT
#----------------------------------------------------------------------------
# Allows alternative ResourceManager and ResourceCache implementations
# to be plugged in.
#----------------------------------------------------------------------------
resource.manager.class = org.apache.velocity.runtime.resource.ResourceManagerImpl
resource.manager.cache.class = org.apache.velocity.runtime.resource.ResourceCacheImpl

#----------------------------------------------------------------------------
# PLUGGABLE INTROSPECTOR
#----------------------------------------------------------------------------
# Allows alternative introspection and all that can of worms brings
#----------------------------------------------------------------------------

runtime.introspector.uberspect = org.apache.velocity.util.introspection.UberspectImpl

#increase the default size
parser.pool.size = 35

#load macro library
velocimacro.library=library.vm



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值