MVC框架:
新的软件架构模式
M:model模型,封装和映射数据(javabean)
V:视图层,界面显示工作(.jsp)
C:Controller控制器,控制整个网站的跳转逻辑
?SpringMVC框架对MVC的理解:
?首先前端来了一个请求后会有一个前端控制器将这些请求派发到可以处理的控制器,相应的控制器再去调用Model层去处理这些请求返回数据模型,返回的数据模型又会到前端控制器那里,前端控制器再去寻找对应的视图来将这些数据渲染到页面,渲染完成之后再将这些页面响应回去。
在IDEA里面搭建一个简单的SpingMvc的环境
(1)新建一个maven工程,pom文件如下。主要依赖有spring的几个核心依赖和springmvc的两个核心依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sf.jin</groupId>
<artifactId>SpringMvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringMvc Maven Webapp</name>
<dependencies>
<!--spring框架的三个基础包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!--springMVC的两个包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
(2)写web.xml配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--
SpringMVC有一个前端控制器能拦截所有请求并智能派发,
这个前端控制器是一个Servlet,应该在web.xml里面配置SpringMVC的这个Servlet来拦截所有请求
-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--contextConfigLocation:指定SpringMVC配置文件的位置-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/dispatcher-servlet.xml</param-value>
</init-param>
<!--servelet原来是在第一次访问创建对象时启动加载,
现在配置了load-on-startup,就会在服务器启动的时候创建servelet对象,值越小优先级越高,越先创建对象-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!--/和/*都是拦截所有请求,/*还会拦截*jsp请求,我们要保证*jsp能正常访问,所有使用/-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
(3)写SpringMVC的配置
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.xsd">
<!--扫描所有组件-->
<context:component-scan base-package="controller"/>
<!--配置一个视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
(4)写一个控制器
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/*@Controller注解:
告诉SpringMVC这个是一个控制器,可以处理请求
*/
@Controller
public class IndexController {
@RequestMapping("/a")
public String a() {
/*
视图解析器自动拼串,返回地址即为“/WEB-INF/views/a.jsp”
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
*/
return "a";
}
}
(5)启动tomcat服务,键入url地址
?maven项目的结构:
?
|