一,Spring MVC介绍
Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的 Servlet进行Web开发。
二,Spring MVC请求处理流程
三,Spring MVC的优点
1、清晰的角色划分:前端控制器(DispatcherServlet)、请求到处理器映射(HandlerMapping)、处理器适配器(HandlerAdapter)、视图解析器(ViewResolver)、处理器或页面控制器(Controller)、验证器( Validator)、表单对象(Form Object 提供给表单展示和提交到的对象就叫表单对象)。
2、由于命令对象就是一个POJO,无需继承框架特定API,可以使用命令对象直接作为业务对象;
3、和Spring 其他框架无缝集成,是其它Web框架所不具备的;
4、可适配,通过HandlerAdapter可以支持任意的类作为处理器;
5、可定制性,HandlerMapping、ViewResolver等能够非常简单的定制;
6、功能强大的数据验证、格式化、绑定机制;
7、利用Spring提供的Mock对象能够非常简单的进行Web层单元测试;
8、本地化、主题的解析的支持,使我们更容易进行国际化和主题的切换。
9、RESTful风格的支持、简单的文件上传、约定优于配置的契约式编程支持、基于注解的零配置支持等等。
四、Spring MVC 小案例
1.导入依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency>
2.创建配置文件
配置版:
<?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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean id="/first" class="com.mvc.controller.FirstController"></bean> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
注解版:
<!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--包扫描仪--> <context:component-scan base-package="com.mvc"></context:component-scan> <!--开启mvc配置--> <mvc:annotation-driven></mvc:annotation-driven>
3.配置web
<!--配置前端控制器--> <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:applicationContext-mvc.xml</param-value> </init-param> <!--初始化时机--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
4.创建控制器
配置版:
public class FirstController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv=new ModelAndView(); mv.addObject("user","hehe"); mv.setViewName("index"); return mv; } }
注解版:
@Controller @RequestMapping("/second") public class SecondController { @RequestMapping("/method1") public ModelAndView method1(){ ModelAndView mv=new ModelAndView(); mv.addObject("user","hehe"); mv.setViewName("index"); return mv; } @RequestMapping("/method2") public ModelAndView method2(){ ModelAndView mv=new ModelAndView(); mv.addObject("user","haha"); mv.setViewName("index"); return mv; } }
5.测试
<%@page language="java" pageEncoding="UTF-8" contentType="text/html; UTF-8" isELIgnored="false" %>
<html>
<body>
<img src="/img/pro.jpg">
<h2>Hello World!</h2>
${user}
</body>
</html>
原文:https://www.cnblogs.com/cw172/p/11823286.html