环境的搭建
1. 在idea中创建一个maven项目
2. 把这个项目弄成javaweb项目
- 2.1 在项目结构中facets中添加web模块并修改对应的文件路径
/Users/lovelves/IdeaProjects/lovelvesOne/src/main/webapp/WEB-INF/web.xml
-
2.2 创建artifact
-
-
-
2.3 配置tomcat(在更新选项中选择update calsses and resources 每次代码变动 不需重新启动服务器)
-
2.4 把artifact添加到tomcat中
-
2.5测试tomcat 是否能运行成功 在webapp中创建index.html文件 输入一些文字 当浏览器显示你输入的文字就就表示创建成功
-
2.6 mac 中tomcat启动时遇到一些麻烦 就是 显示一堆英文
-
2.7. 添加所需依赖permission denine 解决方法 在tomacat中的bin目录下 用终端运行 这行代码 chmod u+x *.sh
- 添加所需要的依赖 并设置相对应的配置文件
3.1在pom.xml 添加springmvc依赖 和啊里云仓库 由于国内网速慢
<?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>lovelvesOne</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/repository/public</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.11.RELEASE</version>
</dependency>
</dependencies>
</project>
3.2 在web.xml中设置servlet 来处理请求 并指定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_3_1.xsd"
version="3.1">
<servlet>
<!-- DispatcherServlet 是springmvc的核心 用来拦截http请求 并根据url调用与之对应的controller方法 来完成http请求处理-->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!-- 在web启动时 自动创建springioc容器并初始化DispatcherServlet-->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.3 在applicationContext.xml中配置扫描开启启动注解模式 过滤图片
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 这行的作用 springioc 在初始化项目的时候自动创建并管理 com.lovelves.springmvc中的包及子包中拥有的对象
@Repository
@Service
@Controller
@Component
-->
<context:component-scan base-package="com.lovelves.springmvc"></context:component-scan>
<!-- 启用mvc注解模式-->
<mvc:annotation-driven/>
<!-- 将图片资源等排除过滤 提供性能-->
<mvc:default-servlet-handler/>
</beans>
- 创建控制器类测试项目是否正常运行 运行成功网页返回succes
package com.lovelves.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@GetMapping("/t") //路由
@ResponseBody //直接相应字符串内容给浏览器
public String test(){
return "succes";
}
}
4.2 注意 在运行之前把artifacts中的包引入项目中的lib
4.3 注意2 maven中java版本设置 否者 无法运行 显示不支持版本5
5.4 运行成功😄