Spring MVC+Maven+Velocity的配置过程

1. 简介

本文通过一个简单的实例展示Spring MVC+Maven+Velocity的配置过程,该配置的重点在于以下几个方面:
  1.  创建Maven工程
  2. Maven依赖包配置
  3. 配置 web.xml文件
  4. Spring配置文件
  5. 编写程序验证配置

2.构建步骤

要构建一个如上图所示的工程,分以下几步实现:

2.1 创建Maven(webapp)工程

Spring MVC+Maven+Velocity的配置过程_第1张图片

最终我们的项目看起来是这样的:

Spring MVC+Maven+Velocity的配置过程_第2张图片

2.2 配置pom.xml依赖

可以在Maven官网:http://search.maven.org/ 进行搜索需要的依赖配置。下面是具体的依赖配置:

pom.xml


	4.0.0
	com.boonya.spring.mvc.velocity
	SpringVelocity
	0.0.1-SNAPSHOT
	war
	http://maven.apache.org

	
		
		    javax.servlet
		    javax.servlet-api
		    3.1.0
		

		
		
			org.springframework
			spring-web
			3.2.5.RELEASE
		
		
			org.springframework
			spring-webmvc
			3.2.5.RELEASE
		
		
			org.springframework
			spring-core
			3.2.5.RELEASE
		

		
		
		    org.apache.velocity
		    velocity
		    1.7
		
		
			com.googlecode.sli4j
			sli4j-slf4j-log4j
			2.0
		
		
			junit
			junit
			4.11
			test
		
		
			net.sf.ehcache
			ehcache-core
			2.6.5
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.13
		
		
			net.sf.json-lib
			json-lib
			2.4
			jdk15
		
		
			org.springframework
			spring-core
			3.2.5.RELEASE
		
		
			org.springframework
			spring-context-support
			3.2.4.RELEASE
			jar
			compile
		
		
			org.mybatis
			mybatis-spring
			1.2.1
		
		
			org.mybatis
			mybatis-ehcache
			1.0.0
		
		
			org.springframework
			spring-jdbc
			3.2.5.RELEASE
		
        
        
            com.alibaba
            druid
            1.0.2
        
		
			org.apache.poi
			poi
			3.8
		
		
		
			org.springframework
			spring-webmvc
			3.2.5.RELEASE
		
		
			commons-httpclient
			commons-httpclient
			3.1
		
		
		
			org.apache.cxf
			cxf-rt-frontend-jaxws
			2.7.6
		
	
		
			wsdl4j
			wsdl4j
			1.6.3
			jar
			compile
		
	
		
			org.springframework
			spring-beans
			3.2.5.RELEASE
		
		
			org.springframework
			spring-test
			3.2.5.RELEASE
			test
		
		
			org.aspectj
			aspectjweaver
			1.7.4
		
        
        
            mysql
            mysql-connector-java
            5.1.12
        
		
		
			org.mybatis
			mybatis
			3.1.1
		
		
			javax
			javaee-api
			6.0
			provided
			
		

		
			javax.mail
			mail
			1.4.7
		
		
			javax.servlet
			jstl
			1.2
		
		
			commons-fileupload
			commons-fileupload
			1.3
		
		
			org.apache.clerezza.ext
			org.json.simple
			0.3-incubating
			compile
		
		
			com.alibaba
			fastjson
			1.1.41
		
		
			dom4j
			dom4j
			1.6
		
		
		 
			com.google.zxing
			core
			3.0.1
		
		
		
			net.coobird 
			thumbnailator
			0.4.8
		

		
			org.springframework
			spring-orm
			3.2.5.RELEASE
		
		 
			org.unitils
			unitils-core
			3.1
			test
		
		
			org.unitils
			unitils-database
			3.1
			test
		 
		
			org.unitils
			unitils-dbmaintainer
			3.1
			test
		
		
			org.unitils
			unitils-dbunit
			3.1
			test
		
		
			org.unitils
			unitils-spring
			3.1
			test
		
		
			org.unitils
			unitils-orm
			3.1
			test
		 
		
            org.quartz-scheduler
            quartz
            1.8.5
          
	
	
		
			

				
					org.apache.maven.plugins
					maven-compiler-plugin
					2.3.2
					
						1.7
						1.7
					
				

				
					org.mybatis.generator
					mybatis-generator-maven-plugin
					1.3.1
					
						false
					
				

				
					org.apache.tomcat.maven
					tomcat7-maven-plugin
					2.1
					
						utf-8
						8888
						/
						false
						false
					
				

				
					org.codehaus.mojo
					tomcat-maven-plugin
					1.1
					
						http://192.168.200.121:8080/manager/text
						tomcat
						password
						/SpringVelocity
					
				

			
		
		
			
				src/main/resources
				
					**/*.properties
					**/*.xml
				
			
			
				src/main/java
				
					**/*.java
					**/.svn/*
				
			
		
	

注:有些没用的依赖可以去掉。

2.3web.xml文件配置

web.xml



    Archetype Created Web Application

    
        spring
        org.springframework.web.context.ContextLoaderListener
    

    
    
        springServlet
        org.springframework.web.servlet.DispatcherServlet
        
            spring mvc
            contextConfigLocation          
            /WEB-INF/spring-servlet.xml 
        
    

    
        springServlet
        *.html 
    


2.4Spring文件配置

Spring通用配置:applicationContext.xml




    
    
	 

由于数据库什么的都没用到,所以去掉了多余的配置,如果要配置完整的可以参考src/resources/main/applicationContext.txt.

SpringMVC的配置:spring-servlet.xml



		
    
    

    
    
    

    
    
         
    
    
        
        
        
         
    

2.5视图控制层Controller代码

SpringVelocityController.java

package com.boonya.spring.mvc.velocity.controller;

import java.util.ArrayList;
import java.util.List;
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 SpringVelocityController  {
  
  @RequestMapping("/velocity")
  public ModelAndView velocityView(HttpServletRequest req, HttpServletResponse resp) throws Exception {
      System.out.println("------------ enter SpringVelocityController");

      List list = new ArrayList();
      list.add("a");
      list.add("b");
      list.add("c");
      return new ModelAndView("velocity", "list", list);// velocity:即SpringVelocity/src/main/webapp/WEB-INF/templates/velocity.vm
  }

}

3.测试项目

3.1测试配置正确性

我们加一个空的JSP页面作为首页:index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




SpringVelocity首页



欢迎来到,SpringVelocity!

启动tomcat访问:http://localhost:8080/SpringVelocity/ 看到以下输出说明配置可以正常启动。

Spring MVC+Maven+Velocity的配置过程_第3张图片

3.2测试Velocity模板输出

在浏览器输入:http://localhost:8080/SpringVelocity/velocity.html 如果看到如下输出则配置成功。

Spring MVC+Maven+Velocity的配置过程_第4张图片


还是那句话,实践出真知。文章有借鉴阿里实习小朋友的(文章上面有误):http://blog.csdn.net/sun_wangdong/article/details/51992194

3.3工程代码下载

http://download.csdn.net/detail/boonya/9752942




你可能感兴趣的:(Spring MVC+Maven+Velocity的配置过程)