Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架
pom.xml配置
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring.version}version>
- dependency>
-
- <dependency>
- <groupId>jstlgroupId>
- <artifactId>jstlartifactId>
- <version>1.2version>
- dependency>
-
- <dependency>
- <groupId>taglibsgroupId>
- <artifactId>standardartifactId>
- <version>1.1.2version>
- dependency>
SpringMVC处理请求的流程
1 首先用户发送请求–>DispatherServlet
2 DispatcherServlet–>HandlerMapping
3 DispatcherServlet–>HandlerAdapter
4 HandlerAdapter–>处理器功能处理方法的调用
5 ModelAndView的逻辑视图名–>ViewRecolver
6 View–>渲染
7 返回控制权给DispatcherServlet,由DispatcherServlet返回呼应给用户,流程结束
SpringMVC核心开发步骤
1 DispatcherServlet在web.xml中的部署描述,从而拦截请求到springMVC
2 HandlerMapping的配置,从而将请求映射到处理器
3 HandlerAdapter的配置,从而支持多种类型的处理器
4 处理器(页面控制器)的配置,从而刊行功能处理
5 ViewResolver的配置,从而将逻辑视图名解析为具体的视图技术
- "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">
-
-
- <aop:aspectj-autoproxy/>
- <context:component-scan base-package="com.zwc.ssm"/>
-
-
-
-
- <mvc:annotation-driven>mvc:annotation-driven>
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView">property>
- <property name="prefix" value="/"/>
- <property name="suffix" value=".jsp"/>
- bean>
-
-
-
- <mvc:resources location="/static/" mapping="/static/**"/>
-
-
-
- beans>
- "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:tx="http://www.springframework.org/schema/tx"
- 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
-
- <import resource="applicationContext-mybatis.xml">import>
- beans>
- package com.zwc.ssm.controller;
-
-
- import org.springframework.stereotype.Controller;
- import org.springframework.stereotype.Service;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
-
- import javax.servlet.http.HttpServletRequest;
-
- /**
- * @authorzwc
- * @site www.javazwc.com
- * @company xxx公司
- * @create 2022-08-16 23:08
- */
- /*
- Controller:被标记的类会交给 Spring进行管理
- Service
- Repository
- Component
- 以上四个都代表当前类交给Springr容器进行管理
- */
-
- @Controller
- @Service
- public class HelloController {
-
- // 浏览器发送请求
- @RequestMapping("/helloReq")
- public String hello(){
- System.out.println("hello springmvc....");
- return "hello";
- }
-
- @RequestMapping("/hello2")
- public ModelAndView hello(HttpServletRequest req){
- ModelAndView mv = new ModelAndView();
- mv.setViewName("hello");
- mv.addObject("msg","success...");
- return mv;
- }
-
- }
Hello.jsp
- <%--
- Created by IntelliJ IDEA.
- User: Administrator
- Date: 2022/8/16
- Time: 17:26
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- SpringMVC 你好
- ${msg}
- body>
- html>
-

- package com.zwc.ssm.controller;
-
- import com.zwc.ssm.biz.BookBiz;
- import com.zwc.ssm.model.Book;
- import com.zwc.ssm.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.RequestMethod;
-
- import javax.servlet.http.HttpServletRequest;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * @authorzwc
- * @site www.javazwc.com
- * @company xxx公司
- * @create 2022-08-17 0:21
- *
- * @RequestMapping加在类上面,称窄化路径,其实就相当于包的概念
- *
- **/
- @Controller
- @RequestMapping("/book")
- public class BookController {
-
- @Autowired
- private BookBiz biz;
- @RequestMapping(value = "/list",method = RequestMethod.GET)
- public String hello(HttpServletRequest req){
- System.out.println("hello springmvc....");
- PageBean pageBean = new PageBean();
- pageBean.setRequest(req);
- Map map = new HashMap();
- String bname = req.getParameter("bname");
- map.put("bname",bname);
- List
- req.setAttribute("list",maps);
- return "hello";
- }
-
- @RequestMapping("/add")
- public String add(Book book){
- this.biz.insertSelective(book);
- return "redirect:/book/list";
- }
-
- @RequestMapping("/edit")
- public String edit(Book book){
- this.biz.updateByPrimaryKeySelective(book);
- return "redirect:/book/list";
- }
-
- @RequestMapping("/del/{bid}")
- public String del(@PathVariable("bid") Integer bid){
- this.biz.deleteByPrimaryKey(bid);
- return "redirect:/book/list";
- }
-
- }
index.jsp
- <%--
- Created by IntelliJ IDEA.
- User: Administrator
- Date: 2022/8/17
- Time: 11:22
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <a href="${pageContext.request.contextPath}/book/list?bname=圣墟">查询所有a>
- <a href="${pageContext.request.contextPath}/book/add?bid=2&bname=aa&price=9.9">增加a>
- <a href="${pageContext.request.contextPath}/book/edit?bid=2&bname=bb&price=9">修改a>
- <a href="${pageContext.request.contextPath}/book/del/2">删除a>
-
- <img src="${pageContext.request.contextPath}/static/images/2.jpg">
- body>
- html>
hello.jsp
- <%--
- Created by IntelliJ IDEA.
- User: Administrator
- Date: 2022/8/16
- Time: 23:26
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- Springmvc你好
- <hr>
- ${list}
- <c:forEach items="${list}" var="c">
- ${c.bid}:${c.bname}:${c.price}<br>
- c:forEach>
- body>
- html>
运行结果:
查询所有:

数据库数据:
测试增加:

测试修改:

测试删除:
