01_SpringMVC入门

张创琦

基础文件

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.kkb</groupId>
  7. <artifactId>springmvc01</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <!-- web项目,需要引入war包 -->
  10. <packaging>war</packaging>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-webmvc</artifactId>
  15. <version>5.2.13.RELEASE</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>javax.servlet</groupId>
  19. <artifactId>javax.servlet-api</artifactId>
  20. <version>4.0.1</version>
  21. <!-- 不会运行上去 -->
  22. <scope>provided</scope>
  23. </dependency>
  24. </dependencies>
  25. <build>
  26. <plugins>
  27. <plugin>
  28. <groupId>org.apache.maven.plugins</groupId>
  29. <artifactId>maven-compiler-plugin</artifactId>
  30. <version>3.8.0</version>
  31. <configuration>
  32. <target>1.8</target>
  33. <source>1.8</source>
  34. </configuration>
  35. </plugin>
  36. <plugin>
  37. <groupId>org.apache.tomcat.maven</groupId>
  38. <artifactId>tomcat7-maven-plugin</artifactId>
  39. <version>2.2</version>
  40. <configuration>
  41. <path>/</path>
  42. <port>80</port>
  43. </configuration>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. <properties>
  48. <maven.compiler.source>8</maven.compiler.source>
  49. <maven.compiler.target>8</maven.compiler.target>
  50. </properties>
  51. </project>

applicationContext.xml

  1. <?xml version="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. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  7. <!-- spring配置文件,除了控制器之外的bean对象都在这里扫描, model层 -->
  8. <context:component-scan base-package="com.kkb.dao, com.kkb.service"/>
  9. </beans>

springmvc.xml

  1. <?xml version="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"
  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 https://www.springframework.org/schema/context/spring-context.xsd
  8. http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  9. <!-- springmvc的配置文件,控制器的bean对象都在这里扫描 -->
  10. <context:component-scan base-package="com.kkb.controller"/>
  11. <!--注解驱动-->
  12. <mvc:annotation-driven/>
  13. <!--视图解析器-->
  14. <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <!--前缀-->
  16. <property name="prefix" value="/jsp/"></property>
  17. <!--后缀-->
  18. <property name="suffix" value=".jsp"></property>
  19. </bean>
  20. </beans>

TeamService.java

  1. package com.kkb.service;
  2. import org.springframework.stereotype.Service;
  3. @Service
  4. public class TeamService {
  5. public void add(){
  6. System.out.println("TeamService-----add--------");
  7. }
  8. }

TeamController.java

  1. package com.kkb.controller;
  2. import com.kkb.service.TeamService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.servlet.ModelAndView;
  7. @Controller
  8. public class TeamController {
  9. @Autowired
  10. public TeamService teamService;
  11. // /hello.do也可
  12. @RequestMapping("hello.do")
  13. public ModelAndView add(){
  14. System.out.println("TeamController------add------");
  15. teamService.add();
  16. ModelAndView mv = new ModelAndView();
  17. mv.addObject("teamName", "湖人队"); //相当于Request.setAttribute("","")
  18. mv.setViewName("index"); //未来通过springmvc的视图解析器处理。转换成物理资源路径,相当于request.getRequestDispatcher("index.jsp").forward();
  19. //经过InternalResourceViewResolver对象的处理之后加上前后缀就变成了 /jsp/index.jsp
  20. return mv;
  21. }
  22. }

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!-- Spring配置 -->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:applicationContext.xml</param-value>
  10. </context-param>
  11. <listener>
  12. <!-- ContextLoaderListener默认监听WEB-INF下面的applicationContext.xml文件,所以上面的spring配置必须写 -->
  13. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  14. </listener>
  15. <!-- springmvc前端/核心/中央控制器 -->
  16. <servlet>
  17. <!-- 如果将servlet-name改为springmvc的话,那下面的mapping映射里的servlet-name也要改为springmvc,此时param-name找的是springmvc-servlet.xml -->
  18. <servlet-name>dispatcherServlet</servlet-name>
  19. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  20. <init-param>
  21. <param-name>contextConfigLocation</param-name> <!-- 会去找dispatcherServlet-servlet.xml -->
  22. <param-value>classpath:springmvc.xml</param-value>
  23. </init-param>
  24. <!--没有写的话现用现创建,大于0的话表示立即创建对象,数字越小优先级越高,小于0的话不会立即创建对象 -->
  25. <load-on-startup>1</load-on-startup>
  26. </servlet>
  27. <servlet-mapping>
  28. <servlet-name>dispatcherServlet</servlet-name>
  29. <url-pattern>*.do</url-pattern>
  30. </servlet-mapping>
  31. </web-app>

index.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: DELL
  4. Date: 2022/2/12
  5. Time: 20:41
  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>index</title>
  12. </head>
  13. <body>
  14. <h1>index----------${teamName}</h1>
  15. </body>
  16. </html>

说明:

1 整理创建web项目的流程(视频:SpringMVC入门程序)

2 JDK选择1.8

3 spring和springmvc版本号统一

5.2.13.RELEASE

4 springmvc的底层就是对servlet的底层封装

5 前端发送的请求将会去springmvc查找

6 方便配置文件的管理