第一步
下载安装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