Spring:No mapping found for HTTP request with URI

拥有回忆 提交于 2020-01-05 17:15:12

问题


I have started using spring and am encountering this error

No mapping found for HTTP request with URI [/SpringSocialSample/login.htm] in DispatcherServlet with name 'SpringSocialSample'

I figured that login.htm cant be located by dispatcher servlet

My SpringSocialSample-servlet.xml

<context:component-scan
        base-package="com.social.spring.controllers" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

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" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" 
version="2.5">
  <display-name>SpringSocialSample</display-name>

  <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>updatestatus.root</param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <servlet>
    <servlet-name>SpringSocialSample</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringSocialSample</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

I have index.jsp where I redirect to login.jsp placed under WEB-INF/views..


回答1:


You've mapped the Spring Dispatcher to *.htm, so each time a url matching that pattern is requested, the dispatcher will look for a controller mapped to that specific URL request, it will NOT try to load static files (aka, your login.html html file is effectivelly hidden by this config, you cannot return it unless you first pass through a Spring Controller that sends it back as a view). You create a Spring Controller that returns that page, map it to the [login.htm] URI, and then Spring will no longer complain that it cannot find a mapping for that URL:

Check out the chapter "13.4. Handler mappings" from :

http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html



来源:https://stackoverflow.com/questions/7785308/springno-mapping-found-for-http-request-with-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!