Maven+SpringMVC整合之helloworld

来源:互联网 发布:Windows测试udp 编辑:程序博客网 时间:2024/05/09 12:04

一、废话

算是一个入门的例子,虽然难度不大,但对于我这种刚接触Maven和SpringMVC的菜鸟来讲还是费了点时间。貌似网上很多教程都是关于在Eclipse下搭建的,不过像我这样的屌丝还是坚持我的Myeclipse之旅吧。Eclipse神马的,=日后再说吧。废话不多说,切入主题。

二、项目搭建流程

都是流程上的东西,记住便可了。下面步骤没有非常细化,自认为比较重要

Step1:

Step2:

控制器Controller编写

[java] view plaincopyprint?
  1. package com.techbirds.web.controller;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import org.springframework.context.annotation.Scope;  
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.ui.Model;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.bind.annotation.RequestParam;  
  10.   
  11. @Controller  
  12. @Scope("prototype")  
  13. public class HelloContorller {  
  14.       
  15.     //hello world例子  
  16.     @RequestMapping(value="/hello")  
  17.     public String hello(){  
  18.         System.out.println("spring mvc hello world!");  
  19.         return "hello";  
  20.     }  
  21.       
  22. }  

Step3:

maven项目的pom.xml配置

[html] view plaincopyprint?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.techbirds.web</groupId>  
  5.     <artifactId>springmvc-hello</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>springmvc-hello Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <properties>  
  11.         <org.slf4j-version>1.6.1</org.slf4j-version>  
  12.     </properties>  
  13.   
  14.     <dependencies>  
  15.         <dependency>  
  16.             <groupId>junit</groupId>  
  17.             <artifactId>junit</artifactId>  
  18.             <version>4.10</version>  
  19.             <scope>test</scope>  
  20.         </dependency>  
  21.   
  22.         <dependency>  
  23.             <groupId>org.springframework</groupId>  
  24.             <artifactId>spring-context</artifactId>  
  25.             <version>3.1.1.RELEASE</version>  
  26.         </dependency>  
  27.         <dependency>  
  28.             <groupId>org.springframework</groupId>  
  29.             <artifactId>spring-webmvc</artifactId>  
  30.             <version>3.1.1.RELEASE</version>  
  31.         </dependency>  
  32.   
  33.   
  34.         <dependency>  
  35.             <groupId>org.apache.geronimo.specs</groupId>  
  36.             <artifactId>geronimo-servlet_2.5_spec</artifactId>  
  37.             <version>1.2</version>  
  38.         </dependency>  
  39.           
  40.   
  41.     </dependencies>  
  42.   
  43. </project>  

 

Step4:

使用外部tomcat部署,方便debug。不过必须每次依赖配置后,redeploy一下,否则依赖的包更新不到tomcat下。

这里顺便记录下:

   因为多个spring版本一起时造成的异常:

                                    java.lang.NoSuchMethodError: org.springframework.beans.MutablePropertyValues.add

 

 

运行成功:hello world!

0 0
原创粉丝点击