• SpringMVC入门


    目录

    一,SpringMVC简介

    二,helloworld实现

    三,SpringMVC的工作原理

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


    一,SpringMVC简介

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

    也有人声称SpringMVC
    是视图层框架 :原因:SpringMVC部分功能是向用户渲染展示数据
    是控制层框架 原因:DispatherServlet:接受浏览器端的任意请求
    然后寻找对应的 子控制器(适配器*Adapter)处理浏览器请求

    二者皆有所理

    二,helloworld实现

    我们使用框架首先第一步:搭建环境

    1.导入pom依赖

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>org.examplegroupId>
    6. <artifactId>ssmartifactId>
    7. <version>1.0-SNAPSHOTversion>
    8. <packaging>warpackaging>
    9. <name>ssm Maven Webappname>
    10. <url>http://www.example.comurl>
    11. <properties>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <maven.compiler.source>1.8maven.compiler.source>
    14. <maven.compiler.target>1.8maven.compiler.target>
    15. <maven.compiler.plugin.version>3.7.0maven.compiler.plugin.version>
    16. <spring.version>5.0.2.RELEASEspring.version>
    17. <mybatis.version>3.4.5mybatis.version>
    18. <mysql.version>5.1.44mysql.version>
    19. <pagehelper.version>5.1.2pagehelper.version>
    20. <mybatis.spring.version>1.3.1mybatis.spring.version>
    21. <commons.dbcp2.version>2.1.1commons.dbcp2.version>
    22. <commons.pool2.version>2.4.3commons.pool2.version>
    23. <log4j2.version>2.9.1log4j2.version>
    24. <junit.version>4.12junit.version>
    25. <servlet.version>4.0.0servlet.version>
    26. <lombok.version>1.18.2lombok.version>
    27. properties>
    28. <dependencies>
    29. <dependency>
    30. <groupId>org.springframeworkgroupId>
    31. <artifactId>spring-contextartifactId>
    32. <version>${spring.version}version>
    33. dependency>
    34. <dependency>
    35. <groupId>org.springframeworkgroupId>
    36. <artifactId>spring-ormartifactId>
    37. <version>${spring.version}version>
    38. dependency>
    39. <dependency>
    40. <groupId>org.springframeworkgroupId>
    41. <artifactId>spring-txartifactId>
    42. <version>${spring.version}version>
    43. dependency>
    44. <dependency>
    45. <groupId>org.springframeworkgroupId>
    46. <artifactId>spring-aspectsartifactId>
    47. <version>${spring.version}version>
    48. dependency>
    49. <dependency>
    50. <groupId>org.springframeworkgroupId>
    51. <artifactId>spring-webartifactId>
    52. <version>${spring.version}version>
    53. dependency>
    54. <dependency>
    55. <groupId>org.springframeworkgroupId>
    56. <artifactId>spring-testartifactId>
    57. <version>${spring.version}version>
    58. dependency>
    59. <dependency>
    60. <groupId>org.mybatisgroupId>
    61. <artifactId>mybatisartifactId>
    62. <version>${mybatis.version}version>
    63. dependency>
    64. <dependency>
    65. <groupId>mysqlgroupId>
    66. <artifactId>mysql-connector-javaartifactId>
    67. <version>${mysql.version}version>
    68. dependency>
    69. <dependency>
    70. <groupId>com.github.pagehelpergroupId>
    71. <artifactId>pagehelperartifactId>
    72. <version>${pagehelper.version}version>
    73. dependency>
    74. <dependency>
    75. <groupId>org.mybatisgroupId>
    76. <artifactId>mybatis-springartifactId>
    77. <version>${mybatis.spring.version}version>
    78. dependency>
    79. <dependency>
    80. <groupId>org.apache.commonsgroupId>
    81. <artifactId>commons-dbcp2artifactId>
    82. <version>${commons.dbcp2.version}version>
    83. <exclusions>
    84. <exclusion>
    85. <artifactId>commons-pool2artifactId>
    86. <groupId>org.apache.commonsgroupId>
    87. exclusion>
    88. exclusions>
    89. dependency>
    90. <dependency>
    91. <groupId>org.apache.commonsgroupId>
    92. <artifactId>commons-pool2artifactId>
    93. <version>${commons.pool2.version}version>
    94. dependency>
    95. <dependency>
    96. <groupId>org.apache.logging.log4jgroupId>
    97. <artifactId>log4j-coreartifactId>
    98. <version>${log4j2.version}version>
    99. dependency>
    100. <dependency>
    101. <groupId>org.apache.logging.log4jgroupId>
    102. <artifactId>log4j-apiartifactId>
    103. <version>${log4j2.version}version>
    104. dependency>
    105. <dependency>
    106. <groupId>org.apache.logging.log4jgroupId>
    107. <artifactId>log4j-webartifactId>
    108. <version>${log4j2.version}version>
    109. dependency>
    110. <dependency>
    111. <groupId>junitgroupId>
    112. <artifactId>junitartifactId>
    113. <version>${junit.version}version>
    114. <scope>testscope>
    115. dependency>
    116. <dependency>
    117. <groupId>javax.servletgroupId>
    118. <artifactId>javax.servlet-apiartifactId>
    119. <version>${servlet.version}version>
    120. <scope>providedscope>
    121. dependency>
    122. <dependency>
    123. <groupId>org.projectlombokgroupId>
    124. <artifactId>lombokartifactId>
    125. <version>${lombok.version}version>
    126. <scope>providedscope>
    127. dependency>
    128. <dependency>
    129. <groupId>org.springframeworkgroupId>
    130. <artifactId>spring-webmvcartifactId>
    131. <version>${spring.version}version>
    132. dependency>
    133. <dependency>
    134. <groupId>jstlgroupId>
    135. <artifactId>jstlartifactId>
    136. <version>1.2version>
    137. dependency>
    138. <dependency>
    139. <groupId>taglibsgroupId>
    140. <artifactId>standardartifactId>
    141. <version>1.1.2version>
    142. dependency>
    143. dependencies>
    144. <build>
    145. <finalName>ssmfinalName>
    146. <resources>
    147. <resource>
    148. <directory>src/main/javadirectory>
    149. <includes>
    150. <include>**/*.xmlinclude>
    151. includes>
    152. resource>
    153. <resource>
    154. <directory>src/main/resourcesdirectory>
    155. <includes>
    156. <include>jdbc.propertiesinclude>
    157. <include>*.xmlinclude>
    158. includes>
    159. resource>
    160. resources>
    161. <pluginManagement>
    162. <plugins>
    163. <plugin>
    164. <groupId>org.apache.maven.pluginsgroupId>
    165. <artifactId>maven-compiler-pluginartifactId>
    166. <version>${maven.compiler.plugin.version}version>
    167. <configuration>
    168. <source>${maven.compiler.source}source>
    169. <target>${maven.compiler.target}target>
    170. <encoding>${project.build.sourceEncoding}encoding>
    171. configuration>
    172. plugin>
    173. <plugin>
    174. <groupId>org.mybatis.generatorgroupId>
    175. <artifactId>mybatis-generator-maven-pluginartifactId>
    176. <version>1.3.2version>
    177. <dependencies>
    178. <dependency>
    179. <groupId>mysqlgroupId>
    180. <artifactId>mysql-connector-javaartifactId>
    181. <version>${mysql.version}version>
    182. dependency>
    183. dependencies>
    184. <configuration>
    185. <overwrite>trueoverwrite>
    186. configuration>
    187. plugin>
    188. <plugin>
    189. <artifactId>maven-clean-pluginartifactId>
    190. <version>3.1.0version>
    191. plugin>
    192. <plugin>
    193. <artifactId>maven-resources-pluginartifactId>
    194. <version>3.0.2version>
    195. plugin>
    196. <plugin>
    197. <artifactId>maven-compiler-pluginartifactId>
    198. <version>3.8.0version>
    199. plugin>
    200. <plugin>
    201. <artifactId>maven-surefire-pluginartifactId>
    202. <version>2.22.1version>
    203. plugin>
    204. <plugin>
    205. <artifactId>maven-war-pluginartifactId>
    206. <version>3.2.2version>
    207. plugin>
    208. <plugin>
    209. <artifactId>maven-install-pluginartifactId>
    210. <version>2.5.2version>
    211. plugin>
    212. <plugin>
    213. <artifactId>maven-deploy-pluginartifactId>
    214. <version>2.8.2version>
    215. plugin>
    216. plugins>
    217. pluginManagement>
    218. build>
    219. project>

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

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

    2.框架配置文件

    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.ljj.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. beans>

     注意:此依赖为设置访问路径

    3.web.xml配置核心类

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    4. version="3.1">
    5. <display-name>Archetype Created Web Applicationdisplay-name>
    6. <context-param>
    7. <param-name>contextConfigLocationparam-name>
    8. <param-value>classpath:applicationContext.xmlparam-value>
    9. context-param>
    10. <listener>
    11. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    12. listener>
    13. <servlet>
    14. <servlet-name>SpringMVCservlet-name>
    15. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    16. <init-param>
    17. <param-name>contextConfigLocationparam-name>
    18. <param-value>/WEB-INF/springmvc-servlet.xmlparam-value>
    19. init-param>
    20. <load-on-startup>1load-on-startup>
    21. <async-supported>trueasync-supported>
    22. servlet>
    23. <servlet-mapping>
    24. <servlet-name>SpringMVCservlet-name>
    25. <url-pattern>/url-pattern>
    26. servlet-mapping>
    27. web-app>

    放置框架配置文件的路径

     二,helloworld实现

    HelloController:

    1. package com.ljj.ssm.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. /**
    5. * @author ljj
    6. * @site www.ljj.com
    7. * @create 2022-08-17 2:49
    8. */
    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. @RequestMapping("/helloReq")
    22. public String hello(){
    23. System.out.println("hello springmvc...");
    24. return "hello";
    25. }
    26. }

    Hello.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Administrator
    4. Date: 2022/8/17
    5. Time: 8:23
    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. Spring Mvc...你好 oyang
    15. body>
    16. html>

    运行效果:

     输入网址:

    http://localhost:8080/helloReq
    

    三、SpringMVC的工作原理

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

     四,CRUD及常用注解及返回值

    所用的注解

    Controller

    Requestmapping

    Requestparam

    1. package com.ljj.ssm.controller;
    2. import com.ljj.ssm.biz.BookBiz;
    3. import com.ljj.ssm.model.Book;
    4. import com.ljj.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. <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=sb&price=9.9">增加a>
    16. <a href="${pageContext.request.contextPath}/book/edit?bid=2&bname=suibian&price=9.8">修改a>
    17. <a href="${pageContext.request.contextPath}/book/del/2">删除a>
    18. body>
    19. html>

    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. <html>
    11. <head>
    12. <title>Titletitle>
    13. head>
    14. <body>
    15. SpringMVC 你好
    16. ${msg}
    17. <hr>
    18. ${lst}
    19. <c:forEach items="${lst}" var="l">
    20. ${l.bid}:${l.bname}<hr>
    21. c:forEach>
    22. body>
    23. html>

    点击新增后:

     补充:springMvc的工作原理

    1、浏览器发送请求到中央控制器Dispatcherservlet

    2、中央控制器DispatcherServlet通过处理器映射器HandlerMapping找到处理器适配器HandlerAdapter

    3、执行HandlerAdapter,返回值ModelAndView回传给DispatcherServlet

    4、视图解析器viewResolver会对 返回值ModelAndView进行渲染,得到view视图对象

    5、最后DispatcherServlet会将view视图对象返回给用户

     转发与重定向的设置

            "hello"转发到/hello.jsp

            "forward:hello" 转发到/hello.jsp

            "redirect:/hello" 重定向到.../项目名/hello    book/list

    四、静态资源的配置

  • 相关阅读:
    SpringMVC中异常处理详解
    Maven 换源 & Mybatis 开启 Log4j 日志框架
    JS 流行框架(六):Animate
    千万级用户的大型网站,如何进行服务器压力预估?
    Python 基础面试第三弹
    自动控制原理4.2---根轨迹绘制的基本法则
    leetCode 1035.不相交的线 动态规划 + 滚动数组 (最长公共子序列)
    【Android知识笔记】热修复专题
    部署Envoy
    java毕业设计教师档案管理系统mybatis+源码+调试部署+系统+数据库+lw
  • 原文地址:https://blog.csdn.net/weixin_64313980/article/details/126376676