Spring MVC简介
一、概念
Spring+MVC,用来创建web应用,关键在于配置
二、web应用配置文件web.xml
1. web.xml文件用来初始化配置信息,比如welcome页面、servlet、filter、listener
2. Spring MVC是通过servlet拦截所有URL,来达到控制的目的
3. contextConfigLocation配置:结合了Spring和web
4. servlet配置:值为DispatcherServlet,包含了Spring MVC的请求逻辑,Spring使用DispatcherServlet来拦截web请求并进行逻辑处理
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="false"> <display-name>fs-webhook-provider</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
三、Spring配置文件applicationContext.xml
四、servlet配置文件spring-servlet.xml,前端控制器的配置
1. 存放web部分的配置
2. Spring MVC是基于Servlet的实现,服务器会加载对应于Servlet的配置文件
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>