项目是使用 Spring MVC

1)在浏览器中访问,后台总报错:

No mapping found for HTTP request with URI [/com.mimvp/welcome] in DispatcherServlet with name 'MVC'  

查了好半天,才发现是controller 没有扫描到

cd E:\myPro\workspace\com.mimvp.homer\src\main\resources\spring-mvc.xml

vim spring-mvc.xml

spring-mvc.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.1.xsd


http://www.springframework.org/schema/mvc

                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
                        
	<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
	<context:component-scan base-package="com.mimvp.com.controller" />
	
	<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	
	<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" />	<!-- JSON转换器 -->
			</list>
		</property>
	</bean>
	
	<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
	<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean> 

</beans>

上面xml配置文件里,扫描包前缀错误写成了:

<context:component-scan base-package="com.mimvp.com.controller" />

正确应该为:

<context:component-scan base-package="com.mimvp.homer.controller" />

controller 目录结构如下:

spring-mvc-no-mapping-found-for-http-request-with-uri-xxx-in-dispatcherservlet-with-name-mvc-01

 

2)问题解决之后,又报错:

No mapping found for HTTP request with URI [/com.mimvp/WEB-INF/jsp/welcome.jsp] in DispatcherServlet with name 'MVC'

结果发现是web.xml配置得有问题

E:\myPro\workspace\com.mimvp.homer\src\main\webapp\WEB-INF\web.xml
vim web.xml

下面是有问题的:

<servlet>  
	<servlet-name>spring2</servlet-name>  
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
	<load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
	<servlet-name>spring2</servlet-name>  
	<url-pattern>/*</url-pattern>  
</servlet-mapping>  

解决方法

把 url-pattern 由 /* 改为 /

 <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>
/</url-pattern>
  </servlet-mapping>

原因解释

1) <url-pattern>/*</url-pattern>  

该设置表示路径映射,*表示匹配所有,因此所有传入的路径都会被匹配到,进而交由DispatcherServlet处理,而[/myspring/WEB-INF/jsp/hello.jsp]没有对应的Controller,自然会发生报错。此时你需要<mvc:resources location="" mapping="">进行静态资源映射。例如,在 spring-mvc.xml 配置中:

<mvc:resources mapping="/" location="/index.jsp" />  
<mvc:resources mapping="/javascript/**" location="/static/javascript/" />  
<mvc:resources mapping="/styles/**" location="/static/css/" />  
<mvc:resources mapping="/images/**" location="/static/images/" /> 

2) <url-pattern>/</url-pattern>  

该设置表示将DispatcherServlet作为default Servlet(默认是org.apache.catalina.servlets.DefaultServlet),所有其他路径映射未匹配情况下才会交由它处理。而由于隐式映射的关系,使得 .jsp 扩展名被映射到静态资源进而被执行。 

3) 小结

/*强迫所有的请求及响应都经过该servlet
/将使你配置的servlet成为默认的servlet

更多请参考: Spring MVC:servlet 的url-pattern 匹配规则

 

web.xml 完整文件内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mybatis.xml</param-value>
  </context-param>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <async-supported>true</async-supported>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <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-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>
  <session-config>
    <session-timeout>15</session-timeout>
  </session-config>
</web-app>

 

 

参考推荐

Spring 具体分析 applicationContext.xml 和 spring3-servlet.xml

Maven 构建 Spring MVC + MySQL + Mybatis 详细完整示例

Spring MVC:servlet 的url-pattern 匹配规则