目录
(2)在web.xml中配置前端控制器DispatcherServlet
(3)编写SpringMVC核心配置文件springmvc.xml
我们创建的是一个maven项目,并且它是从原型创建的,如下:
![]()
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.2.12.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webartifactId>
- <version>5.2.12.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.2.12.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>servlet-apiartifactId>
- <version>2.5version>
- <scope>providedscope>
- dependency>
-
- <dependency>
- <groupId>javax.servlet.jspgroupId>
- <artifactId>jsp-apiartifactId>
- <version>2.0version>
- <scope>providedscope>
- dependency>
- dependencies>
-
-
- <build>
- <plugins>
-
- <plugin>
- <groupId>org.apache.tomcat.mavengroupId>
- <artifactId>tomcat7-maven-pluginartifactId>
- <version>2.1version>
- <configuration>
- <port>8080port>
- <path>/path>
- <uriEncoding>UTF-8uriEncoding>
- <server>tomcat7server>
- <systemProperties>
- <java.util.logging.SimpleFormatter.format>%1$tH:%1$tM:%1$tS %2$s%n%4$s: %5$s%6$s%n
- java.util.logging.SimpleFormatter.format>
- systemProperties>
- configuration>
- plugin>
- plugins>
- build>
- <web-app>
- <display-name>Archetype Created Web Applicationdisplay-name>
-
- <servlet>
- <servlet-name>dispatcherServletservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:springmvc.xmlparam-value>
- init-param>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>dispatcherServletservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- web-app>
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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">
-
-
-
- <context:component-scan base-package="com.itbaizhan"/>
-
-
-
- <mvc:annotation-driven/>
-
-
- beans>
- @Controller
- public class MyController1 {
- // 该方法的访问路径是/c1/hello1
- @RequestMapping("/c1/hello1")
- public void helloMVC(){
- System.out.println("hello SpringMVC!");
- }
- }
(1)点击配置,如图的maven标志,在没有配置时上面写的是添加配置
![]()
(2)点击左上角的加号
(3)找到maven标志,并且点击
(4)命令行中如下命令即可
使用tomcat插件启动项目,访问 http://localhost:8080/c1/hello1
出现如上两种即为成功!