SpringMVC01

目录

一、SpringMVC的简介

二、SpringMVC的Helloword实现

三、SpringMVC的工作原理

四、CRUD执常用注解及返回值


一、SpringMVC的简介

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

二、SpringMVC的Helloword实现

1、导入pom依赖

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
      </dependency>
<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>

2、SpringMVC处理请求的流程

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

2 DispatcherServlet-->HandlerMapping

3 DispatcherServlet-->HandlerAdapter

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

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

6 View-->渲染

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

3、SpringMVC核心开发步骤

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

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

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

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

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

3、SpringMVC的组件

1 前端控制器(DispatcherServlet)

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

3 处理器适配器(HandlerAdapter)

4 视图解析器(ViewResolver)

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

6 验证器(Validator)

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

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

HelloController :

package com.zxw.ssm.controller;
 
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
 
/**
 * 被controller标记的类 会交给 spring 进行管理
 * @Controller
 * @Service
 * @Repository
 * @Component
 * 以上四个都代表 当前类 交给Spring容器进行管理
 */
 
@Controller
public class HelloController {
 
//    自定义mvc:浏览器发送请求 http://localhost:8080/hello.action?methodName=hello
//    springmvc:浏览器发送请求 http://localhost:8080/helloReq
//    此处是下面的简写版
    @RequestMapping("/helloReq")
    public String hello(){
        System.out.println("hello springmvc...");
//        /hello.jsp
        return "hello";
    }
 
    @RequestMapping("/hello2")
    public ModelAndView hello2(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>Title</title>
</head>
<body>
SpringMVC 你好
${msg}
</body>
</html>
 

最终效果:

三、SpringMVC的工作原理

1、浏览器发送请求到中央控制器Dispatcherservlet
2、中央控制器DispatcherServlet通过处理器映射器HandlerMapping找到处理器适配器HandlerAdapter
3、执行HandlerAdapter,返回值ModelAndView回传给DispatcherServlet
4、视图解析器viewResolver会对 返回值ModelAndView进行渲染,得到view视图对象
5、最后DispatcherServlet会将view视图对象返回给用户

四、CRUD执常用注解及返回值

BookController :

package com.zxw.ssm.controller;
 
import com.zxw.ssm.biz.BookBiz;
import com.zxw.ssm.model.Book;
import com.zxw.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @RequestMapping 加在 类上面,称窄化路径,其实相当于包的概念
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookBiz bookBiz;
 
//    http://localhost:8080/book/list
    @RequestMapping
//    @GetMapping=@RequestMapping(value = "/list",method = RequestMethod.GET)
//    @PostMapping
//    @DeleteMapping
//    @PutMapping
    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<Map> maps = this.bookBiz.listPager(map, pageBean);
        req.setAttribute("lst",maps);
        return "hello";
    }
 
    @RequestMapping("/add")
    public String add(Book book){
        this.bookBiz.insertSelective(book);
        return "redirect:/book/list";
    }
 
    @RequestMapping("/edit")
    public String edit(Book book){
        this.bookBiz.insertSelective(book);
        return "redirect:/book/list";
    }
 
    @RequestMapping("/del/{bid}")
    public String del(@PathVariable("bid") Integer bid){
        this.bookBiz.deleteByPrimaryKey(bid);
        return "redirect:/book/list";
    }
}
 

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/8/16
  Time: 21:52
  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>
    <a href="${pageContext.request.contextPath}/book/list?bname=圣墟">查询所有</a>
    <a href="${pageContext.request.contextPath}/book/add?bid=2&bname=sb&price=9.9">增加</a>
    <a href="${pageContext.request.contextPath}/book/edit?bid=2&bname=suibian&price=9.8">修改</a>
    <a href="${pageContext.request.contextPath}/book/del/2">删除</a>
</body>
</html>
 

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" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
SpringMVC 你好
${msg}
<hr>
${lst}
<c:forEach items="${lst}" var="l">
    ${l.bid}:${l.bname}<hr>
</c:forEach>
</body>
</html>
 

效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值