一、导入springMVC所需要的jar包
下载地址:http://repo.spring.io/release/org/springframework/spring/
二、springMVC框架配置
1、web.xml配置
1 <servlet> 2 <servlet-name>spring</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <load-on-startup>1</load-on-startup> 5 </servlet> 6 <servlet-mapping> 7 <servlet-name>spring</servlet-name> 8 <url-pattern>*.do</url-pattern> 9 </servlet-mapping>
2、(在WebRoot/WEB-INF下)创建spring配置文件
注意:此配置文件名称必须为 :servletName-servlet.xml (servletName即为web.xml中核心servlet的name),此处名称为spring-servlet.xml 如果不在WebRoot/WEB-INF下创建,则需要在web.xml中作相应配置,否则文件找不到。spring-servlet.xml配置:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!--1 MVC注解支持 --> <mvc:annotation-driven /> <!--2 注解扫描目录--> <context:component-scan base-package="com.sxdx"></context:component-scan> <!--3 处理静态文件 --> <mvc:default-servlet-handler /> <!-- 视图解析器 --> <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages/" /> <property name="suffix" value=".jsp" /> </bean> --> </beans>
3、创建Controller控制器(Action)
1 package com.sxdx.hello; 2 import javax.servlet.http.HttpServletRequest; 3 import javax.servlet.http.HttpServletResponse; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.servlet.ModelAndView; 7 8 @Controller 9 public class demo { 10 @RequestMapping(value="/demo1") 11 public String demo1(HttpServletRequest request, 12 HttpServletResponse response){ 13 System.out.println("demo1==="); 14 return ""; 15 } 16 }
4、浏览器访问 http://localhost:8080/spring01/demo1.do 后台输出demo1===