• SSM学习42:SpringMVC入门案例(重点)


    目录

    创建SpringMVC项目

     目录

    补全目录结构 :添加java项结构

    导入jar包

    添加tomcat运行快捷键

    创建配置类 SpringMvcConfig.class

    创建Controller类

    使用配置类ServletConfig替换web.xml

    运行结果


    因为SpringMVC是一个Web框架,将来是要替换Servlet,所以先来回顾下以前Servlet是如何进行开发的?

    1.创建web工程(Maven结构)

    2.设置tomcat服务器,加载web工程(tomcat插件)

    3.导入坐标(Servlet)

    4.定义处理请求的功能类(UserServlet)

    5.设置请求映射(配置映射关系)

    SpringMVC的制作过程和上述流程几乎是一致的,具体的实现流程是什么?

    1.创建web工程(Maven结构)

    2.设置tomcat服务器,加载web工程(tomcat插件)

    3.导入坐标(==SpringMVC==+Servlet)

    4.定义处理请求的功能类(==UserController==)

    5.==设置请求映射(配置映射关系)==

    6.==将SpringMVC设定加载到Tomcat容器中==

     

    创建SpringMVC项目

     目录

    补全目录结构 :添加java项结构

     

    导入jar包

    将pom.xml中多余的内容删除掉,再添加SpringMVC需要的依赖

    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>com.itheimagroupId>
    6. <artifactId>SpringMVCartifactId>
    7. <version>1.0-SNAPSHOTversion>
    8. <packaging>warpackaging>
    9. <name>SpringMVC 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. properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>junitgroupId>
    19. <artifactId>junitartifactId>
    20. <version>4.11version>
    21. <scope>testscope>
    22. dependency>
    23. <dependency>
    24. <groupId>javax.servletgroupId>
    25. <artifactId>javax.servlet-apiartifactId>
    26. <version>3.1.0version>
    27. <scope>providedscope>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframeworkgroupId>
    31. <artifactId>spring-webmvcartifactId>
    32. <version>5.2.1.RELEASEversion>
    33. dependency>
    34. dependencies>
    35. <build>
    36. <plugins>
    37. <plugin>
    38. <groupId>org.apache.tomcat.mavengroupId>
    39. <artifactId>tomcat7-maven-pluginartifactId>
    40. <version>2.1version>
    41. <configuration>
    42. <port>80port>
    43. <path>/path>
    44. configuration>
    45. plugin>
    46. plugins>
    47. build>
    48. project>

    添加tomcat运行快捷键

    创建配置类 SpringMvcConfig.class

    1. package com.itheima.config;
    2. import org.springframework.context.annotation.ComponentScan;
    3. import org.springframework.context.annotation.Configuration;
    4. @Configuration
    5. @ComponentScan("com.itheima.controller")
    6. public class SpringmvcConfig {
    7. }

    创建Controller类

    1. package com.itheima.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.ResponseBody;
    5. //@Controller:Springmvc的bean标志
    6. @Controller
    7. public class UserController {
    8. @RequestMapping("/save")
    9. @ResponseBody
    10. public String save(){
    11. System.out.println("郭浩康第一个Sprintmvc项目创建成功");
    12. return "{spring1save()...}";
    13. }
    14. }

    使用配置类ServletConfig替换web.xml

    将web.xml删除,换成ServletContainersInitConfig

    1. package com.itheima.config;
    2. import org.springframework.web.context.WebApplicationContext;
    3. import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
    4. import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
    5. public class ServletConfig extends AbstractDispatcherServletInitializer {
    6. //加载Springmvc容器
    7. @Override
    8. protected WebApplicationContext createServletApplicationContext() {
    9. AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
    10. ctx.register(SpringmvcConfig.class);
    11. return ctx;
    12. }
    13. //设置请求处理
    14. @Override
    15. protected String[] getServletMappings() {
    16. return new String[]{"/"};
    17. }
    18. //加载Spring容器
    19. @Override
    20. protected WebApplicationContext createRootApplicationContext() {
    21. return null;
    22. }
    23. }

    运行结果

  • 相关阅读:
    分布式文件存储 - - - MinIO从入门到飞翔
    树莓派上,docker下安装rancher与k8s,docker版本对应关系
    2022深圳工业展,正运动技术邀您一起开启激光加工智能制造!
    session.upload_progress进行文件包含和反序列化学习
    RabbitMQ 一文读懂
    modprobe和insmod的区别、 rmmod及modinfo
    基于JAVA鞍山丘比特房屋租赁管理系统计算机毕业设计源码+系统+lw文档+部署
    Rmarkdown教程
    微服务【同步和异步通讯详解】第6章
    jsoncpp库的使用及用httplib库搭建HTTP服务器
  • 原文地址:https://blog.csdn.net/weixin_51330376/article/details/127592010