SpringMVC整合Velocity要点
2016-03-02 14:30
399 查看
本文主要整理了SpringMVC整合Velocity的一些步骤和要点:
1、在pom.xml增加依赖的velocity包
2、在servlet-context.xml中的配置如下:
<!--配置spring的扫描器实现bean的自动载入-->
<context:component-scan base-package="com.xtt.webdemo.**.controller"/>
<!--属性占位器配置-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:config.properties</value>
</array>
</property>
</bean>
<!--内容协商管理工厂Bean配置-->
<!--1、检查扩展名(如my.pdf);2、检查Parameter(如my?format=pdf);3、检查Accept Header-->
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="favorParameter" value="false" />
<property name="ignoreAcceptHeader" value="true" />
<!--扩展名到MIME的映射;favorPathExtension, favorParameter是true时起作用 -->
<property name="mediaTypes">
<props>
<prop key="json">application/json</prop>
<prop key="xml">application/xml</prop>
</props>
</property>
</bean>
<!--开启自动扫描驱动,并配置消息转换器-->
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="true">
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="features">
<value>WriteDateUseDateFormat</value>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- velocityConfig -->
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
<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="suffix" value=".vm"/>
<property name="contentType" value="text/html;charset=utf-8"/>
<property name="layoutUrl" value="layout/layout.vm"/>
<property name="requestContextAttribute" value="ac"/>
<property name="order" value="1"/>
<property name="numberToolAttribute" value="numbertool"/>
<property name="dateToolAttribute" value="datetool"/>
</bean>
3、Controller编写
1、在pom.xml增加依赖的velocity包
<!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>3.2.0.RELEASE</version> </dependency>
<!--velocity--> <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>
2、在servlet-context.xml中的配置如下:
<!--配置spring的扫描器实现bean的自动载入-->
<context:component-scan base-package="com.xtt.webdemo.**.controller"/>
<!--属性占位器配置-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:config.properties</value>
</array>
</property>
</bean>
<!--内容协商管理工厂Bean配置-->
<!--1、检查扩展名(如my.pdf);2、检查Parameter(如my?format=pdf);3、检查Accept Header-->
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="favorParameter" value="false" />
<property name="ignoreAcceptHeader" value="true" />
<!--扩展名到MIME的映射;favorPathExtension, favorParameter是true时起作用 -->
<property name="mediaTypes">
<props>
<prop key="json">application/json</prop>
<prop key="xml">application/xml</prop>
</props>
</property>
</bean>
<!--开启自动扫描驱动,并配置消息转换器-->
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="true">
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="features">
<value>WriteDateUseDateFormat</value>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- velocityConfig -->
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
<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="suffix" value=".vm"/>
<property name="contentType" value="text/html;charset=utf-8"/>
<property name="layoutUrl" value="layout/layout.vm"/>
<property name="requestContextAttribute" value="ac"/>
<property name="order" value="1"/>
<property name="numberToolAttribute" value="numbertool"/>
<property name="dateToolAttribute" value="datetool"/>
</bean>
3、Controller编写
@Controller public class IndexController { @RequestMapping("/") public String index(Model model) { model.addAttribute("name", "tester"); return "index"; } @RequestMapping("/error/404") public String error404() { return "404"; } @RequestMapping("/error/500") public String error500() { return "500"; } }
相关文章推荐
- html文件中jquery与velocity变量中的$冲突的解决方法
- velocity显示List与Map的方法详细解析
- JAVA velocity模板引擎使用实例
- Velocity基本语法介绍
- Spring MVC 集成 Velocity 配置
- Velocity入门例子
- Velocity读取后台数据
- 树形结构
- Spring整合Dwr,Cannot locate BeanDefinitionParser for element [annotation-scan]
- spring3 MVC手工搭建Spring3项目demo
- spring mvc 开发需要的jar包的用途详解
- velocity 注释语法
- 基于注解的 Spring MVC 简单入门-网摘
- [MVC] 深入浅出Spring MVC
- velocity include
- Velocity浅析及与Jsp、Freemarker对比
- Velocity 官方实例学习二 (context_example)
- Velocity 官方实例学习一 (app_example1)
- Velocity入门指南——第一章 快速入门
- Velocity入门指南——第二章 注释