spring3集成velocity配置

来源:互联网 发布:2017编程笔记本 编辑:程序博客网 时间:2024/06/11 23:42

今天说一下spring3集成velocity的例子,供自己以后参考使用。本文的搭建使用到了 spring、springmvc、mybatis、velocity框架。还是建议大家使用maven来构建,这样就不用考虑 jar 的依赖问题.


本人项目的构建环境:

  • MyEclipse 2014
  • Jdk 1.6
  • Tomcat7

1、使用myEclipse创建一个web项目

项目目录结构

                                   

该项目需要用到的jar包(websocket、spring-data-mongodb不需要)



2、在web.xml配置spring和springmvc的入口

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">  <display-name>ssm</display-name>    <!-- 配置字符编码 -->  <filter>  <filter-name>encoding</filter-name>  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  <init-param>  <param-name>encoding</param-name>  <param-value>UTF-8</param-value>  </init-param>  </filter>  <filter-mapping>  <filter-name>encoding</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>   <!-- 配置springmvc的核心控制器 -->  <servlet>  <servlet-name>springmvc</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:com/pzdf/ssm/config/spring-mvc.xml</param-value>  </init-param>  </servlet>  <servlet-mapping>  <servlet-name>springmvc</servlet-name>  <url-pattern>*.do</url-pattern>  </servlet-mapping>    <!-- 配置监听器,启动spring容器 -->  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/applicationContext.xml</param-value>  </context-param></web-app>

3、spring的总配置文件

<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- 引入外部的jdbc配置文件 --><context:property-placeholder location="classpath:com/pzdf/ssm/config/jdbc.properties"/><!-- 配置 dbcp数据源 --><bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="${driverName}"></property><property name="url" value="${url}"></property><property name="username" value="${username}"></property><property name="password" value="${password}"></property><!-- 配置连接池初始化连接大小 --><property name="initialSize" value="${initialSize}"></property><!-- 配置连接池的最大数量 --><property name="maxActive" value="${maxActive}"></property><!-- 配置连接池的最大空闲 --><property name="maxIdle" value="${maxIdle}"></property><!-- 配置连接池的连接的最长等待时间 --><property name="maxWait" value="${maxWait}"></property></bean><!-- 配置sqlSessionFactory --><bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 配置数据源 --><property name="dataSource" ref="dataSource"></property><!-- 配置别名的映射 --><property name="typeAliasesPackage" value="com.pzdf.ssm.pojo"></property><!-- 读取mybatis的配置文件,主要读取延迟加载的配置 -->  <property name="configLocation" value="classpath:com/pzdf/ssm/config/mybatis.xml"></property><!-- 配置读取映射文件 --><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean><!-- 将dao接口和mapper映射 --><bean name="mapperConfig" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 配置dao接口所在的包 --><property name="basePackage" value="com.pzdf.ssm.dao"></property><!-- 配置sqlSessionFacotry --><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean><!-- 配置事务管理器 --><bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 自动扫描注解包 --><context:component-scan base-package="com.pzdf.ssm"></context:component-scan></beans>

4、springmvc的配置文件

<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- 配置handlerMapping --><mvc:annotation-driven></mvc:annotation-driven><!-- 放过静态资源 --><mvc:default-servlet-handler/><!-- 配置velocity引擎 --><bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"><!-- 模板的加载路径 --><property name="resourceLoaderPath" value="/WEB-INF/views/"></property><property name="velocityProperties"><props><prop key="input.encoding">utf-8</prop><prop key="output.encoding">utf-8</prop><prop key="contentType">text/html;charset=UTF-8</prop></props></property></bean><!-- 配置viewResolver --><bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"><property name="cache" value="true"></property><!-- 视图类,这里使用的自己重写的类。后面说为什么 --><property name="viewClass" value="com.pzdf.ssm.utils.VelocityToolbox2View"></property><!-- 前缀 --><property name="prefix" value=""></property><!-- 后缀 --><property name="suffix" value=".html"></property><!-- 编码 --><property name="contentType" value="text/html;charset=UTF-8"></property><!-- velocity工具配置,这个xml可以在org.apache.velocity.tools.generic中找到 --><property name="toolboxConfigLocation" value="/WEB-INF/tools.xml"></property></bean><!-- 扫描controller包 --><context:component-scan base-package="com.pzdf.ssm.controller"></context:component-scan></beans>

5、重写后的 VelocityToolbox2View.java 类

package com.pzdf.ssm.utils;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.velocity.context.Context;import org.apache.velocity.tools.Scope;import org.apache.velocity.tools.ToolboxFactory;import org.apache.velocity.tools.config.ConfigurationUtils;import org.apache.velocity.tools.config.XmlFactoryConfiguration;import org.apache.velocity.tools.view.ViewToolContext;import org.springframework.web.servlet.view.velocity.VelocityToolboxView;/** * 目前的spring3只能支持velocity-tools的1.x版本,无法使用velocity tools 2.x版本,所以需要适配一下 * @author chenkangjing * @date 2017年2月13日 * @version 1.0 */public class VelocityToolbox2View extends VelocityToolboxView {@Overrideprotected Context createVelocityContext(Map<String, Object> model,HttpServletRequest request, HttpServletResponse response)throws Exception {ViewToolContext context = new ViewToolContext(getVelocityEngine(), request, response, getServletContext());if(getToolboxConfigLocation() != null){XmlFactoryConfiguration factoryConfiguration = new XmlFactoryConfiguration();factoryConfiguration.read(getServletContext().getResourceAsStream(getToolboxConfigLocation()));ToolboxFactory factory = factoryConfiguration.createFactory();factory.configure(ConfigurationUtils.getVelocityView());for (String scope : Scope.values()) {context.addToolbox(factory.createToolbox(scope));}}if(model != null){context.putAll(model);}return context;}}

6、编写controller来进行测试
package com.pzdf.ssm.controller;import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/moveController")public class MoveController {@RequestMapping(value="/move.do")public ModelAndView move(){ModelAndView mv = new ModelAndView("/sse/websocket");List<String> language = new ArrayList<String>(0);language.add("react");language.add("angular");language.add("node");language.add("signlar");language.add("typeScript");mv.addObject("languages", language);return mv;}}

7、html界面代码

<!DOCTYPE html><html>  <head>    <title>websocket.html</title>    <meta name="keywords" content="keyword1,keyword2,keyword3">    <meta name="description" content="this is my page">    <meta name="content-type" content="text/html; charset=UTF-8">      </head>    <body>  ##遍历集合中的值   #foreach($lang in $!{languages})<p>$!{lang}</p>      #end   <hr/>  </body></html>

到这里就算集成完成了.


1 0
原创粉丝点击