• 01-SpringMVC项目构建


    Overview

    梳理了创建基于SpringMVC的项目创建流程和注意点,防止遗忘

    1. 创建一个空项目

    在这里插入图片描述

    2. 添加新模块

    在这里插入图片描述

    3. 在pom文件中指定打包方式并刷新

    在这里插入图片描述

    4. 将模块目录结构改造成web项目

    在这里插入图片描述
    这里的web配置是你在pom文件中指定war的打包方式后就会出现的,当然没出现也可以新增一个web
    在这里插入图片描述
    点击+号新增项目的web.xml文件,主力需要主要web.xml的目录位置
    在这里插入图片描述
    这里是指定webapp所在的目录
    在这里插入图片描述
    下面是创建好的目录
    在这里插入图片描述

    5. 在pom文件中引入相关的依赖

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>org.examplegroupId>
        <artifactId>DemoartifactId>
        <version>1.0-SNAPSHOTversion>
        <packaging>warpackaging>
        <dependencies>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.2.5.RELEASEversion>
            dependency>
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.1.0version>
                <scope>providedscope>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    6.编写spring的配置文件

    最主要的是开启包扫描,和配置InternalResourceViewResolver,这里可以指定默认路径拼接的前缀和后缀

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
           
        <context:component-scan base-package="person.controller"/>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value=""/>
            <property name="suffix" value=""/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    7. 配置web.xml

    这里的DispatcherServlet可以理解为SpringMVC写的拦截器,所有action后缀的请求都会被它拦截处理。注意需要配置初始化参数,它代表了spring配置文件所在的地方

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             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_4_0.xsd"
             version="4.0">
            <servlet>
                <servlet-name>springMVCservlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
                <init-param>
                    <param-name>contextConfigLocationparam-name>
                    <param-value>classpath:springMVC.xmlparam-value>
                init-param>
            servlet>
        <servlet-mapping>
            <servlet-name>springMVCservlet-name>
            <url-pattern>*.actionurl-pattern>
        servlet-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    8. 配置Tomcat

    在这里插入图片描述

    9. 编写index.jsp文件

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Titletitle>
    head>
    <body>
    <form action="${pageContext.request.contextPath}/login.action" method="post">
      Name: <input type="text" name="name" /><br>
      Age: <input type="text" name="age" /><br>
      <input type="submit" value="submit">
    form>
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    编写具体的action类

    在这里插入图片描述
    你创建的类必须包含在包扫描的路径中,必须指定Controller注解,让spring帮助我们管理对象
    在方法上添加RequestMapping注解,value值代表了路径,主要需要省略.action后缀。

    至此项目可以跑通了,提交表单可以看到控制台打印的信息。

  • 相关阅读:
    无密码时代开启,谷歌率先实施!
    解析异常SAXParseExceptionis如何处理
    docker中使用GPU+rocksdb
    创建镜像发布到镜像仓库【不依赖docker环境】
    python面向对象编程:类和对象
    算法通关村第十四关:黄金挑战-数据流的中位数
    基于深度学习的图像生成与识别技术研究
    Vue3 脚手架创建项目
    TCP三次握手四次挥手深入
    apple MFI工厂认证,干货,为防止MFI工作人员查看,已设置VIP阅读
  • 原文地址:https://blog.csdn.net/qq_43152622/article/details/127985196