1. 在web.xml中配置 DispatcherServlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
    <!-- 配置DispatcherServlet -->
    <servlet>
    	<servlet-name>springDispatcherServlet</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	
    	<init-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:springmvc.xml</param-value>
    	</init-param>
    	<load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- 
      /:  拦截所有请求,不拦截jsp页面,  *.jsp页面
      /*: 拦截所有请求,拦截jsp页面
      处理 *.jsp是tomcat做的事, 所有项目的小web.xml都是集成于大web.xml
      DefaultServlet是tomcat中处理静态资源的。
         除过jsp,servlet以外的都是静态资源
         *.html就是静态资源,tomcat就会在服务器下找到这个资源并返回。
         这里的前端控制器的/禁用了tomcat服务器的DefaultServlet
     -->
    <servlet-mapping>
    	<servlet-name>springDispatcherServlet</servlet-name>
    	<url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2. 加入Spring MVC的配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	 <!-- springmvc只是控制网站跳转逻辑 -->
	 <!-- 只扫描控制器 -->
     <context:component-scan base-package="com.atChina" use-default-filters="false">
     	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
     </context:component-scan>

	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<mvc:annotation-driven></mvc:annotation-driven>
	<mvc:default-servlet-handler/>
</beans>

3. 编写处理请求的处理器,并标识为处理器

package com.atChina.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorld {

	/*
	 *  1. @RequestMapping: 映射请求的URL
	 *  2. 返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver视图解析器来说,
	 *  会做如下的解析: 通过prefix+返回值+suffix的方式得到实际的物理视图,然后做转发操作
	 *  
	 */
	@RequestMapping("/helloworld")
	public String hello(){
		System.out.println("Hello World");
		return "success"; // /WEB-INF/pages/success.jsp
	}
}

4. 编写视图

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	你好啊
</body>
</html>

 

工程结构如下;

springmvc一:编写简单的HelloWorld_xml

 

 web.xml也可以如下进行配置:(使用默认方式)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
    <!-- 配置DispatcherServlet -->
    <servlet>
    	<servlet-name>springDispatcherServlet</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	
    	<!-- 实际上也可以不通过contextConfigLocation来配置SpringMVC的配置文件,而使用默认的配置文件.
    	     默认的配置文件为:   /WEB-INF/<servlet-name>-servlet.xml
    	
    	<init-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:springmvc.xml</param-value>
    	</init-param>
    	 -->
    	<load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
    	<servlet-name>springDispatcherServlet</servlet-name>
    	<url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

这时候的工程结构如下: 

springmvc一:编写简单的HelloWorld_mvc_02