内容概述:
1,搭建环境(导入配置,编写配置文件,完善项目结构)
2,编写测试用的入门程序
3,部署项目到tomcat服务器上
4,运行成功查看
一,搭建环境:
1,建立webapp项目,在main下面建立java,resources文件,并设为源文件。
图一:
图二:
图三:
图四:
图五:
2,在pom.xml的文件中导入SpringMVC需要的配置
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>10.0.1</maven.compiler.source>
<maven.compiler.target>10.0.1</maven.compiler.target>
<spring.version>5.2.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
3,在resources文件下建立SpringMVC.xml文件,配置里面的基本信息:
<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:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 配置组件扫描 -->
<context:component-scan base-package="cn.itcast" />
<!-- 配置MVC注解扫描 -->
<mvc:annotation-driven />
<!-- 配置视图解析器, -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" /><!-- 前缀 -->
<property name="suffix" value=".jsp" /><!-- 后缀 -->
</bean>
4,在web.xml配置SpringMVC前端控制器的映射,和初始化
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
二,编写测试文件
1,编写index.jsp文件,模拟前端页面发送一个请求
2,前端控制器接收到请求,处理请求
3,视图解析器接收处理请求后返回的信息,跳转到目标页面
4,跳转的页面
三,部署服务器(已经安装tomcat服务器)
图一:选择右上角的编辑
图二:选择本地的tomcat服务器
图三:编写必要的信息
图四:添加要部署的项目
四,测试结果:
图一:点击右上角运行的按钮
图二:运行成功后,自动跳转到浏览器页面,点击链接模拟发送请求
图三:成功跳转到success.jsp页面,并在服务端打印处理请求方法中的信息