01-SpringMVC项目构建

Overview

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

1. 创建一个空项目

在这里插入图片描述

2. 添加新模块

在这里插入图片描述

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

在这里插入图片描述

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

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

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

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <!-- spring mvc,引入之后关于spring的相关依赖会自动引入       -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <!--  servlet的依赖      -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

6.编写spring的配置文件

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

<?xml version="1.0" encoding="UTF-8"?>
<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>

7. 配置web.xml

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

<?xml version="1.0" encoding="UTF-8"?>
<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>springMVC</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springMVC.xml</param-value>
            </init-param>
        </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
</web-app>

8. 配置Tomcat

在这里插入图片描述

9. 编写index.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</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>

编写具体的action类

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值