1:web.xml> 配置DispatcherServlet
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatcherServlet要绑定spring的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--
/:匹配的是所有的请求,但不包含jsp页面请求
/*:匹配的是所有请求,以及jsp页面
-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2 配置springmvc-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理-->
<context:component-scan base-package="com.kuang.controller"/>
<!--让spring mvc不处理静态资源-->
<mvc:default-servlet-handler/>
<!--
支持MVC注解驱动
在spring中一般采用@RequestMapping注解来完成映射关系,
要想@RequestMapping生效,必须向上下文中注册
DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 实例
这两个实例分别在类级别和方法级别处理
而annotation driven配置帮助我们自动完成上述两个实例的注入-->
<mvc:annotation-driven/>
<!--上面三个配置是固定的-->
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
注:一般springmvc-servlet.xml配置好以后就固定不再改动了
3 创建controller,其中@RequestMapping可以作用到类和方法上面,存在父子关系。@RestController不会被视图解析器解析,返回的只是一个字符串。
----------------------------------------------------------------------------------
**注意:**一般新建项目后,File—>project structure里面,在WEB-INF目录下创建lib目录,把所有的相关依赖包放到lib目录下,然后编译部署。