Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。
1 首先用户发送请求-->DispatherServlet
2 DispatcherServlet-->HandlerMapping
3 DispatcherServlet-->HandlerAdapter
4 HandlerAdapter-->处理器功能处理方法的调用
5 ModelAndView的逻辑视图名-->ViewRecolver
6 View-->渲染
7 返回控制权给DispatcherServlet,由DispatcherServlet返回呼应给用户,流程结束
1 DispatcherServlet在web.xml中的部署描述,从而拦截请求到springMVC
2 HandlerMapping的配置,从而将请求映射到处理器
3 HandlerAdapter的配置,从而支持多种类型的处理器
4 处理器(页面控制器)的配置,从而刊行功能处理
5 ViewResolver的配置,从而将逻辑视图名解析为具体的视图技术
1 前端控制器(DispatcherServlet)
2 请求到处理器映射(HandlerMapping)
3 处理器适配器(HandlerAdapter)
4 视图解析器(ViewResolver)
5 处理器或页面控制器(Controller)
6 验证器(Validator)
6 命令对象(Command 请求参数绑定到的对象就叫命令对象)
7 表单对象(Form Object提供给表单展示和提交到的对象就叫表单对象)
1 @Controller:用于标识处理器类
2 @RequestMapping:请求到处理器功能方法的映射规则,可定义到类和方法
常用参数:value、method
可将@RequestMapping标签定义到类名处窄化路径3 @RequestParam:请求参数到处理器功能处理方法的方法参数上的绑定
常用参数:value、required、defaultValue
注:required设置成false的参数类型必须是引用类型,因为基本数据类型是不能为null的4 @ModelAttribute:请求参数到命令对象的绑定
常用参数:value
4.1 可用@ModelAttribute标注方法参数,方法参数会被添加到Model对象中(作用:向视图层传数据)
4.2 可用@ModelAttribute标注一个非请求处理方法,此方法会在每次调用请求处理方法前被调用(作用:数据初始化)
4.3 可用@ModelAttribute标注方法,方法返回值会被添加到Model对象中(作用:向视图层传数据)
但此方法视图的逻辑图就会根据请求路径解析,例如:a/test42 --> /WEB-INF/a/test42.jsp
太麻烦几乎不用,不用直接保存到Model或ModelAndView中5 @SessionAttributes:指定ModelMap中的哪些属性需要转存到session
常用参数:value、types
注1:必须放到class类名处6 @InitBinder(本章暂不介绍):用于将请求参数转换到命令对象属性的对应类型
7 @RequestBody(重要~~~~~):用于目前比较流行的ajax开发的数据绑定(即提交数据的类型为json格式)
注1:使用@RequestBody注解的时候,前台的Content-Type必须要改为application/json,
如果没有更改,前台会报错415(Unsupported Media Type)。
后台日志就会报错Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported。
这些错误Eclipse下Tomcat是不会显示错误信息的,只有使用了日志才会显示
POM.xml
在项目中添加springmvc和JSTL依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- ********************** JSTL依赖 ********************** -->
<!-- 缺少下面的这两个jar包会报java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config-->
<!-- 原因:org.springframework.web.servlet.view.JstlView在视图解析时需要这二个jar包-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
在WEB-INF下添加springmvc-servlet.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 通过context:component-scan元素扫描指定包下的控制器--> <!--1) 扫描com.javaxl.zf及子子孙孙包下的控制器(扫描范围过大,耗时)--> <aop:aspectj-autoproxy/> <context:component-scan base-package="com.ht"/> <!--2) 此标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --> <!--两个bean,这两个bean是spring MVC为@Controllers分发请求所必须的。并提供了数据绑定支持,--> <!--@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)--> <mvc:annotation-driven></mvc:annotation-driven> <!--3) ViewResolver 视图分析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- viewClass需要在pom中引入两个包:standard.jar and jstl.jar --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <!-- <!–4) 单独处理图片、样式、js等资源 –>--> <!-- <!–<mvc:resources location="/css/" mapping="/css/**"/>–>--> <!-- <mvc:resources location="/static/img" mapping="/static/img/**"/>--> <!-- <!–<mvc:resources location="/js/" mapping="/js/**"/>–>--> </beans>
修改web.xml
<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_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 读取Spring上下文的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring和web项目集成end --> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 中文乱码处理 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--web.xml 3.0的新特性,是否支持异步--> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring MVC servlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--此参数可以不配置,默认值为:/WEB-INF/springmvc-servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!--web.xml 3.0的新特性,是否支持异步--> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
创建一个类:HelloController
package com.ht.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; /** * @author 黄大娘 * @company dogson有限公司 * @create 2019-09-28 11:27 * * 关于结果集处理的五种情况 * 1、转发到页面(webapp下) * 2、转发到action * 3、重定向到页面(webapp下) * 4、重定向到action * 5、转发到 WEB-INF下的页面 */ @Controller public class HelloController { /** * 转发到页面 * @return */ @RequestMapping("hello") public String hello(HttpServletRequest request){ request.setAttribute("msg","转发到页面的方式"); return "hello"; } /** * 重定向到action * @param request * @return */ @RequestMapping("hello2") public String redirectPage(HttpServletRequest request){ request.setAttribute("msg","重定向到action的方式"); return "redirect:hello"; } /** * 重定向到页面 * @param request * @return */ @RequestMapping("hello3") public String redirectPage2(HttpServletRequest request){ // request.setAttribute("msg","重定向到页面的方式"); Object msg = request.getAttribute("msg"); return "redirect:/hello.jsp"; } /** * 转发到action * @param request * @return */ @RequestMapping("hello4") public String forwardAction(HttpServletRequest request){ request.setAttribute("msg","转发到action的方式"); return "forward:hello3"; } /** * 转发到WEB-INF下的页面 * @param request * @return */ @RequestMapping("hello5") public String forwardAction2(HttpServletRequest request){ request.setAttribute("msg","转发到WEB-INF下的页面方式"); return "abc"; } }
前端显示 Hello.jsp
<%-- Created by IntelliJ IDEA. User: 2019091008 Date: 2019/9/28 Time: 22:27 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>黄大娘</title> </head> <body> hello~本是青灯不归客 <br> ${msg} </body> </html>
效果:
原文:https://www.cnblogs.com/huangting/p/11605559.html