• 怎样快速入门SpringMVC


    目录

    1.springmvc的简介和配置

    2.Springmvc之helloworld实现

     3.CRUD之常用注解及返回值


    1.springmvc的简介和配置

            什么是springmvc?

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

    导入pom依赖:

            org.springframework

            spring-webmvc

            ${spring.version}

         

    缺少下面的这两个jar包会报java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

    原因:org.springframework.web.servlet.view.JstlView在视图解析时需要这二个jar包

    1. jstl
    2. jstl
    3. 1.2
    4. taglibs
    5. standard
    6. 1.1.2

    2.Springmvc之helloworld实现

    第一个springMVC程序:HelloWorld

    配置文件配置:

    1.扫描base-package

    [ 定义注解扫描,开启注解扫描,开启动态扫描]

    2.配置视图解析器

    本地资源视图解析器:internalResourceViewResolver

    springmvc-servlet.xml


           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">
       
       
       
       

       
       
       
       

       
       
           
                              value="org.springframework.web.servlet.view.JstlView">
           
           
       

       
       
       
       



     

    做静态资源映射

    修改web.xml

             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">
      Archetype Created Web Application
     
        contextConfigLocation
        classpath:applicationContext.xml
     

     
     
        org.springframework.web.context.ContextLoaderListener
     

     
     
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
       
       
          contextConfigLocation
          /WEB-INF/springmvc-servlet.xml
       

        1
       
        true
     

     
        SpringMVC
        /
     


     




           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
           



     

    HelloController:

    1. package com.xbb.ssm.controller;
    2. import org.springframework.stereotype.Component;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.stereotype.Repository;
    5. import org.springframework.stereotype.Service;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.servlet.ModelAndView;
    8. import javax.servlet.http.HttpServletRequest;
    9. /**
    10. * 被controller标记的类 会交给 spring 进行管理
    11. * @Controller
    12. * @Service
    13. * @Repository
    14. * @Component
    15. * 以上四个都代表 当前类 交给Spring容器进行管理
    16. */
    17. @Controller
    18. public class HelloController {
    19. // 自定义mvc:浏览器发送请求 http://localhost:8080/hello.action?methodName=hello
    20. // springmvc:浏览器发送请求 http://localhost:8080/helloReq
    21. // 此处是下面的简写版
    22. @RequestMapping("/helloReq")
    23. public String hello(){
    24. System.out.println("hello springmvc...");
    25. // /hello.jsp
    26. return "hello";
    27. }
    28. @RequestMapping("/hello2")
    29. public ModelAndView hello2(HttpServletRequest req){
    30. ModelAndView mv=new ModelAndView();
    31. mv.setViewName("hello");
    32. mv.addObject("msg","success");
    33. return mv;
    34. }
    35. }

    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. Title
    10. SpringMVC 你好
    11. ${msg}

    运行效果:

     3.CRUD之常用注解及返回值

            BookController

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

    index.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Administrator
    4. Date: 2022/8/16
    5. Time: 21:52
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. Title

    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. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    10. Title
    11. SpringMVC 你好
    12. ${msg}

    13. ${lst}
    14. "${lst}" var="l">
    15. ${l.bid}:${l.bname}

    运行效果:

     

  • 相关阅读:
    【英语:发音基础】A2.单词、句子、意群发音
    每天一道算法题——动态规划
    不可忽视的字符函数与字符串函数:它们如何改变你的编程世界
    Win32 键盘鼠标模拟输入
    idea连接redis
    【算法-动态规划】最长公共子序列
    前端面试常见手写题
    11个开源测试自动化框架,如何选?
    从一线开发到技术总监,你就差一个赶鸭子上架
    ARM64 SMP多核启动详解1(spin_table)
  • 原文地址:https://blog.csdn.net/m0_68211831/article/details/126382930