Spring MVC框架简单使用
- 在pom.xml中引入mvc模块
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.28.RELEASE</version>
</dependency>
- 配置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">
<!--配置mvc中设置好的servlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<url-pattern>/</url-pattern>
中传入的/
表示让spring的这个servlet处理所有请求,但不包括jsp
/*
表示处理所有请求
.do
表示处理以.do结尾的请求
3. 类创建
因为需要处理所有请求,所以将创建时间提前到tomcat创建时,并且类创建时创建spring容器
<?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">
<!--配置mvc中设置好的servlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--在类启动时创建spring 容器-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<!--随tomcat的启动创建类-->
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--处理除jsp外的所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 配置spring文件
这里需要注意:
正确模块导入:
xmlns:mvc="http://www.springframework.org/schema/mvc"
导入的mvc模块默认可能会导入:xmlns:mvc="http://www.springframework.org/schema/cache"
,启动项目会报错:
Cannot resolve reference to bean ‘cacheManager’ while setting bean property 'cacheManager’异常
<?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/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置mvc最小驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--将要使用的类扫描到配置文件中-->
<context:component-scan base-package="com.example.servlet"></context:component-scan>
</beans>
- 开发一个处理响应请求的类和方法
使用@Controller注册到spring容器中
在方法上使用@RequestMapping("/要匹配的网址")
//注册到spring容器中
@Controller
public class DemoA {
//使用注解表示该方法映射到什么路径的请求
@RequestMapping("/dem")
public String method(){
return "demo1.jsp";
}
}
- 创建转发的demo1.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>mvc开发的web</h1>
</body>
</html>
- 显示结果:
- 逻辑视图到物理页面配置
类方法中return返回的是逻辑视图,不是真实的物理页面,
如果要简化逻辑视图为demo1,需要在spring配置文件中配置
核心类InternalResourceViewResolver
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--增加前缀-->
<property name="prefix" value="/WEB-INF/"></property>
<!--增加后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
类简化:
//注册到spring容器中
@Controller
public class DemoA {
//使用注解表示该方法映射到什么路径的请求
@RequestMapping("/dem")
public String method(){
return "demo1";
}
}