现在的Web框架基本都采用了MVC(model-view-Controller)设计模式,其中,Servlet和Filter都可以充当控制器.Spring MVC采用一个Servlet作为控制器,而Struts2则采用的Filter作为控制器.
对于他们的区别我在这里不多赘述,可以参见这篇文章SpringMVC与Struts2区别与比较总结
Spring MVC的搭建可以通过Java代码的方式(在《Spring 实战》一书中有详细讲解),也可以通过配置文件的方式,这里讲到的是运用配置文件的方式进行搭建.
这是一个毫无技术含量的最简单例子
使用工具
当然啦~如果你不想使用Maven可以直接跳过前两步
Demo结构

搭建一个Web项目
可以参见Idea 使用 Maven 搭建 Web 项目
添加依赖
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 | <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-core</artifactId>   <version>RELEASE</version> </dependency> <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-context</artifactId>   <version>RELEASE</version> </dependency> <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-beans</artifactId>   <version>RELEASE</version> </dependency> <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-expression</artifactId>   <version>RELEASE</version> </dependency> <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-web</artifactId>   <version>RELEASE</version> </dependency> <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-webmvc</artifactId>   <version>RELEASE</version> </dependency> <dependency>   <groupId>junit</groupId>   <artifactId>junit</artifactId>   <version>4.12</version>   <scope>test</scope> </dependency>
 | 
配置classpath
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 | <build>     <finalName>demo</finalName>     <!--配置Classpath-->     <resources>       <resource>         <directory>src/main/java</directory>       </resource>       <resource>         <directory>src/main/resources</directory>         <includes>           <include>**/*xml</include>           <include>**/*.properties</include>         </includes>       </resource>     </resources>   </build>
 | 
配置web.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 | <!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app>   <!--spring的配置文件-->   <context-param>     <param-name>contextConfigLocation</param-name>     <param-value>classpath:applicationContext.xml</param-value>   </context-param>   <!--Spring MVC必须的servlet,他会帮你把Spring的配置文件到application域中-->   <servlet>     <servlet-name>dispatcher-servlet</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <!--Spring MVC的配置文件-->     <init-param>       <param-name>contextConfigLocation</param-name>       <param-value>classpath:dispatcher-servlet.xml</param-value>     </init-param>     <load-on-startup>1</load-on-startup>   </servlet>   <servlet-mapping>     <servlet-name>dispatcher-servlet</servlet-name>     <url-pattern>/</url-pattern>   </servlet-mapping> </web-app>
 | 
配置applicationContext.xml
| 1
 | <context:component-scan base-package="com.geek"/>
 | 
配置dispatcher-servlet.xml
| 1 2 3 4 5 6 7 8 9 10
 | <!--启动Spring MVC的注解--> <mvc:annotation-driven/> <!--自动扫描包--> <context:component-scan base-package="com.geek"/> <!--视图解析器--> <bean id="viewResolver"       class="org.springframework.web.servlet.view.InternalResourceViewResolver">     <property name="suffix" value=".jsp"/> </bean>
 | 
<mvc:annotation-driven/>这句话非常关键!如果不写会导致@Controller注解不能使用
测试
TestController.java
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14
 | @Controller public class TestController {     @Autowired     private TestService service;     @RequestMapping("/test")     public String Test(String s, HttpServletRequest request){         service.show();         request.setAttribute("s",s);         return "index";//由于前面配置了视图解析器,此处会跳转到index.jsp页面     } }
 | 
在这里使用了@Controller注解,逻辑上标志着这个类是一个控制器,让编程过程中更容易理解
其中的@RequestMapping("/test")指将这个url(localhost:8080/test)映射到此方法上,在Spring MVC中以一个方法为上下文进行拦截.
TestService.java
| 1 2 3 4 5 6 7 8
 | @Service public class TestService {     public void show(){         System.out.println("success");     } }
 | 
index.jsp
| 1 2 3 4 5 6 7 8 9
 | <%@page pageEncoding="UTF-8" contentType="text/html"%> <html> <body> <%     String s = (String) request.getAttribute("s");     out.print(s!=null?s:"hello"); %> </body> </html>
 | 
启动服务器输入  http://localhost:8080/test?s=123456
可以看到控制台打印success,index.jsp页面的显示由hello变为123456
这样,Spring MVC 的一个小Demo就算完成了,当然,这里面使用了Spring的自动装配,整体上非常的简单例子,动手试试吧~