❤️最后的大爆发❤️五万字总结SpringMVC教程——三部曲封神之作(建议收藏)
Posted Maynor的大数据奋斗之路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了❤️最后的大爆发❤️五万字总结SpringMVC教程——三部曲封神之作(建议收藏)相关的知识,希望对你有一定的参考价值。
文章目录
前言
上回分别为各位分享了《六万字最全总结Java数据库编程MyBatis》《爆肝万字!一文最全总结之Spring从入门到入土》。
没有学过的同学,建议先学MyBatis,后学Spring,这次在此基础上我们来学习SpringMVC,此篇学完再无后端!
❤️爆肝六万字最全总结Java数据库编程MyBatis(建议收藏)
❤️爆肝万字!一文最全总结之Spring从入门到入土❤️(建议收藏)
相关资料:
1. Spring MVC 概述
2.1 Spring MVC是什么
Spring web mvc是表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来:
1.0 Spring MVC处理流程
l 1)核心控制器:处理特定请求。(例如:以*.action为后缀)
名称:DispatcherServlet
配置:是一个servlet,需要在web.xml中配置。
XML:需要确定核心XML配置文件的位置
l 2)核心XML配置文件:确定Controller位置、确定JSP页面位置
名称:springmvc.xml
l 3)控制器:开发人员编写的主要内容
名称:*Controller
l 流程示意图
2. 入门案例:查询详情 ( xml)
3.1 需求说明
在index.html中点击 Hello SpringMVC 超链接,在 show01 .jsp 页面显示信息。
2.1 思路分析
整体思路:
跟HelloServlet入门案例几乎相同 .
服务器设置一个Controller,相当于之前的Servlet,可以接收请求,把请求在转发给jsp页面.
这里的区别就是需要设置web.xml和springmvc.xml两个配置文件,前者是用来配置核心控制器,后者是用来扫描我们自己书写的Controller的.
书写步骤:
1.拷贝pom文件,SpringMVC.xml文件和web.xml配置文件核心代码.
2.创建HelloController和show01.jsp
项目结构如下:
2.2 实现步骤
2.2.1 拷贝配置文件相关代码
获取pom文件:
参见当前文档末尾的附录 <SpringMVC01_pom>
获取springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 扫描注解包 -->
<context:component-scan base-package="com.Maynor.controller" />
</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_4_0.xsd"
version="4.0">
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
2.2.2 创建java类和jsp文件
创建HelloController.java
package com.Maynor.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello.action")
public String show(Model model){
model.addAttribute("data", "嘿嘿嘿");
return "/WEB-INF/show01.jsp";
}
}
Show01.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
你好啊 老弟儿 ${data}
</body>
</html>
Index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="${pageContext.request.contextPath}/hello.action">入门案例</a>
</body>
</html>
测试结果:
index.jsp页面
点击入门案例结果:
3. 入门查询:查询所有(无xml)
3.2 需求说明
该需求跟上一个入门案例需求完全相同.
在index.html中点击 Hello SpringMVC 超链接,在 show01 .jsp 页面显示信息。
区别在于实现的时候:
使用 WebInitializer类代替web.xml
使用MVCConfig代替springmvc.xml
3.1 思路分析
书写步骤:
1.拷贝pom文件,书写MVCConfig和WebInitializer类.
2.创建HelloController和show01.jsp
项目结构如下:
3.2 步骤实现
3.2.1 拷贝pom文件,创建MVCConfig和WebInitializer类
Pom文件,沿用上一个案例.即附录 <SpringMVC01_pom>
MVCConfig类代码如下:
package com.Maynor.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.Maynor.controller")
public class MVCConfig {
}
传统Web项目中,程序的入口是web.xml文件。
在spring4中提供WebApplicationInitializer接口,表示服务器启动初始化。也就是说接口实现类中编写的内容,就是web.xml文件中配置的内容。
类或方法 | 描述 |
---|---|
AnnotationConfigWebApplicationContext | WEB环境下spring工厂 |
servletContext.addFilter(name , Class) | 添加过滤器,等效 |
servletContext.addServlet(name , Class) | 添加servlet,等效 |
过滤器相关方法 ServletRegistration.Dynamic | 描述 |
---|---|
addMapping(urlPatterns) | 追加过滤路径 |
setLoadOnStartup(int) | 设置启动顺序 |
WebInitializer类代码如下:
package com.Maynor.config;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//1.初始化spring容器
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(MVCConfig.class);
applicationContext.setServletContext(servletContext);
//2.设置核心控制器
ServletRegistration.Dynamic springMVCServlet = servletContext.addServlet("springmvc", new DispatcherServlet(applicationContext));
springMVCServlet.addMapping("*.action");
springMVCServlet.setLoadOnStartup(2);
}
}
3.2.2 创建HelloController和show01.jsp
HelloController代码:
package com.Maynor.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(path = "/hello.action")
public String show(Model model){
model.addAttribute("data", "呵呵呵");
return "/WEB-INF/show01.jsp";
}
}
Show01.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
来了 老弟儿 ${data}
</body>
</html>
测试结果:
练习:
查询所有. 希望得到如下结果:
参考代码:
l 编写JavaBean用于封装数据
public class Item {
private String name; //名称
private Float price; //价格
private String detail; //详情
l 编写业务控制器Controller
@Controller
@RequestMapping("/user")
public class HelloController {
@RequestMapping("showInfo")
public String showInfo(Model model){
System.out.println("展示页面");
//1 模拟数据
List<Item> list = new ArrayList<Item>();
list.add(new Item("联想笔记本", 6999f, "ThinkPad T430 联想笔记本电脑!"));
list.add(new Item("苹果手机", 8800f, "iphone X 苹果手机!"));
//设置数据
model.addAttribute("list", list);
return "/showInfo.jsp";
}
}
l 展示数据
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: 86153
Date: 2019/6/14
Time: 0:50
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>
<table border="1">
<tr>
<td>名称</td>
<td>价格</td>
<td>描述</td>
</tr>
<c:forEach items="${list}" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td>${item.detail }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
配置类
*MVCConfig
@Configuration
@ComponentScan("com.Maynor.controller")
public class MVCConfiguration {
}
WebInitalizer
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//1 初始化spring容器
AnnotationConfigWebApplicationContext appliation = new AnnotationConfigWebApplicationContext();
appliation.register(MVCConfiguration.class);
appliation.setServletContext(servletContext);
//3 前端控制器
ServletRegistration.Dynamic springMvcServlet = servletContext.addServlet("springmvc", new DispatcherServlet(appliation));
springMvcServlet.addMapping("*.action");
springMvcServlet.setLoadOnStartup(2);
}
}
测试:
l 步骤2:启动,并访问
http://localhost:8080/s02/user/showInfo.action
4. SSM整合:改造UMS
需求:
改造ums 实现 查询所有用户.
示意图如下:
思路:
-
环境搭建:拷贝pom,资源文件和配置文件,初始化数据库和表,创建javaBean
-
编写功能:dao,Service,Controller
-
编写配置类:SpringConfig,MyBatisConfig,SpringMvcConfig,Web启动类.
-
测试
项目最终结构如下:
4.1 搭建环境
4.0.1 创建项目、 拷贝pom文件
<org.springframework.version>4.2.4.RELEASE</org.springframework.version>
<dependencies>
<!-- spring start -->
<!--spring core start-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring core end-->
<!--spring aop start-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spirng aop end-->
<!--spring aspects start-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring aspects end-->
<!--spring instrumentation start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring instrumentation end-->
<!--spring messaging start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring messaging end-->
<!--spring data access start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring data access end-->
<!--spring web start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring web end -->
<!--spring test start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--spring test end -->
<!-- spring end -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies以上是关于❤️最后的大爆发❤️五万字总结SpringMVC教程——三部曲封神之作(建议收藏)的主要内容,如果未能解决你的问题,请参考以下文章
❤️五万字C语言入门图文教材,写到最后我电脑卡爆了❤️(建议收藏)
❤️五万字❤️离职后一天4面,总结了204道高频Java面试题,已拿阿里offer(建议收藏)