• SpringMVC入门


    一、SpringMVC简介

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

    二、SpringMVC的HelloWord实现

    pom.xml配置

    1. <dependency>
    2. <groupId>org.springframeworkgroupId>
    3. <artifactId>spring-webmvcartifactId>
    4. <version>${spring.version}version>
    5. dependency>
    6. <dependency>
    7. <groupId>jstlgroupId>
    8. <artifactId>jstlartifactId>
    9. <version>1.2version>
    10. dependency>
    11. <dependency>
    12. <groupId>taglibsgroupId>
    13. <artifactId>standardartifactId>
    14. <version>1.1.2version>
    15. 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的配置,从而将逻辑视图名解析为具体的视图技术

     

     

    在WEB-INF下添加springmvc-servlet.xml(spring-mvc.xml)

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    7. 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">
    8. <aop:aspectj-autoproxy/>
    9. <context:component-scan base-package="com.zwc.ssm"/>
    10. <mvc:annotation-driven>mvc:annotation-driven>
    11. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    12. <property name="viewClass"
    13. value="org.springframework.web.servlet.view.JstlView">property>
    14. <property name="prefix" value="/"/>
    15. <property name="suffix" value=".jsp"/>
    16. bean>
    17. <mvc:resources location="/static/" mapping="/static/**"/>
    18. beans>

    applicationContext.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. 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">
    7. <import resource="applicationContext-mybatis.xml">import>
    8. beans>

    HelloController

    1. package com.zwc.ssm.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.stereotype.Service;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.servlet.ModelAndView;
    6. import javax.servlet.http.HttpServletRequest;
    7. /**
    8. * @authorzwc
    9. * @site www.javazwc.com
    10. * @company xxx公司
    11. * @create  2022-08-16 23:08
    12. */
    13. /*
    14. Controller:被标记的类会交给 Spring进行管理
    15. Service
    16. Repository
    17. Component
    18. 以上四个都代表当前类交给Springr容器进行管理
    19. */
    20. @Controller
    21. @Service
    22. public class HelloController {
    23. // 浏览器发送请求
    24. @RequestMapping("/helloReq")
    25. public String hello(){
    26. System.out.println("hello springmvc....");
    27. return "hello";
    28. }
    29. @RequestMapping("/hello2")
    30. public ModelAndView hello(HttpServletRequest req){
    31. ModelAndView mv = new ModelAndView();
    32. mv.setViewName("hello");
    33. mv.addObject("msg","success...");
    34. return mv;
    35. }
    36. }

     Hello.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Administrator
    4. Date: 2022/8/16
    5. Time: 17:26
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. head>
    13. <body>
    14. SpringMVC 你好
    15. ${msg}
    16. body>
    17. html>

     

    3、增删改查之常用注解及返回值 

    BookController

    1. package com.zwc.ssm.controller;
    2. import com.zwc.ssm.biz.BookBiz;
    3. import com.zwc.ssm.model.Book;
    4. import com.zwc.ssm.util.PageBean;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Controller;
    7. import org.springframework.web.bind.annotation.PathVariable;
    8. import org.springframework.web.bind.annotation.RequestMapping;
    9. import org.springframework.web.bind.annotation.RequestMethod;
    10. import javax.servlet.http.HttpServletRequest;
    11. import java.util.HashMap;
    12. import java.util.List;
    13. import java.util.Map;
    14. /**
    15. * @authorzwc
    16. * @site www.javazwc.com
    17. * @company xxx公司
    18. * @create  2022-08-17 0:21
    19. *
    20. * @RequestMapping加在类上面,称窄化路径,其实就相当于包的概念
    21. *
    22. **/
    23. @Controller
    24. @RequestMapping("/book")
    25. public class BookController {
    26. @Autowired
    27. private BookBiz biz;
    28. @RequestMapping(value = "/list",method = RequestMethod.GET)
    29. public String hello(HttpServletRequest req){
    30. System.out.println("hello springmvc....");
    31. PageBean pageBean = new PageBean();
    32. pageBean.setRequest(req);
    33. Map map = new HashMap();
    34. String bname = req.getParameter("bname");
    35. map.put("bname",bname);
    36. List maps = this.biz.listPage(map, pageBean);
    37. req.setAttribute("list",maps);
    38. return "hello";
    39. }
    40. @RequestMapping("/add")
    41. public String add(Book book){
    42. this.biz.insertSelective(book);
    43. return "redirect:/book/list";
    44. }
    45. @RequestMapping("/edit")
    46. public String edit(Book book){
    47. this.biz.updateByPrimaryKeySelective(book);
    48. return "redirect:/book/list";
    49. }
    50. @RequestMapping("/del/{bid}")
    51. public String del(@PathVariable("bid") Integer bid){
    52. this.biz.deleteByPrimaryKey(bid);
    53. return "redirect:/book/list";
    54. }
    55. }

    index.jsp 

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Administrator
    4. Date: 2022/8/17
    5. Time: 11:22
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. head>
    13. <body>
    14. <a href="${pageContext.request.contextPath}/book/list?bname=圣墟">查询所有a>
    15. <a href="${pageContext.request.contextPath}/book/add?bid=2&bname=aa&price=9.9">增加a>
    16. <a href="${pageContext.request.contextPath}/book/edit?bid=2&bname=bb&price=9">修改a>
    17. <a href="${pageContext.request.contextPath}/book/del/2">删除a>
    18. <img src="${pageContext.request.contextPath}/static/images/2.jpg">
    19. body>
    20. html>

    hello.jsp
     

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Administrator
    4. Date: 2022/8/16
    5. Time: 23:26
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    10. <html>
    11. <head>
    12. <title>Titletitle>
    13. head>
    14. <body>
    15. Springmvc你好
    16. <hr>
    17. ${list}
    18. <c:forEach items="${list}" var="c">
    19. ${c.bid}:${c.bname}:${c.price}<br>
    20. c:forEach>
    21. body>
    22. html>

    运行结果: 

    查询所有:

    数据库数据:

     

     

    测试增加:

    测试修改:

     

     测试删除:

     

  • 相关阅读:
    华为Atlas 300I 推理卡显卡安装
    技术分享| 小程序实现音视频通话
    [IOS自动化]Xcode build时报错: Cannot link directly with dylib/framework
    FreeSWITCH添加h264编码及pcap视频提取
    Python 进阶 - 日常工作中使用过的简单Trick
    Opencv实现颜色检测
    基于Amazon EC2和Amazon Systems Manager Session Manager的堡垒机设计和自动化实现
    第13章 类继承
    新手必看!!超详细!STM32-基本定时器
    操作系统安装在哪里?
  • 原文地址:https://blog.csdn.net/qq_65345936/article/details/126352588