Spring Web MVC是什么?
- Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架。
- 使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型 。
- 框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的。
Spring Web MVC能帮我们做什么?
- 让我们能非常简单的设计出干净的Web层和薄薄的Web层
- 进行更简洁的Web层的开发
- 天生与Spring框架集成(如IoC容器、AOP等)
- 提供强大的约定大于配置的契约式编程支持
- 非常灵活的数据验证、格式化和数据绑定机制
- 支持Restful风格
Spring Web MVC处理请求的流程
Spring Web MVC架构图
基本开发步骤
第一步 创建web工程,添加Spring支持
pom.xml
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!--springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>springmvc1008</finalName>
<plugins>
<!--jettt服务器插件-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.14.v20161028</version>
</plugin>
<!--tomcat服务器插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8080</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 添加了springmvc、junit单元测试、servlet依赖支持
- 添加了jetty服务器插件和tomcat服务器插件(因为配置本地的tomcat服务器较为繁琐,这里不详细赘述,如果想要了解,可以参考我的以往博客),便于运行测试
第二步 前端控制器的配置
- 在web.xml中添加如下配置
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
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">
<display-name>Archetype Created Web Application</display-name>
<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.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 自此请求已交给Spring Web MVC框架处理,因此我们需要配置Spring的配置文件,默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件。
- 注意,如果不配置<init-param>的话,那么springmvc会去WEB-INF/路径下去查找”你的servletname”-servlet.xml的配置文件,所以一般都会配置局部初始化参数,告诉spring配置文件的路径和名称.
第三步 在Spring配置文件中配置HandlerMapping、HandlerAdapter
- 具体配置在resources/spring.xml文件中:
spring.xml
- BeanNameUrlHandlerMapping:表示将请求的URL和Bean名字映射,如URL为 “上下文/hello”,则Spring配置文件必须有一个名字为“/hello”的Bean,上下文默认忽略。
- SimpleControllerHandlerAdapter:表示所有实现了org.springframework.web.servlet.mvc.Controller接口的Bean可以作为Spring Web MVC中的处理器。如果需要其他类型的处理器可以通过实现HadlerAdapter来解决。
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean name="/hello" class="com.csx.HelloController" />
</beans>
- 配置处理器映射器、处理器适配器
<bean name="/hello" class="com.csx.HelloController" />
- 这里只能使用name给bean起别名,这里是用来映射对应的controller类(暂时没有写,下面接着就会写)
第四步 在Spring配置文件中配置ViewResolver
- 具体配置在resources/spring.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean name="/hello" class="com.csx.HelloController" />
</beans>
- InternalResourceViewResolver:用于支持Servlet、JSP视图解析。
- prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp”
- 注意,这里的视图解析器的前缀(prefix)的value值是从webapp目录(即根目录)下开始查找的;后缀(suffix)的value指的是文件的后缀名。
第五步 开发页面控制器
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv =new ModelAndView();
//添加模型数据 可以是任意的POJO对象
mv.addObject("msg","你好,我是马喽");
//设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
mv.setViewName("hello");
return mv;
}
}
通过返回ModelAndView对象,进行页面跳转和数据绑定。
第六步 配置控制器我们需要将其添加到Spring配置文件让其接受Spring IoC容器管理
<bean name="/hello" class="com.csx.HelloController" />
- 上面已经配置过了,这里不必重复书写
- 在spring配置文件中添加控制器的配置
第七步,开发视图页面
- 创建 /WEB-INF/hello.jsp视图页面
hello.jsp
<%--
Created by IntelliJ IDEA.
User: 21038
Date: 2024/10/8
Time: 15:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
使用el表达式取值。
结果显示
需要配置jetty服务器或tomcat服务器,启动web项目,如何在maven项目中启动web项目,我在以往的博客中有介绍
总结
项目结构如下