一、SpringMVC组件
-
DispatcherServlet
(controller)
调用其他组件处理用户的请求,DispatcherServlet的存在降低了组件之间的耦合性 -
HandlerMapping 处理器映射器
负责根据用户请求找到Handler。
不同的映射器实现不同映射方式:配置文件方式、实现接口方式、注解方式 -
Handler 控制器
后端控制器 对用户请求进行处理 -
HandlerAdapter 处理器适配器
对处理器进行执行(适配器模式的应用) -
ViewResolver 视图解析器
二、项目创建
- 新建一个maven工程
- pom.xml中添加
添加依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
- 配置
4. web.xml
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml
Spring配置文件,扫描除了Controller以外的所有包
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.kk.springmvc02" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
<!--spring配置 进行包扫描 除去注解controller-->
spring-servlet.xml
Springmvc的配置文件:除了controller,其他包都不扫描
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.kk.springmvc02" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
<!--springmvc配置 不进行包扫描(false) 只扫描注解controller-->
三、实际使用
直接在Controller下写Controller类,使用注解;
类里可以定义视图 利用@RequestMapping("/hello") 进行映射(映射器)
我们是使用类来开发接口的,相当于一个类一个接口。 引入RequestMappingHandlerMapping做处理器映射器,一个类中可以开发出多个接口。
@Controller
public class MyController {
@RequestMapping("/hello")
//可以定义一些视图 有了mapping以后能找到这里,但是还没有执行
public ModelAndView hello()
{
ModelAndView mv = new ModelAndView("hello");
mv.addObject("name","kkzzjx");
return mv;
}
}
配置处理器映射器(映射)和处理器适配器(执行):(这两个组件很关键!!!)
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"
id="requestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
id="requestMappingHandlerAdapter"/>
注意:可以用 <mvc:annotation-driven/>
代替以上两个组件(怎么配?)
配置视图解析器:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
hello.jsp 显示
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>hello ${name}</h1>
</body>
</html>
配置Tomcat:
然后启动!