Spring MVC

来源:互联网 发布:俄罗斯时间知乎 编辑:程序博客网 时间:2024/06/06 05:37

一、什么是Spring MVC
Spring MVC是一个mvc的开源框架,可以说是spring的一个模块

二、Spring和Spring MVC的区别
1、Spring是一个轻量级的IOC(控制反转)和AOP(切面编程)的容器框架
2、SpringMVC是Spring的一部分,是一个mvc的开源框架,主要由前端控制器、处理器映射器和视图解析器构成

三、Spring MVC 配置
1、框架原理
<1>发起请求到前端控制器(Dispatcherservlet)
<2>前端控制器请求HandlerMapping查找Handler
<3>查找到Handler后,HandlerMapping向前端控制器返回Handler
<4>前端控制器调用处理器适配器(HandlerAdapter)去执行Handler
<5>Handler执行完成后给处理器适配器返回ModelAndView,处理器适配器再将其返回给前端控制器,ModelAndView是SpringMVC框架的一个底层对象,包括Model和View
<6>前端控制器请求视图解析器(ViewResolver)去进行视图解析,根据逻辑视图名解析真正的视图
<7>视图解析器向前端控制器返回view
<8>前端控制器进行视图渲染,视图渲染将模型数据,(在ModelAndView对象中)添加到request域,并向用户响应结果
2、前端控制器配置

<servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>    <!-- contextConfigLocation配置springmvc加载的配置文件,如果不配置,默认加载的    是/WEB-INF/servlet名称-servlet.xml -->        <param-name>contextConfigLocation</param-name>        <param-value>classpath:springmvc-servlet.xml</param-value>    </init-param>  </servlet>  <servlet-mapping>    <servlet-name>springmvc</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>

2.1、非注解方式配置
所有的映射器实现HandlerMapping接口,多个映射器可以并存,前端控制器判断url能让那些映射器映射

两种映射器:

org.springframework.web.servlet.handler.BeanNameUrlHandlerMappingorg.springframework.web.servlet.handler.SimpleUrlHandlerMapping

两种处理器:

org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapterorg.springframework.web.servlet.mvc.HttpRequestHandlerAdapter

视图解析器

org.springframework.web.servlet.view.InternalResourceViewResolver

springmvc.xml配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"        xmlns:mvc="http://www.springframework.org/schema/mvc"        xmlns:context="http://www.springframework.org/schema/context"        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-4.0.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置Handler --> <bean id="queryclients" name="/queryfclients.action" class="cn.labelnet.controller.InfosController"></bean> <bean id="queryclient" class="cn.labelnet.controller.InfosController1"></bean><!-- 配置映射器适配器:将bean的name作为URl进行查找,需要在配置handler时指定的beanname就是URL--><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean><!-- 简单URL映射器 --><bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">   <property name="mappings">     <props>        <!-- 对handler进行URL映射,url是地址queryfclients.action,后面是handler的id -->        <prop key="/queryfclients1.action">queryclients</prop>        <prop key="/queryclient2.action">queryclient</prop>     </props>   </property></bean><!-- 配置处理器适配器 ,所有的处理器适配器都实现Handleradapter接口--><bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean><!-- 另一个适配器 --><bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <!-- 在这里可以配置前缀和后缀 --><!--   <property name="prefix" value=".jsp"></property>  <property name="suffix" value="路径"></property> --> </bean></beans>

实现Controller接口,对应的实现适配器是:org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;

实现HttpRequestHandler接口,对应的实现适配器是:org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter

2.2、注解方式配置

Springmvc.xml配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"        xmlns:mvc="http://www.springframework.org/schema/mvc"        xmlns:context="http://www.springframework.org/schema/context"        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-4.0.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.0.xsd">                        <!-- scan the package and the sub package -->    <context:component-scan base-package="test.SpringMvc"/>    <!-- 注解映射器 -->      <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->      <!-- 注解适配器 -->      <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->     <!-- 可以代替上面的注解映射器和适配器,推荐 -->    <mvc:annotation-driven />    <!-- configure the InternalResourceViewResolver -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!-- 前缀 -->        <property name="prefix" value="/WEB-INF/views/" />        <!-- 后缀 -->        <property name="suffix" value=".jsp" />    </bean></beans>

Handler实现
@Controller表示是控制器
@RequestMapping(“/url”)

package test.SpringMvc;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloWorld {    @RequestMapping("/helloworld")    public String hello(){        System.out.println("hello world");        //返回视图名称        return "success";    }}

测试:http://localhost:8080/springMvc/helloworld

引用:http://blog.csdn.net/lablenet/article/details/50456709

0 0
原创粉丝点击