(三)velocity--springmvc整合velocity

还是在springmvc工程上进行整合

  • 添加jar包
右键pom.xml,菜单maven-->Add Dependency ,可以搜索添加velocity和velocity-tools两个包,另外还需要添加spring-context-support,不然会报错


  • 添加vm文件
在WEB-INF下新建了一个views文件夹,里面加了两个文件,一个用来做模板,一个具体页面。

layout.vm

    <head>
        <title>Spring MVC and Velocity</title>
    </head>
    <body>
        <h1>Spring MVC and Velocity</h1>
        $screen_content
        <hr />
        Copyright &copy 2016 lkf
    </body>
</html>

hello.vm

welcome ${user}

  • 配置

在servlet的配置文件中,加入velocity的相关配置,注意路径
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <mvc:annotation-driven />
    
    <context:component-scan base-package="com.lucky.study"/>
    <bean id="velocityConfig"
		class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
		<property name="resourceLoaderPath" value="/WEB-INF/views/" />
		<property name="velocityProperties">    
        <props>    
            <prop  key="input.encoding">UTF-8</prop>    
            <prop  key="output.encoding">UTF-8</prop>      
         </props>    
     </property> 
     </bean> 
  	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
		<property name="cache" value="true" />
		<property name="prefix" value="" />
		<property name="layoutUrl" value="layout.vm" />
		<property name="suffix" value=".vm" />
		<property name="contentType"><value>text/html;charset=UTF-8</value></property>  
	</bean> 
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/views/" />  
        <property name="suffix" value=".jsp" />  
    </bean>   
</beans>

  • java文件

package com.lucky.study.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

	 @RequestMapping(value = "/hello") 
	 public ModelAndView hello() {   
	        ModelAndView mv = new ModelAndView();   
	        mv.addObject("user", "lucky");  
	        //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面  
	        mv.setViewName("hello");  
	        return mv;                                        
	 }  
}

运行后 访问http://localhost:8080/velocity/hello.do

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值