No mapping found for HTTP request with URI [/test/login/login2.html] in Disp

我配置的SpringMVC,在web.xml文件中拦截的是*.html,Controller类中的方法能接受到页面请求,返回一个ModelAndView页面视图,返回htm或jsp页面都可以跳转到请求页面,同样的配置只要返回html页面就会报No mapping found for HTTP request with URI [/test/login/login2.html] in Disp,捯饬了一下午加一晚上,在网上查了很多,终于找到原因了,只需要在servlet.xml文件中配置下面一句话即可,然后百度一下这句话作用,配置SpringMVC静态资源

下面是我的配置文件

web.xml配置文件



  test
 
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
 

 
 
 
  test
  org.springframework.web.servlet.DispatcherServlet
 
  contextConfigLocation
  classpath:springmvc_servlet.xml
 

  1
 

 
  test
  *.html
 

 
 


   
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
       
            encoding
            UTF-8
       

       
            forceEncoding
            true
       

   

   
        encodingFilter
        *
   


 
 
  BASIC
 

 
 
 


springmvc_servlet.xml配置文件


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

  
   

  

    


   
   
    
   
    
    
    
    
    

    

    

    
    
         
    
    
    
         
    

    
   


controller类

@Controller
@RequestMapping(value="/login/")
public class Login {
    @RequestMapping(value="login")
    public ModelAndView login(){
        System.out.println("sssssssssssssssssss");
        ModelAndView mv= new ModelAndView();
        mv.setViewName("login2");
        
        return mv;
        
    }

}

以上是我的整个代码,希望对你有用




下面是转发复制别人的配置静态页面的三种方法



如何你的DispatcherServlet拦截 *.do这样的URL,就不存在访问不到静态资源的问题。如果你的DispatcherServlet拦截“/”,拦截了所有的请求,同时对*.js,*.jpg的访问也就被拦截了。

 目的:可以正常访问静态文件,不要找不到静态文件报404。
 
方案一:激活Tomcat的defaultServlet来处理静态文件


    default
    *.jpg


    default
    *.js


    default
    *.css
  
要配置多个,每种文件配置一个    

要写在DispatcherServlet的前面, 让defaultServlet先拦截,这个就不会进入Spring了,我想性能是最好的吧。

Tomcat, Jetty, JBoss, and GlassFish  默认 Servlet的名字 -- "default"
Google App Engine 默认 Servlet的名字 -- "_ah_default"
Resin 默认 Servlet的名字 -- "resin-file"
WebLogic 默认 Servlet的名字  -- "FileServlet"
WebSphere  默认 Servlet的名字 -- "SimpleFileServlet"

方案二: 在spring3.0.4以后版本提供了mvc:resources 

<mvc:resources 的使用方法:


<mvc:resources mapping="/images/**" location="/images/" />

  /images /**映射到 ResourceHttpRequestHandler 进行处理,location指定静态资源的位置.可以是web application根目录下、jar包里面,这样可以把静态资源压缩到jar包中。cache-period可以使得静态资源进行web cache  

如果出现下面的错误,可能是没有配置 <mvc:annotation-driven /> 的原因。 
报错WARNING: No mapping found for HTTP request with URI [/mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springMVC'

使用 <mvc:resources/> 元素,把 mapping 的 URI 注册到 SimpleUrlHandlerMapping的urlMap 中,
key 为 mapping 的 URI pattern值,而 value为 ResourceHttpRequestHandler,
这样就巧妙的把对静态资源的访问由 HandlerMapping 转到 ResourceHttpRequestHandler 处理并返回,所以就支持 classpath 目录, jar 包内静态资源的访问.
另外需要注意的一点是,不要对 SimpleUrlHandlerMapping 设置 defaultHandler. 因为对 static uri 的 defaultHandler 就是ResourceHttpRequestHandler,
否则无法处理static resources request.

方案三 ,使用<mvc:default-servlet-handler/>

本文原始链接:http://unmi.cc/spring-mvc-access-static-resources-jpgjscss, 来自:隔叶黄莺 Unmi Blog
<mvc:default-servlet-handler/>

会把 "/**" url,注册到 SimpleUrlHandlerMapping 的 urlMap 中,把对静态资源的访问由 HandlerMapping 转到 org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler 处理并返回.
DefaultServletHttpRequestHandler 使用就是各个 Servlet 容器自己的默认 Servlet.

补充说明:多个HandlerMapping的执行顺序问题:

DefaultAnnotationHandlerMapping 的 order 属性值是:0

<mvc:resources/ >自动注册的 SimpleUrlHandlerMapping 的 order 属性值是: 2147483646

<mvc:default-servlet-handler/>自动注册的 SimpleUrlHandlerMapping 的 order 属性值是:2147483647

spring 会先执行 order 值比较小的。当访问一个 a.jpg 图片文件时,先通过 DefaultAnnotationHandlerMapping 来找处理器,一定是找不到的,我们没有叫 a.jpg 的 Action。再按 order 值升序找,由于最后一个 SimpleUrlHandlerMapping 是匹配 "/**" 的,所以一定会匹配上,再响应图片。

访问一个图片,还要走层层匹配。真不知性能如何?改天做一下压力测试,与Apache比一比。

最后再说明一下,如何你的 DispatcherServlet 拦截 *.do 这样的 URL,就不存上述问题了。

转自:http://lzy83925.iteye.com/blog/1186609


你可能感兴趣的:(不能返回静态网页问题,Springmvc)