spring-mvc(一)spring-mvc原理小结

来源:互联网 发布:盖尔霍华德 软件 编辑:程序博客网 时间:2024/05/16 01:35

简介


springmvc作为前端mvc框架的后起之秀,与之前的struts类似,但是更为灵活,配置简单和spring以及火热的restful结合的更好。

 

原理

 

 




对应上面那张图,结合springmvc的源码先从web.xml来讲起。当容器启动,加载web.xml。这里无论是基本的jspservlet服务器,还是支持更加广泛的jboss都可以来支持springmvc


1.web.xml内配置了对应的url路径就会请求对应的DispatcherServlet


2.找到dispatcherservlet这个类之后呢?执行doService方法,经过一些判断之后在执行doDispatch方法,这个方法就是用来顺序中心HandMapping的。如果没有在HandMapping有相应的controller则调用对应的controller即可


3.因为spring-mvc是方法级别的,如果返回的的是字符串则转化为视图对象;如果是ModelAndView则直接返回即可。



4.DispatcherServlet接受到数据通过ViewResolver来解析渲染到服务器端,整个就是这样大概这样一个过程。


简单配置



下载地址http://repo.spring.io/libs-release-local/org/springframework/spring/




xml 配置



Web.xml


Web.xmlsun公司对于jsp应用定制的,也就是说对于所有的jsp应用的都会这是一般是任何框架的入口,所以无论是什么样的框架都会从这个入口进入


<servlet>         <servlet-name>springMVC</servlet-name>         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  </servlet>  <servlet-mapping>         <servlet-name>springMVC</servlet-name>         <url-pattern>/*</url-pattern>  </servlet-mapping>


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:mvc="http://www.springframework.org/schema/mvc"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-3.0.xsd            http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd            http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx-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/context            http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <!--        使Spring支持自动检测组件,如注解的Controller    -->    <context:component-scan base-package="com.minx.crm.web.controller"/>    <bean name="/test/helloworld" class="com.cfl.controller.HelloWorldController"></bean>    <bean id="viewResolver"          class="org.springframework.web.servlet.view.InternalResourceViewResolver"          p:prefix="/WEB-INF/jsp/"          p:suffix=".jsp" /></beans>

controller

 

package com.cfl.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class HelloWorldController implements Controller{@Overridepublic ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {// TODO Auto-generated method stubSystem.out.println("helloword");return new ModelAndView("welcome");}}



 综上,相比struts上springmvc结合注解的方式更大的解放了程序猿的工作量,另外和spring结合的更好,从配置controller上来


讲,也是和spring的习惯是一样的。这篇只能算是一个非常入门级的说明,更多springmvc相关几大组件都没有深究,值得学习。

1 0