SpringMVC 注解(maven tomcat插件)启动的环境搭建
maven依赖
<!-- 整合springmvc框架依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- json对象-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.14</version>
</dependency>
控制(器)层类
package com.painter.controller;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
/**
* @Author: Painter
* @project_name: SpringMVC
* @system_login: sunshine
* @time: 2022/10/915:52
*/
/**
* @Controller 标记该类为springMVC控制类
*/
@Controller
public class PainterController { // 控制类
/**
* 访问到该请求返回json数据
* @return hashMap (json数据)
*
* @RequestMapping ("/get") url请求映射 和servlet一样
* @ResponseBody 响应正文
*/
@RequestMapping("/get")
@ResponseBody
public String get(){ // 此方法称之为接口
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("code","200");
hashMap.put("msg","OK");
// return "{'code':'200','msg':'OK'}";
return JSONObject.toJSONString(hashMap);
}
}
创建配置类
package com.painter.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @Author: Painter
* @project_name: SpringMVC
* @system_login: sunshine
* @time: 2022/10/916:00
*/
/**
* @Configuration 等同于创建以个xml配置文件
* @ComponentScan("com.painter.controller") 将com.painter.controller包下的所有类注入到ioc容器中
*
* 在springmvc原理 所有请求过来先达到我们的 DispatcherServlet 分发具体控制类 方法执行
*/
@Configuration
@ComponentScan("com.painter.controller")
public class SpringMVCConfig { // 配置类
}
注册配置类
package com.painter.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
/**
* @Author: Painter
* @project_name: SpringMVC
* @system_login: sunshine
* @time: 2022/10/916:05
*/
public class ServletInitializer extends AbstractDispatcherServletInitializer { // 注册配置类
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
// 注册我们的 springmvc config 配置类
annotationConfigWebApplicationContext.register(SpringMVCConfig.class);
return annotationConfigWebApplicationContext;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
maven tomcat 插件 依赖
<!-- 配置服务器插件 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<address>127.0.0.1</address> <!-- 访问地址 -->
<port>8080</port> <!--访问端口号-->
<path>/</path> <!-- 应用程序上下文 也是访问路径根路径-->
<ignorePackaging>true</ignorePackaging>
</configuration>
</plugin>
</plugins>
</build>
添加运行配置