1.创建web工程
2.进行相关配置,选择"web app"
3.耐心等待下载完成,生成项目结构
4.添加一些所需的包(根据自己的需求添加)
5.配置服务器,我这里是使用已下载好的Tomcat,建议使用本地的比较方便。如果没有,可以在官网上下载。(已有的话,直接忽略这一步)
下载tomcat地址: https://tomcat.apache.org/
下载Tomcat步骤:
6.下载好之后,直接解压即可,再进行相关的配置,点击OK,会自动生成相关信息
注:选择浏览器和JRE版本之后,先不要点击OK!还需要将自己创建的project加载到tomcat中。
7.将项目打包放到tomcat容器中
8.根据自己的需求选择打包的类型,我这儿选择war类型。(建议选择打包,不打包可能项目会比较大)
9.配置完成之后,点击Apply==>>OK,tomcat服务器就创建好啦。
10.添加依赖=>>pom.xml文件
添加servletApi和springWeb依赖
11.配置web-app ,在web.xml中添加代码
添加以下代码
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
12.配置SpringMvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 注释扫描-->
<context:component-scan base-package="com.edu"/>
<!--注释驱动:将注解的功能转化为springMvc的功能-->
<mvc:annotation-driven/>
<!-- springMVC 的视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
13.可以进行测试一下
前端显示
控制台输入