SpringMVC 集成velocity

前言

       没有美工的时代自然少不了对应的模板视图开发,jsp时代我们用起来也很爽,物极必反,项目大了,数据模型复杂了jsp则无法胜任。

 

开发环境

      idea2016、jdk1.8、tomcat8.0.35

      spring 4.3.6、velocity1.6、velocity-tools 2.0

 

SpringMVC集成velocity

1、VelocityViewResolver。

<!--VelocityViewResolver视图配置-->
    <!--<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">-->
        <!--<property name="suffix" value=".vm"/>-->
        <!--<property name="prefix" value=""/>-->
        <!--<property name="contentType" value="text/html;charset=UTF-8"/>-->
    <!--</bean>-->

 

2、VelocityLayoutViewResolver。也就是可以自定义模板布局。和VelocityViewResolver相比较的话多了一个layout.vm,在WEB-INF/views/layout/layout.vm配置。

<!--VelocityLayoutViewResolver-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
        <property name="suffix" value=".vm"/>
        <property name="prefix" value=""/>
        <property name="contentType" value="text/html;charset=UTF-8"/>
        <property name="layoutUrl" value="layout/layout.vm"/>
    </bean>

 

3、完整的springmvc-servlet.xml

 <context:component-scan base-package="com.autohome.controller" />
    <!-- 注解驱动 -->
    <mvc:annotation-driven />


    <!--velocity模板配置-->
    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/views/"/>
        <property name="configLocation" value="classpath:velocity.properties"/>
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
            </props>
        </property>
    </bean>
    <!--VelocityViewResolver视图配置-->
    <!--<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">-->
        <!--<property name="suffix" value=".vm"/>-->
        <!--<property name="prefix" value=""/>-->
        <!--<property name="contentType" value="text/html;charset=UTF-8"/>-->
    <!--</bean>-->

    <!--VelocityLayoutViewResolver-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
        <property name="suffix" value=".vm"/>
        <property name="prefix" value=""/>
        <property name="contentType" value="text/html;charset=UTF-8"/>
        <property name="layoutUrl" value="layout/layout.vm"/>
    </bean>

4、pom.xml

 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity</artifactId>
      <version>1.6.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity-tools</artifactId>
      <version>2.0</version>
    </dependency>
  </dependencies>

 我的layout.vm. 视图页嵌套的内容就在$screen_content中渲染

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>

    </head>
    <body>
        <div>header</div>
        <div>
            $screen_content
        </div>
        <div>footer</div>
    </body>

</html>

index.vm

<h2>${msg}</h2>

layout.vm的渲染结果

 

总结

      1、pom下载spring-context.jar包时并没有自动加载spring-context-support.jar  然后我在springmvc-servlet编写velocityconfig VelocityConfigurer类时找不到configLocation属性。后加上spring-context jar包后OK。

      2、既然在velocity.properties文件中已经配置了编码格式,还要在velocityconfig中继续配置编码方式,如果不配置的话则抛一个spring.vm相关的异常, 不知道这个设计何为。

 

参考资料

http://shishi11.iteye.com/blog/869290

 

IT文库 » SpringMVC 集成velocity
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址