Springmvc中重要的组件
DispatcherServlet :前端控制器,接收了所有的请求(如果配置/就不包括jsp)
HandleMapping:解析请求的格式,执行判断要执行哪一个方法
HandleAdapter:负责调用具体的方法
ViewResouler:视图解析器,解析结果,准备跳转到具体的页面
配置springmvc
第一导入相关的jar ,spring的相关jar 再加spring-webmvc.jar
第二步配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 核心控制器的配置DispatcherServlet --> <servlet> <servlet-name>springmvc1</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加载springMVC的配置文件 --> <init-param> <!-- 通过下面的参数指定配置文件的位置 --> <param-name>contextConfigLocation</param-name> <!-- 改变默认路径--> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 标记我们的容器在启动的时候就要去加载这个DispatchServlet --> <load-on-startup>1</load-on-startup> </servlet> <!-- 下面的配置的意思是一个名字叫springmvc的servlet会拦截和处理这个项目中的所有的url请求 --> <servlet-mapping> <servlet-name>springmvc1</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
spring容器和springmvc时父子容器关系
sringmvc能够调用spring 容器的所有内容
第三配置相关springmvc配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 扫描com.shsxt.controller 下包 --> <context:component-scan base-package="com.Controller"></context:component-scan> <!-- 注解扫描 --> <mvc:annotation-driven></mvc:annotation-driven> </beans>
静态资源的引入: location:项目资源所在 mapping: url的地址
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"></mvc:resources>