1.导入jar
1)导入Spring的四个核心jar。 spring-beans.jar ,spring-core.jar, spring-context.jar, spring-expression.jar
2)日志的jar 。 commons-logging.jar
3)SpringMVC中jar , spring-context-support:工具类,UI,缓存类
spring-webmvc.jar : SpringMVC框架的实现包。
两个jar都位于Spring的核心文件夹/libs/
4)注解式开发需要spring-aop.jar 。 位于Spring核心文件夹/libs/spring-aop.jar
2.定义SpringMVC的配置文件,配置文件和Spring的是一样
注册组件扫描器<context>标签。 引入spring-context.xsd约束文件
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 注册组件扫描器 --> <context:component-scan base-package="com.sinosoft.controllers"/> <!-- 注册视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
3.在web.xml注册中央调度器(DispatcherServlet)。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>01</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 注册中央调度器 --> <servlet> <servlet-name>springmvc01</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> <servlet-mapping> <servlet-name>springmvc01</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
4.定义处理器,接收用户的请求。对请求作出处理。
@Controller:表示处理器类
@RequestMapping:在方法上定义处理器方法处理的请求URI,在类上定义命名空间
package com.sinosoft.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value="/user") public class MyController { @RequestMapping(value={"/some.do","/two.do"}) public ModelAndView doSome(){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","doSome annotion SpringMVC"); mv.setViewName("show"); return mv; } @RequestMapping(value="/other.do") public ModelAndView doOther(){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","doOther annotion SpringMVC"); mv.setViewName("show"); return mv; } }
5)定义显示处理结果的页面(视图)