配置springMVC框架
第一步:导入包依赖
<!--配置springMVC--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.7.RELEASE</version> </dependency>
第二部:编写web.xml--设置前端控制器
<servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>org.kong.config</param-value> </init-param> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</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>
第三部:编写springMVC配置类
1 package org.kong.config; 2 import org.springframework.context.annotation.ComponentScan; 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.web.servlet.config.annotation.EnableWebMvc; 5 6 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 7 8 @SuppressWarnings("deprecation") 9 @Configuration 10 //开启包扫描注解 11 @ComponentScan(basePackages="org.kong") 12 //开启mvc注解驱动 13 @EnableWebMvc 14 public class MVCconfig extends WebMvcConfigurerAdapter { 15 16 }mvc
第四部:编写controller组件类
1 package org.kong.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 public class Test { 8 @RequestMapping("/hellow") 9 public String hello() { 10 System.out.println("hellow"); 11 return null; 12 13 }
通过url访问得到