idea中使用SpringMVC创建web项目(Maven创建)

第一步

下载安装tomcat,配置环境变量;
传送门

第二步创建项目

在这里插入图片描述
在这里插入图片描述

创建java文件夹之后就左键双击,找到下边窗口
在这里插入图片描述

第三步 配置类编写

package config;

import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

public class WebApplicationContext extends AbstractDispatcherServletInitializer {
    // 加载SpringMvc容器配置
    @Override
    protected org.springframework.web.context.WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
        cxt.register(SpringMVCConfig.class);
        return cxt;
    }

    // 设置SpringMvc处理的请求
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    // 加载spring容器配置
    @Override
    protected org.springframework.web.context.WebApplicationContext createRootApplicationContext() {
        return null;
    }
}

SpringMVC配置类

package config;

import org.springframework.context.annotation.*;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan({"service","mapper","dao","controller"})
@PropertySource("jdbc.proprerties")
@Import({config.MybatisConfig.class,config.JDBCConfig.class,config.WebApplicationContext.class})
@EnableTransactionManagement
@EnableAspectJAutoProxy
public class SpringMVCConfig {
}

第四步:添加依赖和tomcat需要的插件

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.22</version>
    </dependency>

  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <!-- 使用tomcat9部署应用 此处的server标签中的值与第二步maven配置文件中的id值一致-->
          <server>tomcat9</server>
          <update>true</update>
        </configuration>
      </plugin>
    </plugins>
  </build>

问题1: tomcat9没有对应的插件
解决方案:传送门

问题2:在上一步找不到settings.xml
解决方案:传送门

问题3:选择和tomcat一致的jdk
在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值