2023快速搭建并初始化SpringMVC框架项目
一.首先idea先创建Java EE
这个部分我在另一个文章写了,需要的请移步到:Idea配置JavaWeb项目 2023最新 (图文详细版)
所以这里就先跳过啦!
二.配置SpringMVC框架 1.安装Springmvc依赖①打开pom.xml文件
②添加依赖
javax.servlet javax.servlet-api 3.1.0 privoded org.springframework spring-webmvc 5.2.3.RELEASE 1.1.记得刷新一下依赖哦 2. 配置SpringMvc控制器用来处理请求2.1 新建一个Controller包下的uerContorller,用来接收请求
package Contorller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;//2. 配置SpringMvc控制器用来处理请求@Controllerpublic class UerContorller { @RequestMapping("/save") @ResponseBody public String Save(){ System.out.println("user save ... "); return "SpringMvc"; }} 3.添加SpringMVC配置类创建Springmvc的配置类,并加载到ioc容器中,
package Config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;//3.创建Springmvc的配置类,并加载到ioc容器中,// 加载contorller对应的bean@Configuration@ComponentScan("Contorller")public class SpringMvcconfig {} 4.初始化Springmvc环境 package Config;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;//4.定义一个servlet容器启动配置类,加载到Spring配置public class SpringMvcInitConfig extends AbstractDispatcherServletInitializer {// 初始化Springmvc环境 @Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext acwac = new AnnotationConfigWebApplicationContext(); acwac.register(SpringMvcconfig.class); return acwac; }//配置Mapping路径为"/",代表所有接口接收 @Override protected String[] getServletMappings() { return new String[]{"/"}; } //初始化spring 环境 @Override protected WebApplicationContext createRootApplicationContext() { return null; }} 5.运行(访问接口/save接口数据)