一、MVC 设计模式
MVC 模式代表 Model-View-Controller(模型-视图-控制器)
在实际的web开发过程中,简单粗暴的看法就是:
Model就是Entity类和相关DAO
Controller就是业务逻辑代码
View就是jsp页面
仔细分析一下这个设模式给我们带来的好处。web开发中,他最大的好处就是把用户所在的视图层和面向对象的模型、和具体的业务代码给分开了。MVC就是用这样一种解耦的方式。从JSP + servlet + javabean,到Struts2到spring MVC都是用了这一种设计模式。
甚至可以这样理解:在前后端分离的场景下,使用@RestController。JSON是服务器端MVC框架的V,是前端MVC框架的M
二、SpringMVC
而Spring Web MVC就是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦。
三、hello springMVC
新建web项目,并引入依赖,这里我直接用mven引的在web.xml中配置servlet为springMVC的DispatchcerServlet
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>springDispatcherServletservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>WEB-INF/applicationContext.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>springDispatcherServletservlet-name> <url-pattern>/url-pattern> servlet-mapping>web-app>
配置applicationContext.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:context="http://www.springframework.org/schema/context" 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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.gwsssi.springmvc">context:component-scan>