添加须要的包后,在web.xml中添加以下配置web
<!-- Spring MVC配置 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/ApplicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/ApplicationContext.xml</param-value> </context-param>
注意里面有一个<load-on-startup> 表示启动容器时初始化该Servlet;spring
自此请求已交给Spring Web MVC框架处理,所以咱们须要配置Spring的配置文件,默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件。本示例为WEB-INF/ springMVC-servlet.xml。spring-mvc
由于我在mvc
<servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/ApplicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
中配置了指定的路径,因此个人WEB-INF/ springMVC-servlet.xml转移到了class路径下的spring包内的app
其余参数:框架
参数 | 做用 |
contextClass | 实现WebApplicationContext接口的类,当前的servlet用它来建立上下文。若是这个参数没有指定, 默认使用XmlWebApplicationContext。 |
contextConfigLocation | 传给上下文实例(由contextClass指定)的字符串,用来指定上下文的位置。这个字符串能够被分红多个字符串(使用逗号做为分隔符) 来支持多个上下文(在多上下文的状况下,若是同一个bean被定义两次,后面一个优先)。 |
namespace | WebApplicationContext命名空间。默认值是[server-name]-servlet。 |
ApplicationContext-mvc.xml中
jsp
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- 启用spring mvc 注解 --> <context:annotation-config /> <mvc:annotation-driven /> <!--<mvc:default-servlet-handler/>--> <!-- 设置使用注解的类所在的jar包 --> <context:component-scan base-package="com.test"></context:component-scan> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!-- 在Spring4以后也取消 HandlerAdapter <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> 完成请求和注解POJP的映射 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> --><!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
<mvc:annotation-driven />
一直出现url
No mapping found for HTTP request with URI [/SpringMVCTest/test] in DispatcherServlet with name 'springMVC'spa
注意这段代码,,本人由于未写这段话硬生生的查了两个多小时的资料才弄好。code
DispatcherServlet会利用一些特殊的bean来处理Request请求和生成相应的视图返回。
关于视图的返回,Controller只负责传回来一个值,而后到底返回的是什么视图,是由视图解析器控制的,在jsp中经常使用的视图解析器是InternalResourceViewResovler,它会要求一个前缀和一个后缀