1. 环境配置:

  2. 创建项目SpringMVC1

  3. 导入jar包,添加web.xml和springMVC-servle.xml到WEB-INF下

  4. web.xml

 SpringMVC1

 

   index.html

   index.htm

   index.jsp

   default.html

   default.htm

   default.jsp

 

 

   springMVC

   org.springframework.web.servlet.DispatcherServlet

   

    contextConfigLocation

    classpath:com/firefly/springmvc1/config/spring-servlet.xml

   

   1

 

 

   springMVC

   /

 

  1. springMVC-servlet.xml

<beans xmlns=”http://www.springframework.org/schema/beans"  

xmlns:context=”http://www.springframework.org/schema/context"  

xmlns:p=”http://www.springframework.org/schema/p"  

xmlns:mvc=”http://www.springframework.org/schema/mvc"  

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"  

xsi:schemaLocation=”http://www.springframework.org/schema/beans  

     http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">

   

   

   

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix" value="/WEB-INF/content/"></property>

    <property name="suffix" value=".jsp"></property>

</bean>

 

  1. 创建controller层

package com.firefly.springmvc1.controller;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

public class HelloWorld implements Controller{

public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {

    System.out.println("-----hello mvc-----");

    ModelAndView mv = new ModelAndView();

    mv.addObject("msg","hello springmvc");

    mv.setViewName("welcome");

    return mv;

}

}

  1. 创建welcome.jsp文件

  2. 运行测试:输入welcome

  3. 可能遇到的问题

  4. springMVC的xml文件报错,有可能是与以前的spring的xml文件版本的不同,eclipse自动保存了xml版本的信息,清理方法:window->preferences->general->network connections->cache,将里面所有的东西都移除掉

  5. 启动tomcat时找不到springMVC的xml,检查xml的名字