1 什么是SpringMVC
是一个mvc框架,用来简化基于mvc架构的web应用程序的 开发。
2 SpringMVC五大组件
DispatcherServlet (前端控制器)
HanlderMapping
Controller (处理器)
ModelAndView
ViewResolver (视图解析器)
2.1 springMVC 的五大组件怎么协同工作
》DispatcherServlet收到请求之后,依据 HandlerMapping的配置调用相应的处理器来处理。
》处理器将处理结果封装成ModelAndView, 然后返回给DispatcherServlet。
》DispatcherServlet依据ViewResovler的 解析,调用相应的视图对象(比如某个jsp)来生成 相应的页面。
3 SpringMVC编程步骤
3.1 导包 : spring-webmvc
》需要配置运行环境为 Tomcat
3.2 添加一个spring的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 12 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 16 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 17 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 18 19 <!-- 配置HandlerMapping --> 20 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 21 <property name="mappings"> 22 <props> 23 <prop key="/hello.do">helloController</prop> 24 </props> 25 </property> 26 </bean> 27 <bean id="helloController" class="test.TestController"/> 28 29 <!-- 配置ViewResolver --> 30 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 31 <property name="prefix" value="/WEB-INF/"/> 32 <property name="suffix" value=".jsp"/> 33 </bean> 34 35 </beans>
spring配置文件模板
3.3 配置DispatcherServlet
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 3 <display-name>springmvc01s</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <servlet> 14 <servlet-name>springmvc</servlet-name> 15 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 16 <!-- 17 DispatcherServlet的初始化方法会启动spring容器, 18 contextConfigLocation用于指定spring配置文件的位置 19 --> 20 <init-param> 21 <param-name>contextConfigLocation</param-name> 22 <param-value>classpath:spring-mvc.xml</param-value> 23 </init-param> 24 <load-on-startup>1</load-on-startup> 25 </servlet> 26 <servlet-mapping> 27 <servlet-name>springmvc</servlet-name> 28 <url-pattern>*.do</url-pattern> 29 </servlet-mapping> 30 </web-app>
DispatcherServlet配置文件
3.4 写Controller(处理器)
3.5 写jsp
3.6 配置HandlerMapping,ViewResolver
本博客源代码:点击前往
时间: 2024-10-29 10:46:24