SpringMVC_01 SpringMVC五大组件、SpringMVC编程步骤(不使用注解进行配置)、SpringMVC编程步骤(利用注解进行配置)、参数获取、响应数据
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>
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>
3.4 写Controller(处理器)
3.5 写jsp
3.6 配置HandlerMapping,ViewResolver
4 SpringMVC编程步骤(利用注解实现)
step1. 导包。
step2. 添加一个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 <context:component-scan base-package="test"></context:component-scan> 20 <!-- 配置mvc注解扫描 --> 21 <mvc:annotation-driven></mvc:annotation-driven> 22 23 <!-- 配置ViewResolver --> 24 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 25 <property name="prefix" value="/WEB-INF/"/> 26 <property name="suffix" value=".jsp"/> 27 </bean> 28 29 </beans>
step3. 配置DispatcherServlet。
step4. 写Controller(处理器)。
要求:
a. 不用实现Controller接口。
b. 可以添加多个方法。
c. 方法名不做要求,返回值可以是ModelAndView, 也可以是String。
d. 将@Controller添加到类名前。
e. 将@RequestMapping添加到类名前或者方法前。
step5.写jsp。
step6.配置ViewResolver,组件扫描,mvc注解扫描。
注:只有配置了mvc注解扫描,@RequestMapping才起 作用
5 参数获取、响应数据
待更新...2017年5月13日22:41:15
本博客源代码2:点击前往
本博客源代码1:点击前往