1. 什么是springMVC?

Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。

2. SpringMVC处理请求的流程

2.1 首先用户发送请求-->DispatherServlet

2.2 DispatcherServlet-->HandlerMapping

2.3 DispatcherServlet-->HandlerAdapter

2.4 HandlerAdapter-->处理器功能处理方法的调用

2.5 ModelAndView的逻辑视图名-->ViewRecolver

2.6 View-->渲染

2.7 返回控制权给DispatcherServlet,由DispatcherServlet返回呼应给用户,流程结束

3. SpringMVC核心开发步骤

3.1 DispatcherServlet在web.xml中的部署描述,从而拦截请求到springMVC

3.2 HandlerMapping的配置,从而将请求映射到处理器

3.3 HandlerAdapter的配置,从而支持多种类型的处理器

3.4 处理器(页面控制器)的配置,从而刊行功能处理

3.5 ViewResolver的配置,从而将逻辑视图名解析为具体的视图技术

4. SpringMVC的组件

4.1 前端控制器(DispatcherServlet)

4.2 请求到处理器映射(HandlerMapping)

4.3 处理器适配器(HandlerAdapter)

4.4 视图解析器(ViewResolver)

4.5 处理器或页面控制器(Controller)

4.6 验证器(Validator)

4.6 命令对象(Command 请求参数绑定到的对象就叫命令对象)

4.7 表单对象(Form Object提供给表单展示和提交到的对象就叫表单对象)

5. 如何在项目中添加springmvc

5.1 添加相关依赖

把上次的spring-context干掉加上这个

<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.</version>
      </dependency>

5.2 在WEB-INF下添加springmvc-servlet.xml(spring-mvc.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元素扫描指定包下的控制器-->
<!--) 扫描com.javaxl.zf及子子孙孙包下的控制器(扫描范围过大,耗时)-->
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.jt"/> <!--) 此标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<!--两个bean,这两个bean是spring MVC为@Controllers分发请求所必须的。并提供了数据绑定支持,-->
<!--@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)-->
<mvc:annotation-driven></mvc:annotation-driven> <!--) 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> <!--) 单独处理图片、样式、js等资源 -->
<!--<mvc:resources location="/css/" mapping="/css/**"/>-->
<mvc:resources location="/static/" mapping="/static/**"/>
<!--<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>
<!-- &lt;!&ndash; 读取Spring上下文的监听器 &ndash;&gt;-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- &lt;!&ndash; Spring和web项目集成end &ndash;&gt;--> <!-- &lt;!&ndash; 防止Spring内存溢出监听器 &ndash;&gt;-->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- &lt;!&ndash; 中文乱码处理 &ndash;&gt;-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- &lt;!&ndash;web.xml .0的新特性,是否支持异步&ndash;&gt;-->
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!--&lt;!&ndash; Spring MVC servlet &ndash;&gt;-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--&lt;!&ndash;此参数可以不配置,默认值为:/WEB-INF/springmvc-servlet.xml&ndash;&gt;-->
<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>
<!-- &lt;!&ndash;web.xml 3.0的新特性,是否支持异步&ndash;&gt;-->
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

2、Springmvc之helloword实现

演示代码

@ResponseBody
@RequestMapping("/hello1")
public String hello1() {
System.out.println("hello springmvs 你大爷");
return "hello springmvs 你大爷";
} @RequestMapping("/hello2")
public String hello2() {
System.out.println("hello2 springmvs 你大爷");
return "hello";
} @ResponseBody
@RequestMapping("/hello3")
public Map hello3() {
System.out.println("hello springmvs 你大爷");
Map map=new HashMap();
map.put("total",);
map.put("rows","一串的数据");
return map;
}

效果

3、Springmvc常用注解及返回值处理

增删改查案例演示
需要用到自定义标签和分页

package com.jt.controller;

import com.jt.model.Book;
import com.jt.service.BookService;
import com.jt.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest;
import java.util.List; /**
* @author jt
* @site www.xiaomage.com
* @company xxx公司
* @create  2019-10-25 16:47
*/
@Controller
@RequestMapping("/book")
public class BookController { @Autowired
private BookService bookService; @RequestMapping("/list")
public String list(Book book, HttpServletRequest req){
System.out.println("list");
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
List<Book> books = this.bookService.listPager1(book,pageBean);
req.setAttribute("bookList",books);
req.setAttribute("pageBean",pageBean);
return "bookList";
}
@RequestMapping("/preSave")
public String preSave(Book book,HttpServletRequest req){
if(book !=null && book.getBid() !=null && book.getBid() !=){
Book b = this.bookService.selectByPrimaryKey(book.getBid());
req.setAttribute("book2" ,b);
}
return "bookEdit";
}
@RequestMapping("/add")
public String add(Book book,HttpServletRequest req){
this.bookService.insertSelective(book);
return "redirect:/book/list";
}
@RequestMapping("/edit")
public String edit(Book book,HttpServletRequest req){
this.bookService.updateByPrimaryKeySelective(book);
return "redirect:/book/list";
} @RequestMapping("/del")
public String del(@RequestParam Integer bid, HttpServletRequest req){
this.bookService.deleteByPrimaryKey(bid);
return "redirect:/book/list";
} }

4、Springmvc静态资源处理

springmvc不可以直接访问到静态资源文件
我们需要加一个专门处理处理静态资源文件的功能
例如: 访问static的图片

<?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元素扫描指定包下的控制器-->
<!--) 扫描com.javaxl.zf及子子孙孙包下的控制器(扫描范围过大,耗时)-->
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.jt"/> <!--) 此标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<!--两个bean,这两个bean是spring MVC为@Controllers分发请求所必须的。并提供了数据绑定支持,-->
<!--@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)-->
<mvc:annotation-driven></mvc:annotation-driven> <!--) 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="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean> <!--) 单独处理图片、样式、js等资源 -->
<!--<mvc:resources location="/css/" mapping="/css/**"/>-->
<!--<mvc:resources location="/images/" mapping="/images/**"/>-->
<!--<mvc:resources location="/js/" mapping="/js/**"/>-->
<mvc:resources location="/static/" mapping="/static/**"/> </beans>

修改这2处地方就可以了

---恢复内容结束---

springmvc的入门的更多相关文章

  1. SpringMVC从入门到精通之第四章

    第一个知识点:@Controller注解,用于标识这个类是一个后端控制器(类似struts中的action),主要作用就是接受页面的参数,转发页面.中间的业务逻辑是调用业务类处理的这个就是MVC设计模 ...

  2. SpringMVC基础入门

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  3. SpringMVC简单入门

    SpringMVC简单入门 与大家分享一下最近对SpringMVC的学习,希望本文章能对大家有所帮助. 首先什么是SpringMVC? Spring 为展现层提供的基于MVC设计理念的优秀的Web框架 ...

  4. SpringMVC基础入门,创建一个HelloWorld程序

    ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...

  5. SpringMVC 快速入门

    SpringMVC 快速入门 SpringMVC 简介 SpringMVC是 Spring为展示层提供的基于Web MVC设计模式的请求驱动类型的轻量级Web框架,它的功能和Struts2一样.但比S ...

  6. SpringMVC之入门程序

    SpringMVC之入门程序——使用浏览器展示商品数据 springMVC执行流程(图片来源:https://www.jianshu.com/p/8a20c547e245) 1.创建pojo(商品实体 ...

  7. SpringMVC框架入门配置 IDEA下搭建Maven项目(zz)

    SpringMVC框架入门配置 IDEA下搭建Maven项目 这个不错哦 http://www.cnblogs.com/qixiaoyizhan/p/5819392.html

  8. 一起学SpringMVC之入门篇

    本文属于SpringMVC的入门篇,属于基础知识,仅供学习分享使用,如有不足之处,还请指正. 什么是SpringMVC ? SpringMVC是一个基于Spring的MVC框架,继承了Spring的优 ...

  9. springmvc快速入门(注解版本)

    1)springmvc快速入门(传统版) 步一:创建springmvc-day02这么一个web应用 步二:导入springioc,springweb和springmvc相关的jar包 ------- ...

  10. springmvc快速入门(XML版本)

    1)springmvc快速入门(传统版) 步一:创建springmvc-day01这么一个web应用 步二:导入springioc,springweb , springmvc相关的jar包 步三:在/ ...

随机推荐

  1. IT兄弟连 HTML5教程 CSS3属性特效 3D变换1

    3D变换较2D变换多了一下的转换属性,3D转换属性及描述如表1: 表1  3D转换属性 3D的转换方法如表2: 表2  3D转换方法     1  transform-style transform- ...

  2. Java描述设计模式(14):解释器模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.解释器模式 1.基础概念 解释器模式是对象的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端 ...

  3. 【UR #6】懒癌

    Problem Description 你绞尽脑汁也没有解开智商锁给的迷题,只见哐地一下门就开了:"您与锁的主人智商一致." 于是你们窃取了大量内部资料,最后端掉了 \(IIIS\ ...

  4. Eigen对齐(加速)方案

    Eigen库为了使用指令集(如SSE等)加速方案,通常采用字节对齐的方案.如果使用C++的标准库,如Vector,Map等,需要使用如下方案 std::map<int, Eigen::Vecto ...

  5. mysql存储4字节的表情包数据报异常_Emoji表情包_Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F

    本文章转载自:https://www.cnblogs.com/coprince/p/7485968.html 原文如下: 问题描述:从新浪微博抓取消息保存到MySQL数据中,对应数据库字段为varch ...

  6. CefSharp 修复Demo无法在其它路径下启动问题

    CefSharp编译后,在直接点击打开CefSharp.Wpf.Example.exe,启动没问题但是复制demo包到其它路径下,无法打开demo.原因:代码中含有相对路径下的资源文件引用. CefS ...

  7. How to: Supply Initial Data for the Entity Framework Data Model 如何:为EF数据模型提供初始数据

    After you have introduced a data model, you may need to have the application populate the database w ...

  8. ARTS改版啦,在改变中前行

    这次打卡,稍微进行了一次改版,在算法和英文文档上进行了拆分,具体的内容在前两天的文章里已经输出,所以在这篇上针对这两块做了一个汇总. 当然,技巧方面的还是在这里先输出,后续再考虑整改吧.循序渐进地上升 ...

  9. ES6-WeakSet数组结构

    WeakSet 也会去重 总结: 1.成员都是对象: 2.成员都是弱引用,可以被垃圾回收机制回收,可以用来保存 DOM 节点,不容易造成内存泄漏: 3.不能遍历,方法有 add.delete.has. ...

  10. ORA-16032和ORA-07286 LOG_ARCHIVE_DEST_1没生效

    主备切换在备库startup时出现归档路径没写到spfile里...注意:修改参数时最好带上scope=spfile或scope=both,以免重启出现异常.SQL> startup mount ...